|
| 1 | +name: "Build & Deploy (GitOps)" |
| 2 | + |
| 3 | +on: |
| 4 | + # This is a TEMPLATE — copy it into your own app repo and set your trigger. |
| 5 | + # Example triggers you can use: |
| 6 | + # push: |
| 7 | + # branches: ["main"] |
| 8 | + # workflow_dispatch: # Manual trigger from the GitHub Actions UI |
| 9 | + workflow_dispatch: |
| 10 | + |
| 11 | +# ── CONFIGURE THESE ───────────────────────────────────────────────────────── |
| 12 | +env: |
| 13 | + # 1. Container registry (ghcr.io = GitHub, docker.io = Docker Hub) |
| 14 | + REGISTRY: ghcr.io |
| 15 | + |
| 16 | + # 2. Leave this as-is — it automatically uses your repo name as the image name |
| 17 | + IMAGE_NAME: ${{ github.repository }} |
| 18 | + |
| 19 | + # 3. The GitOps repository that holds your Kubernetes manifests |
| 20 | + # Format: "github-username/gitops-repo-name" |
| 21 | + GITOPS_REPO: "CHANGE_ME/my-app-gitops" |
| 22 | +# ───────────────────────────────────────────────────────────────────────────── |
| 23 | + |
| 24 | +jobs: |
| 25 | + # ── JOB 1: Build the Docker image and push it to the registry ────────────── |
| 26 | + build-and-push: |
| 27 | + runs-on: ubuntu-latest |
| 28 | + permissions: |
| 29 | + contents: read # Read this repo's code |
| 30 | + packages: write # Push image to GHCR |
| 31 | + |
| 32 | + steps: |
| 33 | + - name: "Checkout app code" |
| 34 | + uses: actions/checkout@v4 |
| 35 | + |
| 36 | + - name: "Log in to registry" |
| 37 | + uses: docker/login-action@v3 |
| 38 | + with: |
| 39 | + registry: ${{ env.REGISTRY }} |
| 40 | + username: ${{ github.actor }} |
| 41 | + password: ${{ secrets.GITHUB_TOKEN }} # Automatically provided by GitHub — no setup needed |
| 42 | + |
| 43 | + - name: "Generate image tags" |
| 44 | + id: meta |
| 45 | + uses: docker/metadata-action@v5 |
| 46 | + with: |
| 47 | + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} |
| 48 | + tags: | |
| 49 | + type=sha,prefix=sha-,format=short # e.g. sha-a1b2c3d |
| 50 | + type=raw,value=latest,enable=${{ github.ref == 'refs/heads/main' }} |
| 51 | +
|
| 52 | + - name: "Set up Docker Buildx" |
| 53 | + uses: docker/setup-buildx-action@v3 |
| 54 | + |
| 55 | + - name: "Build & push image" |
| 56 | + id: push |
| 57 | + uses: docker/build-push-action@v5 |
| 58 | + with: |
| 59 | + context: . |
| 60 | + push: true |
| 61 | + tags: ${{ steps.meta.outputs.tags }} |
| 62 | + cache-from: type=gha |
| 63 | + cache-to: type=gha,mode=max |
| 64 | + |
| 65 | + # ── JOB 2: Update the manifest in the GitOps repo → ArgoCD will deploy ───── |
| 66 | + update-gitops-manifest: |
| 67 | + needs: build-and-push |
| 68 | + runs-on: ubuntu-latest |
| 69 | + |
| 70 | + steps: |
| 71 | + # Checkout the GITOPS repo (not this one) |
| 72 | + # Requires secret: GITOPS_REPO_PAT |
| 73 | + - name: "Checkout GitOps repo" |
| 74 | + uses: actions/checkout@v4 |
| 75 | + with: |
| 76 | + repository: ${{ env.GITOPS_REPO }} |
| 77 | + token: ${{ secrets.GITOPS_REPO_PAT }} |
| 78 | + path: gitops-repo |
| 79 | + |
| 80 | + - name: "Update image tag in deployment.yaml" |
| 81 | + run: | |
| 82 | + # Build the new image reference (lowercase, with SHA tag) |
| 83 | + NEW_IMAGE="${{ env.REGISTRY }}/${{ github.repository_owner }}/$(basename ${{ github.repository }}):sha-$(echo ${{ github.sha }} | cut -c1-7)" |
| 84 | + NEW_IMAGE=$(echo $NEW_IMAGE | tr '[:upper:]' '[:lower:]') |
| 85 | +
|
| 86 | + echo "New image: $NEW_IMAGE" |
| 87 | +
|
| 88 | + # Replace the "image:" line in the manifest |
| 89 | + sed -i "s|image: .*|image: $NEW_IMAGE|g" gitops-repo/deployment.yaml |
| 90 | +
|
| 91 | + - name: "Commit & push to GitOps repo" |
| 92 | + run: | |
| 93 | + cd gitops-repo |
| 94 | + git config user.name "github-actions[bot]" |
| 95 | + git config user.email "github-actions[bot]@users.noreply.github.com" |
| 96 | + git add deployment.yaml |
| 97 | + git commit -m "deploy: update image → sha-${{ github.sha }}" || echo "Nothing to commit" |
| 98 | + git push |
0 commit comments