Skip to content

trying new ci fix

trying new ci fix #24

Workflow file for this run

name: Build and Deploy
on:
push:
branches: ["main"]
workflow_dispatch: # Allow manual triggering
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
PORTAINER_WEBHOOK_URL: ${{ secrets.PORTAINER_WEBHOOK_URL }}
jobs:
check-changes:
name: Check What Changed
runs-on: [self-hosted, home]
outputs:
backend-changed: ${{ steps.changes.outputs.backend }}
frontend-changed: ${{ steps.changes.outputs.frontend }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 2 # Need at least 2 commits to compare
- name: Check for changes
id: changes
run: |
# Check if this is the first commit (no previous commit to compare)
if git rev-parse HEAD~1 >/dev/null 2>&1; then
# Compare with previous commit
BACKEND_CHANGED=$(git diff --name-only HEAD~1 HEAD | grep -E '^(cmd/|internal/|pkg/|go\.|configs/|deployments/|\.github/workflows/deploy\.yml)' || echo '')
FRONTEND_CHANGED=$(git diff --name-only HEAD~1 HEAD | grep -E '^frontend/' || echo '')
else
# First commit - build everything
BACKEND_CHANGED="initial"
FRONTEND_CHANGED="initial"
fi
# Set outputs
if [ -n "$BACKEND_CHANGED" ]; then
echo "backend=true" >> $GITHUB_OUTPUT
echo "πŸ” Backend changes detected: $BACKEND_CHANGED"
else
echo "backend=false" >> $GITHUB_OUTPUT
echo "βœ… No backend changes detected"
fi
if [ -n "$FRONTEND_CHANGED" ]; then
echo "frontend=true" >> $GITHUB_OUTPUT
echo "πŸ” Frontend changes detected: $FRONTEND_CHANGED"
else
echo "frontend=false" >> $GITHUB_OUTPUT
echo "βœ… No frontend changes detected"
fi
build-and-push-backend:
name: Build and Push Backend
runs-on: [self-hosted, home]
needs: check-changes
if: needs.check-changes.outputs.backend-changed == 'true'
permissions:
contents: read
packages: write
steps:
- name: Checkout code
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: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=sha,prefix={{branch}}-
type=raw,value=latest,enable={{is_default_branch}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: ./deployments/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
platforms: linux/amd64,linux/arm64
deploy-frontend-cloudflare:
name: Deploy Frontend to Cloudflare Workers
runs-on: ubuntu-latest
needs: check-changes
if: needs.check-changes.outputs.frontend-changed == 'true'
defaults:
run:
working-directory: ./frontend
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up Node.js
uses: actions/setup-node@v4
with:
node-version: '24'
cache: 'npm'
cache-dependency-path: './frontend/package-lock.json'
- name: Install dependencies
run: npm ci
- name: Install Wrangler
run: npm install -g wrangler
- name: Build for Cloudflare Workers
run: npm run build:cloudflare
env:
NEXT_PUBLIC_API_URL: ${{ secrets.BACKEND_API_URL }}
- name: Deploy to Cloudflare Workers
run: wrangler deploy --env="production"
env:
CLOUDFLARE_API_TOKEN: ${{ secrets.CLOUDFLARE_API_TOKEN }}
CLOUDFLARE_ACCOUNT_ID: ${{ secrets.CLOUDFLARE_ACCOUNT_ID }}
deploy-backend-portainer:
name: Deploy Backend via Portainer
runs-on: [self-hosted, home]
needs: [check-changes, build-and-push-backend]
if: always() && github.ref == 'refs/heads/main' &&
needs.check-changes.outputs.backend-changed == 'true' &&
(needs.build-and-push-backend.result == 'success' || needs.build-and-push-backend.result == 'skipped')
steps:
- name: Verify Portainer Connection
run: |
echo "πŸ” Verifying connection to Portainer..."
if curl -f -s --connect-timeout 10 https://portainer.sankalpnarula.com/api/status > /dev/null; then
echo "βœ… Successfully connected to Portainer"
else
echo "❌ Failed to connect to Portainer"
exit 1
fi
- name: Trigger Portainer Stack Redeployment
env:
PORTAINER_WEBHOOK_URL: ${{ secrets.PORTAINER_WEBHOOK_URL }}
run: |
echo "πŸš€ Starting backend deployment..."
RESPONSE=$(curl -s -w "%{http_code}" -X POST \
-H "Content-Type: application/json" \
-d '{"pullImage": true}' \
"$PORTAINER_WEBHOOK_URL")
HTTP_CODE="${RESPONSE: -3}"
RESPONSE_BODY="${RESPONSE%???}"
case $HTTP_CODE in
200|204)
echo "βœ… Backend redeployment triggered successfully!"
;;
*)
echo "❌ Backend deployment failed with HTTP $HTTP_CODE"
echo "πŸ“ Response: $RESPONSE_BODY"
exit 1
;;
esac
deployment-summary:
name: Deployment Summary
runs-on: ubuntu-latest
needs: [check-changes, deploy-frontend-cloudflare, deploy-backend-portainer]
if: always() && github.ref == 'refs/heads/main'
steps:
- name: Create Summary
run: |
echo "# πŸš€ Deployment Summary" >> $GITHUB_STEP_SUMMARY
echo "" >> $GITHUB_STEP_SUMMARY
# Frontend status
if [ "${{ needs.deploy-frontend-cloudflare.result }}" = "success" ]; then
echo "## βœ… Frontend (Cloudflare Workers)" >> $GITHUB_STEP_SUMMARY
echo "- Status: **Deployed successfully**" >> $GITHUB_STEP_SUMMARY
echo "- Platform: Cloudflare Workers" >> $GITHUB_STEP_SUMMARY
echo "- Domain: https://ocpp.sankalpnarula.com" >> $GITHUB_STEP_SUMMARY
elif [ "${{ needs.check-changes.outputs.frontend-changed }}" = "true" ]; then
echo "## ❌ Frontend (Cloudflare Workers)" >> $GITHUB_STEP_SUMMARY
echo "- Status: **Deployment failed**" >> $GITHUB_STEP_SUMMARY
else
echo "## ⏭️ Frontend (Cloudflare Workers)" >> $GITHUB_STEP_SUMMARY
echo "- Status: **No changes, skipped**" >> $GITHUB_STEP_SUMMARY
fi
echo "" >> $GITHUB_STEP_SUMMARY
# Backend status
if [ "${{ needs.deploy-backend-portainer.result }}" = "success" ]; then
echo "## βœ… Backend (Portainer)" >> $GITHUB_STEP_SUMMARY
echo "- Status: **Deployed successfully**" >> $GITHUB_STEP_SUMMARY
echo "- Platform: Portainer + Docker" >> $GITHUB_STEP_SUMMARY
elif [ "${{ needs.check-changes.outputs.backend-changed }}" = "true" ]; then
echo "## ❌ Backend (Portainer)" >> $GITHUB_STEP_SUMMARY
echo "- Status: **Deployment failed**" >> $GITHUB_STEP_SUMMARY
else
echo "## ⏭️ Backend (Portainer)" >> $GITHUB_STEP_SUMMARY
echo "- Status: **No changes, skipped**" >> $GITHUB_STEP_SUMMARY
fi