name: Build and Deploy Backend on: push: branches: - dev paths: - "Backend/**" - ".forgejo/workflows/deploy-backend.yaml" workflow_dispatch: env: REGISTRY: ${{ vars.CONTAINER_REGISTRY }} REGISTRY_USERNAME: ${{ vars.CONTAINER_REGISTRY_USERNAME }} REGISTRY_PASSWORD: ${{ secrets.CONTAINER_REGISTRY_PASSWORD }} # Docker Image BACKEND_IMAGE_NAME: chamomind/elearning-backend IMAGE_TAG: ${{ github.sha }} # Notifications DISCORD_WEBHOOK_URL: ${{ vars.DISCORD_WEBHOOK_URL }} jobs: build: name: Build Backend Docker Image runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v4 - name: Log in to Container Registry uses: docker/login-action@v3 with: registry: ${{ env.REGISTRY_URL }} username: ${{ env.REGISTRY_USERNAME }} password: ${{ env.REGISTRY_PASSWORD }} - name: Build Docker image (no buildx) run: | # Build using standard docker build (not buildx) cd ${{ github.workspace }} docker build \ --no-cache \ --pull \ --file ./Backend/Dockerfile \ --tag ${{ env.REGISTRY_URL }}/${{ env.BACKEND_IMAGE_NAME }}:${{ env.IMAGE_TAG }} \ --tag ${{ env.REGISTRY_URL }}/${{ env.BACKEND_IMAGE_NAME }}:latest \ . echo "✅ Build successful!" docker images | grep ${{ env.BACKEND_IMAGE_NAME }} - name: Push Docker image run: | docker push ${{ env.REGISTRY_URL }}/${{ env.BACKEND_IMAGE_NAME }}:${{ env.IMAGE_TAG }} docker push ${{ env.REGISTRY_URL }}/${{ env.BACKEND_IMAGE_NAME }}:latest echo "✅ Image pushed to registry" - name: Test image can run run: | # Quick test that image can start docker run --rm ${{ env.REGISTRY_URL }}/${{ env.BACKEND_IMAGE_NAME }}:${{ env.IMAGE_TAG }} node --version # build: # name: Build Backend Docker Image # runs-on: ubuntu-latest # steps: # - name: Checkout code # uses: actions/checkout@v4 # - name: Login to Docker Registry # uses: docker/login-action@v3 # with: # registry: ${{ env.REGISTRY }} # username: ${{ env.REGISTRY_USERNAME }} # password: ${{ env.REGISTRY_PASSWORD }} # - name: Set up Docker Buildx # uses: docker/setup-buildx-action@v3 # with: # config-inline: | # [registry."${{ env.REGISTRY }}"] # http = true # insecure = true # - name: Extract metadata # id: meta # uses: docker/metadata-action@v5 # with: # images: ${{ env.REGISTRY }}/${{ env.BACKEND_IMAGE_NAME }} # tags: | # type=ref,event=branch # type=sha,prefix= # type=raw,value=latest,enable=${{ github.ref == 'refs/heads/dev' }} # - name: Build and push Docker image # uses: docker/build-push-action@v5 # with: # context: ./Backend # file: ./Backend/Dockerfile # push: true # tags: ${{ steps.meta.outputs.tags }} # labels: ${{ steps.meta.outputs.labels }} # cache-from: type=gha # cache-to: type=gha,mode=max deploy: name: Deploy E-learning Backend to Dev Server runs-on: ubuntu-latest needs: build steps: - name: Deploy backend on server uses: appleboy/ssh-action@v1.2.1 with: host: ${{ vars.SSH_DEPLOY_HOST }} port: ${{ vars.SSH_DEPLOY_PORT }} username: ${{ secrets.SSH_DEPLOY_USER }} password: ${{ secrets.SSH_DEPLOY_PASSWORD }} script: | cd ~/repo chmod +x ./replace-env.sh ./deploy.sh ./replace-env.sh BACKEND_TAG "${{ env.IMAGE_TAG }}" ./deploy.sh elearning-backend notify: name: Notify Deployment Status runs-on: ubuntu-latest needs: [build, deploy] if: always() steps: - name: Send Discord notification - Success if: ${{ needs.deploy.result == 'success' }} run: | curl -H "Content-Type: application/json" \ -d '{ "embeds": [{ "title": "✅ E-learning Backend Deployment Successful", "description": "E-learning Backend deployed to dev server", "color": 3066993, "fields": [ { "name": "Environment", "value": "Development", "inline": true }, { "name": "Branch", "value": "${{ github.ref_name }}", "inline": true }, { "name": "Commit", "value": "`${{ github.sha }}`", "inline": false }, { "name": "Image Tag", "value": "`${{ env.IMAGE_TAG }}`", "inline": true }, { "name": "Web URL", "value": "${{ env.NEXT_PUBLIC_APP_URL }}", "inline": false }, { "name": "Deployed By", "value": "${{ github.actor }}", "inline": true } ], "footer": { "text": "CMP CD Pipeline" }, "timestamp": "'"$(date -u +%Y-%m-%dT%H:%M:%S.000Z)"'" }] }' \ ${{ env.DISCORD_WEBHOOK_URL }} - name: Send Discord notification - Failure if: ${{ needs.deploy.result == 'failure' || needs.build.result == 'failure' }} run: | curl -H "Content-Type: application/json" \ -d '{ "embeds": [{ "title": "❌ E-learning Backend Deployment Failed", "description": "Failed to deploy E-learning Backend to dev server", "color": 15158332, "fields": [ { "name": "Environment", "value": "Development", "inline": true }, { "name": "Branch", "value": "${{ github.ref_name }}", "inline": true }, { "name": "Commit", "value": "`${{ github.sha }}`", "inline": false }, { "name": "Failed Job", "value": "${{ needs.build.result == 'failure' && 'Build' || 'Deploy' }}", "inline": true }, { "name": "Deployed By", "value": "${{ github.actor }}", "inline": true } ], "footer": { "text": "CMP CD Pipeline" }, "timestamp": "'"$(date -u +%Y-%m-%dT%H:%M:%S.000Z)"'" }] }' \ ${{ env.DISCORD_WEBHOOK_URL }} exit 1