Skip to content

Commit f17c5d3

Browse files
committed
Integrate GitHub Deployments API into deploy workflows
1 parent d187f3b commit f17c5d3

File tree

2 files changed

+205
-2
lines changed

2 files changed

+205
-2
lines changed

.github/workflows/infra-deploy.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ on:
1212

1313
env:
1414
KUBE_CONFIG_DATA: ${{ secrets.kubeconfig_data_prod }}
15+
# For hub CLI tool
16+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1517

1618
jobs:
1719
deploy-prod:
@@ -46,6 +48,23 @@ jobs:
4648
exit 1
4749
fi
4850
51+
- name: Get deployment information
52+
run: |
53+
hub api /repos/${{ github.repository }}/deployments?ref=$DEPLOY_REF -X GET | jq .[0] > /tmp/deployment.json
54+
55+
- name: Mark deployment as started
56+
run: |
57+
hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \
58+
-X POST \
59+
-H "Accept: application/json, application/vnd.github.flash-preview+json" \
60+
--input <(cat <<EOF
61+
{
62+
"state": "in_progress",
63+
"description": "Rolling out $DEPLOY_REF",
64+
"log_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
65+
}
66+
EOF)
67+
4968
- name: Get manifest archive
5069
run: |
5170
set -e
@@ -66,3 +85,30 @@ jobs:
6685
run: |
6786
kubectl -n chime rollout status deployment.v1.apps/chime
6887
kubectl -n chime get deployment chime -o yaml
88+
89+
- name: Mark deployment as failed
90+
if: failure()
91+
run: |
92+
hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \
93+
-X POST \
94+
-H "Accept: application/json, application/vnd.github.flash-preview+json" \
95+
--input <(cat <<EOF
96+
{
97+
"state": "failure",
98+
"description": "Error in job deploy-prod",
99+
"log_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
100+
}
101+
EOF)
102+
103+
- name: Mark deployment completed
104+
run: |
105+
hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \
106+
-X POST \
107+
-H "Accept: application/json, application/vnd.github.flash-preview+json" \
108+
--input <(cat <<EOF
109+
{
110+
"state": "success",
111+
"description": "$DEPLOY_REF deployed",
112+
"log_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
113+
}
114+
EOF)

.github/workflows/infra-release.yml

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ on:
1010
env:
1111
IMAGE_NAME: penn-chime
1212
KUBE_CONFIG_DATA: ${{ secrets.kubeconfig_data_preprod }}
13+
# For hub CLI tool
14+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
1315

1416
jobs:
1517
# Run tests.
@@ -72,6 +74,50 @@ jobs:
7274
name: release
7375
path: release
7476

77+
# Create the Deployment API object
78+
new-deployment:
79+
needs: release-data
80+
runs-on: ubuntu-latest
81+
if: github.event_name == 'release'
82+
83+
steps:
84+
- name: Get release information
85+
uses: actions/download-artifact@v1
86+
with:
87+
name: release
88+
89+
- name: Create deployment object for release
90+
run: |
91+
set -e
92+
# Determine deployment environment
93+
if ${{ github.event.release.prerelease }}; then
94+
target=preprod
95+
else
96+
target=prod
97+
fi
98+
99+
# Create deployment
100+
hub api /repos/${{ github.repository }}/deployments -X POST --input <(cat <<EOF
101+
{
102+
"ref": "$(cat release/tag)",
103+
"auto_merge": false,
104+
"required_contexts": [],
105+
"environment": "$target"
106+
}
107+
EOF) > /tmp/deployment.json
108+
109+
# Set status to pending
110+
hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \
111+
-X POST \
112+
-H "Accept: application/json, application/vnd.github.flash-preview+json" \
113+
--input <(cat <<EOF
114+
{
115+
"state": "pending",
116+
"description": "Building deployment artifacts",
117+
"log_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
118+
}
119+
EOF)
120+
75121
# Push image to GitHub Packages.
76122
# See also https://docs.docker.com/docker-hub/builds/
77123
publish-image:
@@ -91,6 +137,10 @@ jobs:
91137
with:
92138
name: release
93139

140+
- name: Get deployment information
141+
run: |
142+
hub api /repos/${{ github.repository }}/deployments?ref=$(cat release/tag) -X GET | jq .[0] > /tmp/deployment.json
143+
94144
- name: Build image
95145
run: docker build . --file Dockerfile --tag image
96146

@@ -112,6 +162,20 @@ jobs:
112162
echo prod release published to :latest
113163
fi
114164
165+
- name: Mark deployment as failed
166+
if: failure()
167+
run: |
168+
hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \
169+
-X POST \
170+
-H "Accept: application/json, application/vnd.github.flash-preview+json" \
171+
--input <(cat <<EOF
172+
{
173+
"state": "failure",
174+
"description": "Error in job publish-image",
175+
"log_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
176+
}
177+
EOF)
178+
115179
publish-manifests:
116180

117181
# Published manifests will signify successful image build
@@ -127,6 +191,10 @@ jobs:
127191
with:
128192
name: release
129193

194+
- name: Get deployment information
195+
run: |
196+
hub api /repos/${{ github.repository }}/deployments?ref=$(cat release/tag) -X GET | jq .[0] > /tmp/deployment.json
197+
130198
- name: Build manifest archive
131199
run: |
132200
tagname=$(cat release/tag)
@@ -155,6 +223,20 @@ jobs:
155223
files: '*.zip'
156224
repo-token: ${{ secrets.GITHUB_TOKEN }}
157225

226+
- name: Mark deployment as failed
227+
if: failure()
228+
run: |
229+
hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \
230+
-X POST \
231+
-H "Accept: application/json, application/vnd.github.flash-preview+json" \
232+
--input <(cat <<EOF
233+
{
234+
"state": "failure",
235+
"description": "Error in job publish-manifests",
236+
"log_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
237+
}
238+
EOF)
239+
158240
deploy-preprod:
159241

160242
needs: publish-manifests
@@ -179,6 +261,23 @@ jobs:
179261
with:
180262
name: release
181263

264+
- name: Get deployment information
265+
run: |
266+
hub api /repos/${{ github.repository }}/deployments?ref=$(cat release/tag) -X GET | jq .[0] > /tmp/deployment.json
267+
268+
- name: Mark deployment as started
269+
run: |
270+
hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \
271+
-X POST \
272+
-H "Accept: application/json, application/vnd.github.flash-preview+json" \
273+
--input <(cat <<EOF
274+
{
275+
"state": "in_progress",
276+
"description": "Rolling out $(cat release/tag)",
277+
"log_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
278+
}
279+
EOF)
280+
182281
- name: Get manifest archive
183282
run: |
184283
set -e
@@ -201,6 +300,33 @@ jobs:
201300
kubectl -n chime rollout status deployment.v1.apps/chime
202301
kubectl -n chime get deployment/chime -o yaml
203302
303+
- name: Mark deployment as failed
304+
if: failure()
305+
run: |
306+
hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \
307+
-X POST \
308+
-H "Accept: application/json, application/vnd.github.flash-preview+json" \
309+
--input <(cat <<EOF
310+
{
311+
"state": "failure",
312+
"description": "Error in job deploy-preprod",
313+
"log_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
314+
}
315+
EOF)
316+
317+
- name: Mark deployment completed
318+
run: |
319+
hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \
320+
-X POST \
321+
-H "Accept: application/json, application/vnd.github.flash-preview+json" \
322+
--input <(cat <<EOF
323+
{
324+
"state": "success",
325+
"description": "$(cat release/tag) deployed",
326+
"log_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
327+
}
328+
EOF)
329+
204330
stage-prod-deploy:
205331
needs: publish-manifests
206332
if: github.event_name == 'release' && ! github.event.release.prerelease
@@ -229,7 +355,6 @@ jobs:
229355
run: |
230356
echo "::set-env name=VERSION::$(cat release/version)"
231357
echo "::set-env name=TAGNAME::$(cat release/tag)"
232-
echo "::set-env name=GITHUB_TOKEN::${{ secrets.GITHUB_TOKEN }}"
233358
234359
- name: Get manifest archive
235360
run: |
@@ -240,6 +365,10 @@ jobs:
240365
curl -LO "$arc_url"
241366
unzip $arc_fname
242367
368+
- name: Get deployment information
369+
run: |
370+
hub api /repos/${{ github.repository }}/deployments?ref=$(cat release/tag) -X GET | jq .[0] > /tmp/deployment.json
371+
243372
- name: Open deployment pull request
244373
run: |
245374
set -e
@@ -254,4 +383,32 @@ jobs:
254383
\`\`\`
255384
EOF
256385
257-
hub pull-request -b releases/v1 -h develop -F $pr_body_file
386+
hub pull-request -b releases/v1 -h develop -F $pr_body_file > /tmp/pr.json
387+
388+
- name: Mark deployment as failed
389+
if: failure()
390+
run: |
391+
hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \
392+
-X POST \
393+
-H "Accept: application/json, application/vnd.github.flash-preview+json" \
394+
--input <(cat <<EOF
395+
{
396+
"state": "failure",
397+
"description": "Error in job stage-prod-deploy",
398+
"log_url": "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}"
399+
}
400+
EOF)
401+
402+
- name: Mark deployment queued
403+
run: |
404+
# Update status to success
405+
hub api /repos/${{ github.repository }}/deployments/$(jq .id < /tmp/deployment.json)/statuses \
406+
-X POST \
407+
-H "Accept: application/json, application/vnd.github.flash-preview+json" \
408+
--input <(cat <<EOF
409+
{
410+
"state": "queued",
411+
"description": "Awaiting approval/merge of deployment Pull Request",
412+
"log_url": "$(jq < /tmp/pr.json .html_url)"
413+
}
414+
EOF)

0 commit comments

Comments
 (0)