name: Backend CI + Docker Deploy on: push: branches: - main - master tags: - "backend-*.*.*" paths: - "backend/**" - "docker-compose.yml" - ".forgejo/workflows/backend.yml" pull_request: paths: - "backend/**" - "docker-compose.yml" - ".forgejo/workflows/backend.yml" workflow_dispatch: permissions: contents: read env: REGISTRY: ${{ vars.CONTAINER_REGISTRY || 'ghcr.io' }} IMAGE_NAME: ${{ vars.IMAGE_NAMESPACE }}/word-versioning-backend IMAGE_TAG: ${{ github.ref_name || 'latest' }} DEPLOY_HOST: ${{ vars.SSH_DEPLOY_HOST }} DEPLOY_USER: ${{ vars.SSH_DEPLOY_USER }} jobs: build-and-test: name: Build and test backend runs-on: ubuntu-latest timeout-minutes: 30 env: DOTNET_CLI_TELEMETRY_OPTOUT: "1" steps: - name: Checkout repository uses: actions/checkout@v4 - name: Setup .NET SDK uses: actions/setup-dotnet@v4 with: dotnet-version: "8.0.x" - name: Restore solution run: dotnet restore backend/WordVersioning.slnx - name: Build solution run: dotnet build backend/WordVersioning.slnx --configuration Release --no-restore - name: Run tests run: dotnet test backend/WordVersioning.slnx --configuration Release --no-build --logger "trx;LogFileName=backend-tests.trx" docker-build: name: Build and push backend image needs: build-and-test if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' 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 container registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY }} username: ${{ vars.CONTAINER_REGISTRY_USERNAME }} password: ${{ secrets.CONTAINER_REGISTRY_PASSWORD }} - name: Build and push backend image uses: docker/build-push-action@v6 with: context: . file: ./backend/WordVersioning.Api/Dockerfile push: true tags: | ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest deploy: name: Deploy backend container needs: docker-build if: github.event_name == 'push' || github.event_name == 'workflow_dispatch' runs-on: ubuntu-latest steps: - name: Deploy over 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 docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.IMAGE_TAG }} docker compose -f /opt/word-versioning/docker-compose.prod.yml up -d api docker image prune -f