|
| 1 | +name: Build Docker image and publish to GAR (Dev) |
| 2 | + |
| 3 | +# Actions in this repository could be streamlined with |
| 4 | +# reusable workflows (significant refactor) |
| 5 | +permissions: {} |
| 6 | + |
| 7 | +on: |
| 8 | + push: |
| 9 | + branches: |
| 10 | + - dev |
| 11 | + |
| 12 | +jobs: |
| 13 | + build_and_push_to_gar: |
| 14 | + # Define permissions at the job level |
| 15 | + permissions: |
| 16 | + contents: "read" # Needed for checkout |
| 17 | + id-token: "write" # Needed for GCP auth |
| 18 | + packages: "none" # Explicitly disable package permissions |
| 19 | + name: Build and Push Docker image to GAR |
| 20 | + runs-on: ubuntu-latest |
| 21 | + environment: build |
| 22 | + env: |
| 23 | + GAR_IMAGE_BASE: ${{ vars.GAR_REPO }}/${{ github.event.repository.name }} # Base name for GAR image |
| 24 | + GAR_REGISTRY: us-docker.pkg.dev |
| 25 | + steps: |
| 26 | + - name: Check out the repo |
| 27 | + uses: actions/checkout@v6 |
| 28 | + with: |
| 29 | + persist-credentials: false |
| 30 | + |
| 31 | + - name: Authenticate to Google Cloud |
| 32 | + id: gcp-auth |
| 33 | + uses: google-github-actions/auth@v3 |
| 34 | + with: |
| 35 | + token_format: access_token |
| 36 | + workload_identity_provider: ${{ vars.GCPV2_GITHUB_WORKLOAD_IDENTITY_PROVIDER }} |
| 37 | + service_account: ${{ vars.GCP_GAR_SERVICE_ACCOUNT }} |
| 38 | + |
| 39 | + - name: Login to Artifact Registry |
| 40 | + id: gar-login |
| 41 | + uses: docker/login-action@v3 |
| 42 | + with: |
| 43 | + registry: ${{ env.GAR_REGISTRY }} |
| 44 | + username: oauth2accesstoken |
| 45 | + password: ${{ steps.gcp-auth.outputs.access_token }} |
| 46 | + |
| 47 | + - name: Extract metadata (tags, labels) for Docker |
| 48 | + id: meta |
| 49 | + uses: docker/metadata-action@v5 |
| 50 | + with: |
| 51 | + # Only generate the image name for GAR |
| 52 | + images: ${{ env.GAR_IMAGE_BASE }} |
| 53 | + tags: | |
| 54 | + # Generate tag based on short commit SHA |
| 55 | + type=sha,format=short,prefix=dev- |
| 56 | +
|
| 57 | + - name: Create version.json |
| 58 | + run: | |
| 59 | + # Use full sha here for version.json content |
| 60 | + echo "{\"commit\":\"$GITHUB_SHA\",\"version\":\"$GITHUB_REF_NAME\",\"source\":\"https://github.com/$GITHUB_REPOSITORY\",\"build\":\"$GITHUB_RUN_ID\"}" > version.json |
| 61 | +
|
| 62 | + - name: Set up QEMU |
| 63 | + uses: docker/setup-qemu-action@v3 |
| 64 | + |
| 65 | + - name: Set up Docker Buildx |
| 66 | + id: buildx |
| 67 | + uses: docker/setup-buildx-action@v3 |
| 68 | + |
| 69 | + - name: Build and push Docker image to GAR |
| 70 | + id: build-and-push |
| 71 | + env: |
| 72 | + TAGS: ${{ steps.meta.outputs.tags }} |
| 73 | + uses: docker/build-push-action@v6 |
| 74 | + with: |
| 75 | + context: . |
| 76 | + # Push is true to push to GAR after build |
| 77 | + push: true |
| 78 | + # Tags generated by the metadata action (only GAR tag) |
| 79 | + tags: ${{ env.TAGS }} |
| 80 | + # Pass build arguments |
| 81 | + build-args: | |
| 82 | + SENTRY_RELEASE=${{ github.sha }} |
| 83 | + NEXT_PUBLIC_SENTRY_DSN=${{ secrets.SENTRY_DSN }} |
| 84 | + SENTRY_DSN=${{ secrets.SENTRY_DSN }} |
| 85 | + # Pass secrets securely to the build |
| 86 | + secrets: | |
| 87 | + SENTRY_AUTH_TOKEN=${{ secrets.SENTRY_AUTH_TOKEN }} |
| 88 | + # Enable build cache for faster builds (optional but recommended) |
| 89 | + cache-from: type=gha |
| 90 | + cache-to: type=gha,mode=max |
| 91 | + |
| 92 | + - name: Print Image URI |
| 93 | + env: |
| 94 | + TAGS: ${{ steps.meta.outputs.tags }} |
| 95 | + run: | |
| 96 | + echo "Pushed GAR image: $TAGS" |
0 commit comments