|
| 1 | +name: Build & Deploy |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + tags: ['v*'] |
| 7 | + workflow_dispatch: |
| 8 | + |
| 9 | +concurrency: |
| 10 | + group: deploy-${{ github.ref }} |
| 11 | + cancel-in-progress: true |
| 12 | + |
| 13 | +env: |
| 14 | + IMAGE: ghcr.io/${{ github.repository }} |
| 15 | + |
| 16 | +jobs: |
| 17 | + build-and-push: |
| 18 | + name: Build & push image |
| 19 | + runs-on: ubuntu-latest |
| 20 | + permissions: |
| 21 | + contents: read |
| 22 | + packages: write |
| 23 | + outputs: |
| 24 | + digest: ${{ steps.build.outputs.digest }} |
| 25 | + steps: |
| 26 | + - uses: actions/checkout@v4 |
| 27 | + |
| 28 | + - name: Set up Docker Buildx |
| 29 | + uses: docker/setup-buildx-action@v3 |
| 30 | + |
| 31 | + - name: Log in to GHCR |
| 32 | + uses: docker/login-action@v3 |
| 33 | + with: |
| 34 | + registry: ghcr.io |
| 35 | + username: ${{ github.actor }} |
| 36 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 37 | + |
| 38 | + - name: Extract image metadata |
| 39 | + id: meta |
| 40 | + uses: docker/metadata-action@v5 |
| 41 | + with: |
| 42 | + images: ${{ env.IMAGE }} |
| 43 | + tags: | |
| 44 | + type=raw,value=latest,enable={{is_default_branch}} |
| 45 | + type=sha,format=short |
| 46 | + type=semver,pattern={{version}} |
| 47 | +
|
| 48 | + - name: Build and push |
| 49 | + id: build |
| 50 | + uses: docker/build-push-action@v6 |
| 51 | + with: |
| 52 | + context: . |
| 53 | + push: true |
| 54 | + tags: ${{ steps.meta.outputs.tags }} |
| 55 | + labels: ${{ steps.meta.outputs.labels }} |
| 56 | + cache-from: type=gha |
| 57 | + cache-to: type=gha,mode=max |
| 58 | + |
| 59 | + deploy: |
| 60 | + name: Redeploy on Portainer |
| 61 | + needs: build-and-push |
| 62 | + if: github.ref == 'refs/heads/main' |
| 63 | + runs-on: ubuntu-latest |
| 64 | + steps: |
| 65 | + - name: Trigger Portainer stack redeploy (pull latest image) |
| 66 | + env: |
| 67 | + PORTAINER_URL: ${{ secrets.PORTAINER_URL }} |
| 68 | + PORTAINER_API_KEY: ${{ secrets.PORTAINER_API_KEY }} |
| 69 | + STACK_ID: ${{ secrets.PORTAINER_STACK_ID }} |
| 70 | + ENDPOINT_ID: ${{ secrets.PORTAINER_ENDPOINT_ID }} |
| 71 | + run: | |
| 72 | + set -euo pipefail |
| 73 | +
|
| 74 | + # Re-deploy the existing stack with its current file + env, forcing an |
| 75 | + # image pull so the freshly pushed tag is rolled out. Reusing the |
| 76 | + # stored content means no secrets need to be re-supplied here. |
| 77 | + content=$(curl -fsS -H "X-API-Key: $PORTAINER_API_KEY" \ |
| 78 | + "$PORTAINER_URL/api/stacks/$STACK_ID/file" | jq -r '.StackFileContent') |
| 79 | + env_json=$(curl -fsS -H "X-API-Key: $PORTAINER_API_KEY" \ |
| 80 | + "$PORTAINER_URL/api/stacks/$STACK_ID" | jq -c '.Env // []') |
| 81 | +
|
| 82 | + jq -n --arg content "$content" --argjson env "$env_json" \ |
| 83 | + '{StackFileContent: $content, Env: $env, Prune: true, PullImage: true}' \ |
| 84 | + > payload.json |
| 85 | +
|
| 86 | + curl -fsS -X PUT \ |
| 87 | + -H "X-API-Key: $PORTAINER_API_KEY" \ |
| 88 | + -H "Content-Type: application/json" \ |
| 89 | + "$PORTAINER_URL/api/stacks/$STACK_ID?endpointId=$ENDPOINT_ID" \ |
| 90 | + --data @payload.json > /dev/null |
| 91 | +
|
| 92 | + echo "Portainer stack $STACK_ID redeployed." |
0 commit comments