-
Notifications
You must be signed in to change notification settings - Fork 58
323 lines (302 loc) · 11.6 KB
/
std.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# yq -y < std-unexploded.yml > workflows/std.yml
name: STD
on:
workflow_dispatch:
inputs:
deploy-dev-preview:
description: Deploy to dev-preview
type: boolean
required: true
default: false
deploy-dev-preprod:
description: Deploy to dev-preprod
type: boolean
required: true
default: false
deploy-staging-preprod:
description: Deploy to staging-preprod
type: boolean
required: true
default: false
deploy-dev-mainnet:
description: Deploy to dev-mainnet
type: boolean
required: true
default: false
workflow_call:
inputs:
deploy-dev-preprod:
type: boolean
required: true
deploy-staging-preprod:
type: boolean
required: true
deploy-dev-mainnet:
type: boolean
required: true
secrets:
AWS_ACCESS_KEY:
required: true
AWS_SECRET_ACCESS_KEY:
required: true
SSH_PRIVATE_KEY:
required: true
pull_request:
branches:
- master
- conway-era
push:
branches:
- master
- conway-era
tags:
- '@cardano-sdk/cardano-services**'
env:
# NIX_UPLOAD_CACHE: s3://lace-nix-cache?region=us-east-1
DISCOVERY_USER_NAME: gha-runner
DISCOVERY_KNOWN_HOSTS_ENTRY: "65.109.126.156 ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIEOVVDZydvD+diYa6A3EtA3WGw5NfN0wv7ckQxa/fX1O"
concurrency:
group: std-${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
discover:
# Don’t run on PRs from forks (no access to secrets):
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
outputs:
hits: ${{ steps.discovery.outputs.hits }}
deployment-matrix: ${{ steps.deployment-matrix.outputs.deployment-matrix }}
runs-on: [self-hosted, discovery]
env:
AWS_REGION: us-east-1
AWS_ROLE_ARN: arn:aws:iam::926093910549:role/lace-ci
permissions:
id-token: write
contents: read
steps:
- name: Configure AWS Credentials
uses: aws-actions/[email protected]
with:
role-to-assume: ${{ env.AWS_ROLE_ARN }}
aws-region: ${{ env.AWS_REGION }}
# account is part of ecr url, thus part of `hits` output and needs to pass
mask-aws-account-id: false
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- name: Show commit
# TODO: uncomment when nixbuild works well together with
# nix daemon mode on hosted runners
# - uses: nixbuild/nixbuild-action@v17
# with:
# nixbuild_ssh_key: ${{ secrets.SSH_PRIVATE_KEY }}
# generate_summary_for: job
shell: bash
run: |
echo commit: ${{ github.sha }}
- name: Determine Deployment Matrix
id: deployment-matrix
run: |
(
if [ "true" == ${{ inputs.deploy-dev-preview || (github.event_name == 'push' && github.ref_name == 'master') }} ] ; then
echo '{"environment":"dev-preview", "target":"dev-preview@us-east-1", "url": "https://dev-preview.lw.iog.io/"}'
fi
if [ "true" == ${{ inputs.deploy-dev-preprod || false }} ] ; then
echo '{"environment":"dev-preprod", "target":"dev-preprod@us-east-1@v2", "url": "https://dev-preprod.lw.iog.io/"}'
fi
if [ "true" == ${{ inputs.deploy-staging-preprod || false }} ] ; then
echo '{"environment":"staging-preprod", "target":"staging-preprod@us-east-1@v2", "url": "https://staging-preprod.lw.iog.io/"}'
fi
if [ "true" == ${{ inputs.deploy-dev-mainnet || false }} ] ; then
echo '{"environment":"dev-mainnet", "target":"dev-mainnet@us-east-1", "url": "https://dev-mainnet.lw.iog.io/"}'
fi
) | jq --slurp >deployment-matrix.json
cat deployment-matrix.json
# TODO: should we remove the trailing double quotes?
echo "deployment-matrix=$(cat deployment-matrix.json | jq -c . | jq --raw-input)" >> "$GITHUB_OUTPUT"
- uses: divnix/std-action/discover@main
with: {ffBuildInstructions: true}
id: discovery
images:
name: ${{ matrix.target.jobName }}
runs-on: ubuntu-latest
needs: discover
env:
AWS_REGION: us-east-1
AWS_ROLE_ARN: arn:aws:iam::926093910549:role/lace-ci
# NIX_UPLOAD_CACHE: s3://lace-nix-cache?region=us-east-1
permissions:
id-token: write
contents: read
strategy:
matrix:
target: ${{ fromJSON(needs.discover.outputs.hits).oci-images && fromJSON(needs.discover.outputs.hits).oci-images.publish || fromJSON('["dummy-target"]') }}
steps:
- name: Configure AWS Credentials
if: matrix.target != 'dummy-target'
uses: aws-actions/[email protected]
with:
role-to-assume: ${{ env.AWS_ROLE_ARN }}
aws-region: ${{ env.AWS_REGION }}
- name: Login to Amazon ECR
if: matrix.target != 'dummy-target'
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1
- uses: nixbuild/nix-quick-install-action@v25
if: matrix.target != 'dummy-target'
- uses: nixbuild/nixbuild-action@v17
if: matrix.target != 'dummy-target'
with:
nixbuild_ssh_key: ${{ secrets.SSH_PRIVATE_KEY }}
generate_summary_for: job
- uses: divnix/std-action/setup-discovery-ssh@main
if: matrix.target != 'dummy-target'
with:
ssh_key: ${{ secrets.SSH_PRIVATE_KEY }}
user_name: ${{ env.DISCOVERY_USER_NAME }}
ssh_known_hosts_entry: ${{ env.DISCOVERY_KNOWN_HOSTS_ENTRY }}
- name: Show commit
shell: bash
run: |
echo commit: ${{ github.sha }}
- uses: divnix/std-action/run@main
if: matrix.target != 'dummy-target'
with: {ffBuildInstructions: true, remoteStore: "ssh-ng://eu.nixbuild.net"}
diff:
needs: images
name: Diff & Comment
if: github.event_name == 'pull_request' && (github.base_ref == 'master')
permissions:
contents: read
pull-requests: write
runs-on: ubuntu-22.04
steps:
- uses: actions/checkout@v4
- uses: nixbuild/nix-quick-install-action@v25
- uses: nixbuild/nixbuild-action@v17
with:
nixbuild_ssh_key: ${{ secrets.SSH_PRIVATE_KEY }}
generate_summary_for: job
# Further steps assume AWS_PROFILE=lw, while the official action has no way to specify that profile:
- name: Set up AWS credentials
run: |
mkdir -p ~/.aws
cat <<EOF >~/.aws/credentials
[lw]
aws_access_key_id = ${{ secrets.AWS_ACCESS_KEY}}
aws_secret_access_key = ${{ secrets.AWS_SECRET_ACCESS_KEY }}
EOF
cat <<EOF >~/.aws/config
[lw]
region = us-east-1
EOF
- uses: divnix/std-action/setup-discovery-ssh@main
with:
ssh_key: ${{ secrets.SSH_PRIVATE_KEY }}
user_name: ${{ env.DISCOVERY_USER_NAME }}
ssh_known_hosts_entry: ${{ env.DISCOVERY_KNOWN_HOSTS_ENTRY }}
- name: Generate the Diff
run: |
echo 'export K8S_USER=eks-devs' >.envrc.local
nix develop .#x86_64-linux.local.envs.main -L --command bash -c '
set -euo pipefail
export AWS_PROFILE="lw"
export AWS_REGION="us-east-1"
printf "" >pr-comment.md
for target in \
"dev-preview@us-east-1" \
"dev-preprod@us-east-1@v2" \
"dev-mainnet@us-east-1" \
; do
git restore .kube
nix run -L ".#cardano-services.${target}.plan" | tee k8s-plan.diff
(
echo "<details>"
echo "<summary><code>${target}</code> would change:</summary>"
echo
cat k8s-plan.diff \
| sed -r "s|^[^ +-].*|\`\`\`\n\n\0\n\n\`\`\`diff|g" \
| tail -n +3 \
| sed -r "s|^([^ +-].*)has changed(.*)|\1would change\2|g"
echo "\`\`\`"
echo "</details>"
) >>pr-comment.md
done
'
- name: Post Comment on the PR
env:
GH_TOKEN: ${{ github.token }}
COMMENT_MARKER: "nix-helm-diff-777f3796-c80d-4d68-bf4f-8faad564f03f"
run: |
prNumber=$(cut -d/ -f1 <<<'${{ github.ref_name }}')
# The `gh` command doesn’t return numeric comment ids, but instead node_ids, which don’t work with the regular API
# Why the regular API? Because the `gh` command doesn’t support editing comments:
existingCommentId=$(
curl --fail-with-body -sSL \
-X GET \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/${prNumber}/comments" \
| jq -r --arg commentMarker "$COMMENT_MARKER" 'first(.[] | select(.body | contains($commentMarker)) | .id)'
)
if [ -z "$existingCommentId" ]; then
gh pr comment "$prNumber" --body "$(cat pr-comment.md) <!-- $COMMENT_MARKER -->"
else
curl --fail-with-body -sSL \
-X PATCH \
-H "Authorization: Bearer $GH_TOKEN" \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"https://api.github.com/repos/${GITHUB_REPOSITORY}/issues/comments/${existingCommentId}" \
-d "$(jq --null-input --arg body "$(cat pr-comment.md) <!-- $COMMENT_MARKER -->" '{body: $body}')"
fi
deploy:
needs: [images,discover]
concurrency:
# Only one deployment at a time per environment, and wait for the previous one to finish:
group: deploy-${{ matrix.environment }}
cancel-in-progress: false
if: fromJSON(needs.discover.outputs.deployment-matrix) != '[]'
strategy:
matrix:
include: ${{ fromJSON(fromJSON(needs.discover.outputs.deployment-matrix)) }}
name: Deploy (${{ matrix.environment }})
runs-on: ubuntu-22.04
environment:
name: ${{ matrix.environment }}
url: ${{ matrix.url }}
steps:
- uses: actions/checkout@v4
- uses: nixbuild/nix-quick-install-action@v25
- uses: nixbuild/nixbuild-action@v17
with:
nixbuild_ssh_key: ${{ secrets.SSH_PRIVATE_KEY }}
generate_summary_for: job
# Further steps assume AWS_PROFILE=lw, while the official action has no way to specify that profile:
- name: Set up AWS credentials
run: |
mkdir -p ~/.aws
cat <<EOF >~/.aws/credentials
[lw]
aws_access_key_id = ${{ secrets.AWS_ACCESS_KEY}}
aws_secret_access_key = ${{ secrets.AWS_SECRET_ACCESS_KEY }}
EOF
cat <<EOF >~/.aws/config
[lw]
region = us-east-1
EOF
- uses: divnix/std-action/setup-discovery-ssh@main
with:
ssh_key: ${{ secrets.SSH_PRIVATE_KEY }}
user_name: ${{ env.DISCOVERY_USER_NAME }}
ssh_known_hosts_entry: ${{ env.DISCOVERY_KNOWN_HOSTS_ENTRY }}
- name: Deploy to K8s
run: |
echo 'export K8S_USER=eks-devs' >.envrc.local
nix develop .#x86_64-linux.local.envs.main -L --command bash -c '
set -euo pipefail
export AWS_PROFILE="lw"
export AWS_REGION="us-east-1"
git restore .kube
echo yes | nix run -L ".#cardano-services.${{ matrix.target }}.apply"
'