Repository navigation
100 lines (83 loc) · 3.03 KB
/
Copy path_build.yml
File metadata and controls
100 lines (83 loc) · 3.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
name: Build (reusable)
description: Builds and pushes a Docker image.
on:
workflow_call:
inputs:
environment:
type: string
required: true
ref:
description: "Branch to build (default: main)"
type: string
tag:
description: "Additional tag for the Docker image"
type: string
required: false
env:
IMAGE: mdn-observatory
REGISTRY: us-docker.pkg.dev
concurrency:
group: build-${{ inputs.environment }}
jobs:
docker-build-push:
environment: build
runs-on: ubuntu-latest
permissions:
# Read/write GHA cache.
actions: write
# Checkout.
contents: read
# Authenticate with GCP.
id-token: write
steps:
- name: Validate tag format
if: inputs.tag
env:
TAG: ${{ inputs.tag }}
run: |
if [[ ! "$TAG" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "❌ Invalid tag: $TAG does not match format vX.Y.Z (e.g., v1.2.3)"
exit 1
fi
echo "✅ Valid tag: $TAG"
- name: Checkout
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
ref: ${{ inputs.ref || github.event.repository.default_branch }}
persist-credentials: false
- name: Docker setup
uses: docker/setup-buildx-action@f87e5991a6d7451dcb8d9637bfbc97413f497069 # v4.4.1
- name: GCP auth
id: gcp-auth
uses: google-github-actions/auth@7c6bc770dae815cd3e89ee6cdf493a5fab2cc093 # v3.0.0
with:
token_format: access_token
service_account: artifact-writer@${{ secrets.GCP_PROJECT_NAME }}.iam.gserviceaccount.com
workload_identity_provider: projects/${{ secrets.WIP_PROJECT_ID }}/locations/global/workloadIdentityPools/github-actions/providers/github-actions
- name: Gather GIT_SHA and DOCKER_TAGS
id: gather-build-metadata
run: |
IMAGE_PREFIX="${{ env.REGISTRY }}/${{ secrets.GCP_PROJECT_NAME }}/${{ secrets.GAR_REPOSITORY }}/${{ env.IMAGE }}"
GIT_SHA="$(git rev-parse HEAD)"
DOCKER_TAGS="$IMAGE_PREFIX:$GIT_SHA"
if [ -n "${{ inputs.tag }}" ]; then
DOCKER_TAGS="$DOCKER_TAGS,$IMAGE_PREFIX:${{ inputs.tag }}"
fi
echo "GIT_SHA=$GIT_SHA" >> "$GITHUB_OUTPUT"
echo "DOCKER_TAGS=$DOCKER_TAGS" >> "$GITHUB_OUTPUT"
- name: Docker login
uses: docker/login-action@8aa5831fb8d48d8f76594c7ddde61ae58019f550 # v4.8.0
with:
registry: ${{ env.REGISTRY }}
username: oauth2accesstoken
password: ${{ steps.gcp-auth.outputs.access_token }}
- name: Build and push
uses: docker/build-push-action@c3c9e263c25d99ce0380d002d59b67737d91b0dc # v7.4.0
with:
context: .
build-args: |
GIT_SHA=${{ steps.gather-build-metadata.outputs.GIT_SHA }}
tags: ${{ steps.gather-build-metadata.outputs.DOCKER_TAGS }}
push: true
cache-from: type=gha
cache-to: type=gha,mode=max