-
Notifications
You must be signed in to change notification settings - Fork 2
/
google-cloudrun-docker.yml
206 lines (175 loc) · 7.65 KB
/
google-cloudrun-docker.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
name: Build and Deploy to Cloud Run
on:
release:
types: [published]
jobs:
build-push:
# Add 'id-token' with the intended permissions for workload identity federation
permissions:
contents: 'read'
id-token: 'write'
outputs:
git-tag: ${{ steps.tag.outputs.tag }}
runs-on: ubuntu-latest
environment: dev
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
# Authenticate to Google Cloud
- name: Google Auth
id: auth
uses: 'google-github-actions/auth@v0'
with:
token_format: 'access_token'
workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # e.g. - projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider
service_account: '${{ secrets.WIF_SERVICE_ACCOUNT }}' # e.g. - [email protected]
# NOTE: Alternative option - authentication via credentials json
# - name: Google Auth
# id: auth
# uses: 'google-github-actions/auth@v0'
# with:
# credentials_json: '${{ secrets.GCP_CREDENTIALS }}''
# BEGIN - Docker auth and build (NOTE: If you already have a container image, these Docker steps can be omitted)
# Authenticate Docker to Google Cloud Artifact Registry
- name: Docker Auth
id: docker-auth
uses: 'docker/login-action@v1'
with:
username: 'oauth2accesstoken'
password: '${{ steps.auth.outputs.access_token }}'
registry: '${{ vars.GAR_LOCATION }}-docker.pkg.dev'
- name: Get Tag
id: tag
run: echo "tag=$(git describe --tags --abbrev=0)" >> $GITHUB_OUTPUT
- name: Build and Push Container
run: |-
echo ${{ steps.tag.outputs.tag }}
echo "${{ steps.tag.outputs.tag }}"
docker build -t "${{ vars.GAR_LOCATION }}-docker.pkg.dev/${{ vars.PROJECT_ID }}/${{ vars.SERVICE }}/${{ vars.SERVICE }}:${{ steps.tag.outputs.tag }}" ./
docker push "${{ vars.GAR_LOCATION }}-docker.pkg.dev/${{ vars.PROJECT_ID }}/${{ vars.SERVICE }}/${{ vars.SERVICE }}:${{ steps.tag.outputs.tag }}"
# END - Docker auth and build
deploy-to-staging:
needs: build-push
permissions:
id-token: 'write'
issues: 'write'
environment: dev
runs-on: ubuntu-latest
steps:
# Authenticate to Google Cloud
- name: Google Auth
id: auth
uses: 'google-github-actions/auth@v0'
with:
token_format: 'access_token'
workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # e.g. - projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider
service_account: '${{ secrets.WIF_SERVICE_ACCOUNT }}' # e.g. - [email protected]
- name: Deploy to Cloud Run
id: deploy
uses: google-github-actions/deploy-cloudrun@v0
with:
service: ${{ vars.SERVICE }}
region: ${{ vars.REGION }}
# NOTE: If using a pre-built image, update the image name here
image: ${{ vars.GAR_LOCATION }}-docker.pkg.dev/${{ vars.PROJECT_ID }}/${{ vars.SERVICE }}/${{ vars.SERVICE }}:${{ needs.build-push.outputs.git-tag }}
test-staging:
needs:
- build-push
- deploy-to-staging
permissions:
id-token: 'write'
issues: 'write'
environment: dev
runs-on: ubuntu-latest
steps:
- name: Test staging
uses: cygnetdigital/[email protected]
with:
url: ${{ vars.APP_URL }} # e.g. - https://actions-demo-lkxshjwprq-uc.a.run.app/
responseCode: '200,500'
timeout: 2000
interval: 500
# Authenticate to Google Cloud
- name: Google Auth
id: auth
uses: 'google-github-actions/auth@v0'
with:
token_format: 'access_token'
workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # e.g. - projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider
service_account: '${{ secrets.WIF_SERVICE_ACCOUNT }}' # e.g. - [email protected]
- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v1'
with:
version: '>= 363.0.0'
# Use cloud CLI to run commands needed to check image version
- name: 'Use gcloud CLI'
id: get-tag
run: |
echo "tag=$(gcloud run services describe ${{ vars.SERVICE }} --region=${{ vars.REGION }} | grep Image | awk -F: '{print $3}')" >> $GITHUB_OUTPUT
- name: Check image version
if: ${{steps.get-tag.outputs.tag != needs.build-push.outputs.git-tag }}
run: |-
echo "Error: Version mismatch, Expected ${{needs.build-push.outputs.git-tag}} but got ${{steps.get-tag.outputs.tag}}"
exit 1
deploy-to-production:
needs:
- build-push
- test-staging
permissions:
id-token: 'write'
environment: prod
runs-on: ubuntu-latest
steps:
- name: Google Auth
id: auth
uses: 'google-github-actions/auth@v0'
with:
token_format: 'access_token'
workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # e.g. - projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider
service_account: '${{ secrets.WIF_SERVICE_ACCOUNT }}' # e.g. - [email protected]
- name: Deploy to Cloud Run
id: deploy
uses: google-github-actions/deploy-cloudrun@v0
with:
service: ${{ vars.SERVICE }}
region: ${{ vars.REGION }}
image: ${{ vars.GAR_LOCATION }}-docker.pkg.dev/${{ vars.PROJECT_ID }}/${{ vars.SERVICE }}/${{ vars.SERVICE }}:${{ needs.build-push.outputs.git-tag }}
test-production:
needs:
- build-push
- deploy-to-production
permissions:
id-token: 'write'
issues: 'write'
environment: prod
runs-on: ubuntu-latest
steps:
- name: Test staging
uses: cygnetdigital/[email protected]
with:
url: ${{ vars.APP_URL }} # e.g. - https://actions-demo-lkxshjwprq-uc.a.run.app/
responseCode: '200,500'
timeout: 2000
interval: 500
- name: Google Auth
id: auth
uses: 'google-github-actions/auth@v0'
with:
token_format: 'access_token'
workload_identity_provider: '${{ secrets.WIF_PROVIDER }}' # e.g. - projects/123456789/locations/global/workloadIdentityPools/my-pool/providers/my-provider
service_account: '${{ secrets.WIF_SERVICE_ACCOUNT }}' # e.g. - [email protected]
- name: 'Set up Cloud SDK'
uses: 'google-github-actions/setup-gcloud@v1'
with:
version: '>= 363.0.0'
- name: 'Use gcloud CLI'
id: get-tag
run: |
echo "tag=$(gcloud run services describe ${{ vars.SERVICE }} --region=${{ vars.REGION }} | grep Image | awk -F: '{print $3}')" >> $GITHUB_OUTPUT
- name: Check image version
if: ${{steps.get-tag.outputs.tag != needs.build-push.outputs.git-tag }}
run: |-
echo "Error: Version mismatch, Expected ${{needs.build-push.outputs.git-tag}} but got ${{steps.get-tag.outputs.tag}}"
exit 1