feat(ci): run build and push workflow on labelled pull requests, exclude prs from forks #31
Workflow file for this run
This file contains hidden or 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: Go Demo -- Build and Push Container Image to GAR | |
| on: | |
| pull_request: | |
| types: [labeled, unlabeled, synchronize] | |
| push: | |
| branches: | |
| - main | |
| workflow_dispatch: {} | |
| env: | |
| IMAGE_NAME: go-demo | |
| GAR_LOCATION: us | |
| GCP_PROJECT_ID: moz-fx-cicd-demos-nonprod | |
| 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@v4 | |
| - uses: docker/setup-buildx-action@v3 | |
| - id: gcp-auth | |
| name: Google authentication | |
| 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 }} | |
| - name: Log in to the GAR container registry | |
| uses: docker/login-action@v3 | |
| 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: Build and push image | |
| uses: docker/build-push-action@v6 | |
| with: | |
| context: ./go-demo/ | |
| tags: | | |
| ${{ env.IMAGE_NAMESPACE }}/${{ env.IMAGE_NAME }}:${{ github.event.pull_request.head.sha || github.sha }} | |
| ${{ env.IMAGE_NAMESPACE }}/${{ env.IMAGE_NAME }}:latest | |
| ghcr.io/${{ github.repository }}/${{ env.IMAGE_NAME }}:${{ github.event.pull_request.head.sha || github.sha }} | |
| ghcr.io/${{ github.repository }}/${{ env.IMAGE_NAME }}:latest | |
| push: true | |
| cache-from: type=gha | |
| cache-to: type=gha,mode=max | |