Skip to content

Commit f6783c4

Browse files
author
Andrew Hobson
committed
build(circleci): Save and link to CircleCI artifact
* Save storybook as artifact and link GitHub deployment to it
1 parent 5a60443 commit f6783c4

File tree

3 files changed

+89
-0
lines changed

3 files changed

+89
-0
lines changed

.circleci/config.yml

+16
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,22 @@ jobs:
3232

3333
- run: yarn lint
3434
- run: yarn danger ci --failOnErrors
35+
- run:
36+
name: Create GitHub Deployment
37+
command: ./scripts/deployment_start.sh > deployment
38+
- run:
39+
name: Build Storybook
40+
command: yarn run build-storybook
41+
- store_artifacts:
42+
path: storybook-static
43+
- run:
44+
name: Add GitHub Deployment success status
45+
command: ./scripts/deployment_stop.sh success
46+
when: on_success
47+
- run:
48+
name: Add GitHub Deployment error status
49+
command: ./scripts/deployment_stop.sh error
50+
when: on_fail
3551

3652
workflows:
3753
build:

scripts/deployment_start.sh

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
token=${GITHUB_DEPLOYMENTS_TOKEN:?"Missing GITHUB_TOKEN environment variable"}
6+
7+
if ! deployment=$(curl -s \
8+
-X POST \
9+
-H "Authorization: bearer ${token}" \
10+
-d "{ \"ref\": \"${CIRCLE_SHA1}\", \"environment\": \"storybook\", \"description\": \"Storybook\", \"transient_environment\": true, \"auto_merge\": false, \"required_contexts\": []}" \
11+
-H "Content-Type: application/json" \
12+
"https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/deployments"); then
13+
echo "POSTing deployment status failed, exiting (not failing build)" 1>&2
14+
exit 1
15+
fi
16+
17+
if ! deployment_id=$(echo "${deployment}" | python -c 'import sys, json; print json.load(sys.stdin)["id"]'); then
18+
echo "Could not extract deployment ID from API response" 1>&2
19+
exit 3
20+
fi
21+
22+
echo "${deployment_id}" > deployment

scripts/deployment_stop.sh

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
#!/bin/sh
2+
3+
set -eu
4+
5+
token=${GITHUB_DEPLOYMENTS_TOKEN:?"Missing GITHUB_TOKEN environment variable"}
6+
7+
if ! deployment_id=$(cat deployment); then
8+
echo "Deployment ID was not found" 1>&2
9+
exit 3
10+
fi
11+
12+
if [ "$1" = "error" ]; then
13+
curl -s \
14+
-X POST \
15+
-H "Authorization: bearer ${token}" \
16+
-d "{\"state\": \"error\", \"environment\": \"storybook\"" \
17+
-H "Content-Type: application/json" \
18+
"https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/deployments/${deployment_id}/statuses"
19+
exit 1
20+
fi
21+
22+
if ! repository=$(curl -s \
23+
-X GET \
24+
-H "Authorization: bearer ${token}" \
25+
-d "{}" \
26+
-H "Content-Type: application/json" \
27+
"https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"); then
28+
echo "Could not fetch repository data" 1>&2
29+
exit 1
30+
fi
31+
32+
if ! repository_id=$(echo "${repository}" | python -c 'import sys, json; print json.load(sys.stdin)["id"]'); then
33+
echo "Could not extract repository ID from API response" 1>&2
34+
exit 3
35+
fi
36+
37+
# does not seem to be necessary for react-uswds, not sure why
38+
# path_to_repo=$(echo "$CIRCLE_WORKING_DIRECTORY" | sed -e
39+
# "s:~:$HOME:g")
40+
path_to_repo=""
41+
url="https://${CIRCLE_BUILD_NUM}-${repository_id}-gh.circle-artifacts.com/0${path_to_repo}/storybook-static/index.html"
42+
43+
if ! deployment=$(curl -s \
44+
-X POST \
45+
-H "Authorization: bearer ${token}" \
46+
-d "{\"state\": \"success\", \"environment\": \"storybook\", \"environment_url\": \"${url}\", \"target_url\": \"${url}\", \"log_url\": \"${url}\"}" \
47+
-H "Content-Type: application/json" \
48+
"https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/deployments/${deployment_id}/statuses"); then
49+
echo "POSTing deployment status failed, exiting (not failing build)" 1>&2
50+
exit 1
51+
fi

0 commit comments

Comments
 (0)