Skip to content

feat(ci): run build and push workflow on labelled pull requests, exclude prs from forks #48

feat(ci): run build and push workflow on labelled pull requests, exclude prs from forks

feat(ci): run build and push workflow on labelled pull requests, exclude prs from forks #48

name: Go Demo -- Build and Push Container Image to GAR
on:
pull_request:
types: [labeled, unlabeled, synchronize]
push:
branches:
- main
tags:
- v[0-9]+.[0-9]+.[0-9]+
workflow_dispatch: {}
env:
IMAGE_NAME: go-demo
GAR_LOCATION: us
GCP_PROJECT_ID: moz-fx-cicd-demos-nonprod
GAR_IMAGE_NAMESPACE: us-docker.pkg.dev/moz-fx-cicd-demos-nonprod/cicd-demos-nonprod
jobs:
build-and-push:
if: >
github.event_name == 'push' ||
github.event_name == 'workflow_dispatch' ||
(
github.event_name == 'pull_request' &&
contains(github.event.pull_request.labels.*.name, 'preview') &&
github.event.pull_request.head.repo.full_name == github.repository
)
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
id-token: write
steps:
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683
with:
fetch-tags: true
- uses: docker/setup-buildx-action@b5ca514318bd6ebac0fb2aedd5d36ec1b5c232a2
- id: gcp-auth
name: Google authentication
uses: google-github-actions/auth@ba79af03959ebeac9769e648f473a284504d9193
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 }}
- name: Log in to the GAR container registry
uses: docker/login-action@74a5d142397b4f367a81961eba4e8cd7edddf772
with:
registry: ${{ env.GAR_LOCATION }}-docker.pkg.dev
username: oauth2accesstoken
password: ${{ steps.gcp-auth.outputs.access_token }}
- name: Log in to the GitHub Container registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Run the release script to build version.json file
working-directory: ./go-demo
run: ./release.sh
- name: Generate MozCloud Tag
id: mozcloud-tag
shell: bash
run: |
if [[ "${{ github.ref_type }}" == "tag" ]]; then
tag="${{ github.ref_name }}"
else
tag="$(git rev-parse --short=10 HEAD)"
fi
# append metadata if present
if [[ -n "${{ env.METADATA }}" ]]; then
tag="${tag}--${{ env.METADATA }}"
fi
echo "Setting IMAGE_TAG=${tag} as output"
echo "IMAGE_TAG=${tag}" >> "$GITHUB_OUTPUT"
- name: Docker meta
id: meta
uses: docker/metadata-action@902fa8ec7d6ecbf8d84d538b9b233a880e428804
with:
images: |
${{ env.GAR_IMAGE_NAMESPACE }}/${{ env.IMAGE_NAME }}
ghcr.io/${{ github.repository }}/${{ env.IMAGE_NAME }}
tags: |
type=raw,value=${{ steps.mozcloud-tag.outputs.IMAGE_TAG }}
type=raw,value=latest
type=raw,value=${{ github.event.pull_request.head.sha || github.sha }}
- name: Build and push image
uses: docker/build-push-action@263435318d21b8e681c14492fe198d362a7d2c83
with:
context: ./go-demo/
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
annotations: ${{ steps.meta.outputs.annotations }}
push: true
cache-from: type=gha
cache-to: type=gha,mode=max