-
Notifications
You must be signed in to change notification settings - Fork 731
173 lines (139 loc) · 5.49 KB
/
build-and-push.yml
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
name: Build and push a Docker image
on:
push:
branches:
- main
- dev
tags:
- '*'
workflow_dispatch:
inputs:
ref:
description: 'ref to be deployed (e.g. "refs/heads/main", "v1.0.0", "2c0472cf")'
type: string
required: true
default: refs/heads/dev
env:
APP: sumo
APPLICATION_REPOSITORY: mozilla/kitsune
IMAGE_NAME: kitsune
GAR_LOCATION: us
GCP_PROJECT_ID: moz-fx-sumo-prod
GAR_REPOSITORY: sumo-prod
REF_ID: ${{ github.ref }}
jobs:
build:
permissions:
contents: read
deployments: write
id-token: write
runs-on: ubuntu-latest
outputs:
deployment_env: ${{ env.DEPLOYMENT_ENV }}
deployment_realm: ${{ env.DEPLOYMENT_REALM }}
image_tag: ${{ env.TAG }}
steps:
- uses: actions/checkout@v4
- name: Create version.json
run: |
# create a version.json per
# https://github.com/mozilla-services/Dockerflow/blob/master/docs/version_object.md
printf '{"commit":"%s","version":"%s","source":"%s","build":"%s"}\n' \
"$GITHUB_SHA" \
"$GITHUB_REF_NAME" \
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY" \
"$GITHUB_SERVER_URL/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" > version.json
- id: checkout_application_repo
name: checkout application repo
uses: actions/checkout@v4
with:
fetch-depth: 0
fetch-tags: true
ref: ${{ env.REF_ID }}
- id: dev_image_tag
name: Set Docker dev image tag for updates of the DEV branch
if: github.ref == 'refs/heads/dev'
run: |
echo TAG="dev-$(git describe --tags --abbrev=7)" >> "$GITHUB_ENV"
# Updates to the main branch are deployed to stage.
echo DEPLOYMENT_ENV=dev >> "$GITHUB_ENV"
echo DEPLOYMENT_REALM=nonprod >> "$GITHUB_ENV"
- id: stage_image_tag
name: Set Docker stage image tag for updates of the main branch
if: github.ref == 'refs/heads/main'
run: |
echo TAG="$(git describe --tags --abbrev=7)" >> "$GITHUB_ENV"
# Updates to the main branch are deployed to stage.
echo DEPLOYMENT_ENV=stage >> "$GITHUB_ENV"
echo DEPLOYMENT_REALM=nonprod >> "$GITHUB_ENV"
- id: prod_image_tag
name: Set Docker prod image tag to the git tag for tagged builds
if: startsWith(github.ref, 'refs/tags/')
run: |
echo TAG="$(git describe --tags --abbrev=7)" >> "$GITHUB_ENV"
# Version tags are deployed to prod.
echo DEPLOYMENT_ENV=prod >> "$GITHUB_ENV"
echo DEPLOYMENT_REALM=prod >> "$GITHUB_ENV"
- uses: docker/setup-buildx-action@v3
- id: gcp_auth
name: GCP 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 }}
- id: docker_login
name: Log in to the container registry
uses: docker/login-action@v3
with:
registry: ${{ env.GAR_LOCATION }}-docker.pkg.dev
username: oauth2accesstoken
password: ${{ steps.gcp_auth.outputs.access_token }}
- id: build_and_push
name: Build and push image
uses: docker/build-push-action@v5
with:
context: .
tags: |
${{ env.GAR_LOCATION }}-docker.pkg.dev/${{ env.GCP_PROJECT_ID }}/${{ env.GAR_REPOSITORY }}/${{ env.IMAGE_NAME }}:${{ env.TAG }}
push: true
cache-from: type=gha
cache-to: type=gha,mode=max
upload-static-assets:
name: upload static assets
environment: ${{ needs.build.outputs.deployment_env }}
runs-on: ubuntu-latest
needs:
- build
permissions:
contents: read
id-token: write
steps:
- id: gcp_auth
name: gcp auth
uses: google-github-actions/auth@v2
with:
token_format: access_token
service_account: deploy-${{ needs.build.outputs.deployment_env }}@moz-fx-sumo-${{ needs.build.outputs.deployment_realm }}.iam.gserviceaccount.com
workload_identity_provider: ${{ vars.GCPV2_GITHUB_WORKLOAD_IDENTITY_PROVIDER }}
- id: docker_login
name: docker login
uses: docker/login-action@v3
with:
registry: ${{ env.GAR_LOCATION }}-docker.pkg.dev
username: oauth2accesstoken
password: ${{ steps.gcp_auth.outputs.access_token }}
- id: setup-gcloud
uses: google-github-actions/setup-gcloud@v2
- id: upload-assets
name: upload static assets
run: |-
TMP_DIR=static-upload
TMP_DIR_HASHED=static-upload-hashed
docker create --name tmp $GAR_LOCATION-docker.pkg.dev/$GCP_PROJECT_ID/$GAR_REPOSITORY/$IMAGE_NAME:${{ needs.build.outputs.image_tag }}
mkdir -p ./$TMP_DIR ./$TMP_DIR_HASHED
docker cp tmp:/app/static/. ./$TMP_DIR/
find $TMP_DIR -maxdepth 1 -type f -regextype sed -regex ".*\.[0-9a-f]\{16\}\..*" -exec mv -t $TMP_DIR_HASHED {} +
gsutil -m rsync -r ./$TMP_DIR_HASHED/ gs://$APP-${{ needs.build.outputs.deployment_realm }}-${{ needs.build.outputs.deployment_env }}-assets-bucket/static/
gsutil -m rsync -r ./$TMP_DIR/ gs://$APP-${{ needs.build.outputs.deployment_realm }}-${{ needs.build.outputs.deployment_env }}-assets-bucket/static/
docker rm tmp