name: Deploy Containers on: push: tags: - "addin-*.*.*" - "backend-*.*.*" - "web-*.*.*" workflow_dispatch: permissions: contents: read env: REGISTRY: ${{ vars.CONTAINER_REGISTRY || 'ghcr.io' }} BACKEND_IMAGE: ${{ vars.IMAGE_NAMESPACE }}/word-versioning-backend WEB_IMAGE: ${{ vars.IMAGE_NAMESPACE }}/word-versioning-web BACKEND_TAG: ${{ github.ref_name || 'latest' }} WEB_TAG: ${{ github.ref_name || 'latest' }} DEPLOY_HOST: ${{ vars.SSH_DEPLOY_HOST }} DEPLOY_USER: ${{ vars.SSH_DEPLOY_USER }} jobs: build-and-push-backend: name: Build and push backend image runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ vars.CONTAINER_REGISTRY_USERNAME }} password: ${{ secrets.CONTAINER_REGISTRY_PASSWORD }} - name: Build backend image uses: docker/build-push-action@v6 with: context: . file: ./backend/WordVersioning.Api/Dockerfile push: true tags: | ${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE }}:${{ env.BACKEND_TAG }} ${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE }}:latest build-and-push-web: name: Build and push web image runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Log in to registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ vars.CONTAINER_REGISTRY_USERNAME }} password: ${{ secrets.CONTAINER_REGISTRY_PASSWORD }} - name: Build web image uses: docker/build-push-action@v6 with: context: ./web file: ./web/Dockerfile push: true tags: | ${{ env.REGISTRY }}/${{ env.WEB_IMAGE }}:${{ env.WEB_TAG }} ${{ env.REGISTRY }}/${{ env.WEB_IMAGE }}:latest deploy: name: Deploy to remote host needs: - build-and-push-backend - build-and-push-web runs-on: ubuntu-latest steps: - name: Deploy via SSH uses: appleboy/ssh-action@v1.0.3 with: host: ${{ env.DEPLOY_HOST }} username: ${{ env.DEPLOY_USER }} key: ${{ secrets.SSH_PRIVATE_KEY }} script: | set -e echo "${{ secrets.CONTAINER_REGISTRY_PASSWORD }}" | docker login ${{ env.REGISTRY }} -u "${{ vars.CONTAINER_REGISTRY_USERNAME }}" --password-stdin export BACKEND_IMAGE="${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE }}:${{ env.BACKEND_TAG }}" export WEB_IMAGE="${{ env.REGISTRY }}/${{ env.WEB_IMAGE }}:${{ env.WEB_TAG }}" cd /opt/word-versioning docker compose -f docker-compose.prod.yml pull docker compose -f docker-compose.prod.yml up -d --remove-orphans docker image prune -f