Build and push a Docker image #9
Workflow file for this run
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and push a Docker image | |
on: | |
push: | |
branches: | |
- main | |
- stage | |
- prod | |
workflow_dispatch: | |
inputs: | |
ref: | |
description: 'ref to be deployed (e.g. "refs/heads/main", "refs/heads/stage", "refs/heads/prod")' | |
type: string | |
required: true | |
default: refs/heads/main | |
env: | |
APP: birdbox | |
APPLICATION_REPOSITORY: mozmeao/birdbox | |
IMAGE: birdbox | |
GAR_LOCATION: us | |
GCP_PROJECT_ID: moz-fx-birdbox-prod | |
GAR_REPOSITORY: birdbox-prod | |
REF_ID: ${{ github.event.inputs.ref || github.ref }} | |
jobs: | |
push_image_to_gar: | |
name: Push Image to GAR | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
id-token: write | |
outputs: | |
deployment_env: ${{ steps.set-outputs.outputs.deployment_env }} | |
image_tag: ${{ steps.set-outputs.outputs.image_tag }} | |
steps: | |
- id: checkout-application-repo | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
fetch-tags: true | |
ref: ${{ env.REF_ID }} | |
- id: long-sha | |
run: echo "LONG_SHA=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT | |
- id: set-image-tag | |
name: Set Docker Image Tag and Deployment Environment | |
run: | | |
case "${{ env.REF_ID }}" in | |
"refs/heads/main") | |
echo "TAG=dev-$(git rev-parse HEAD)" >> "$GITHUB_ENV" | |
echo "DEPLOYMENT_ENV=dev" >> "$GITHUB_ENV" | |
;; | |
"refs/heads/stage") | |
echo "TAG=stage-$(git rev-parse HEAD)" >> "$GITHUB_ENV" | |
echo "DEPLOYMENT_ENV=stage" >> "$GITHUB_ENV" | |
;; | |
"refs/heads/prod") | |
echo "TAG=prod-$(git rev-parse HEAD)" >> "$GITHUB_ENV" | |
echo "DEPLOYMENT_ENV=prod" >> "$GITHUB_ENV" | |
;; | |
esac | |
- id: set-outputs | |
run: | | |
echo "deployment_env=${{ env.DEPLOYMENT_ENV }}" >> "$GITHUB_OUTPUT" | |
echo "image_tag=${{ env.TAG }}" >> "$GITHUB_OUTPUT" | |
- uses: docker/setup-buildx-action@v3 | |
- id: gcp-auth | |
uses: google-github-actions/auth@v2 | |
with: | |
token_format: 'access_token' | |
service_account: artifact-writer@${{ env.GCP_PROJECT_ID }}.iam.gserviceaccount.com | |
workload_identity_provider: ${{ vars.GCPV2_GITHUB_WORKLOAD_IDENTITY_PROVIDER }} | |
- uses: docker/login-action@v3 | |
name: Docker login | |
with: | |
registry: ${{ env.GAR_LOCATION }}-docker.pkg.dev | |
username: oauth2accesstoken | |
password: ${{ steps.gcp-auth.outputs.access_token }} | |
- id: build-and-push | |
uses: docker/build-push-action@v5 | |
with: | |
context: . | |
tags: ${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.GCP_PROJECT_ID }}/${{ env.GAR_REPOSITORY}}/${{ env.IMAGE}}:${{ env.TAG }} | |
push: true | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
build-args: GIT_SHA=${{ steps.long-sha.outputs.LONG_SHA }} |