Skip to content

Commit

Permalink
build(circleci): Save and link to CircleCI artifact
Browse files Browse the repository at this point in the history
* Save storybook as artifact and link GitHub deployment to it
  • Loading branch information
Andrew Hobson committed Feb 22, 2021
1 parent 5a60443 commit f6783c4
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,22 @@ jobs:

- run: yarn lint
- run: yarn danger ci --failOnErrors
- run:
name: Create GitHub Deployment
command: ./scripts/deployment_start.sh > deployment
- run:
name: Build Storybook
command: yarn run build-storybook
- store_artifacts:
path: storybook-static
- run:
name: Add GitHub Deployment success status
command: ./scripts/deployment_stop.sh success
when: on_success
- run:
name: Add GitHub Deployment error status
command: ./scripts/deployment_stop.sh error
when: on_fail

workflows:
build:
Expand Down
22 changes: 22 additions & 0 deletions scripts/deployment_start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh

set -eu

token=${GITHUB_DEPLOYMENTS_TOKEN:?"Missing GITHUB_TOKEN environment variable"}

if ! deployment=$(curl -s \
-X POST \
-H "Authorization: bearer ${token}" \
-d "{ \"ref\": \"${CIRCLE_SHA1}\", \"environment\": \"storybook\", \"description\": \"Storybook\", \"transient_environment\": true, \"auto_merge\": false, \"required_contexts\": []}" \
-H "Content-Type: application/json" \
"https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/deployments"); then
echo "POSTing deployment status failed, exiting (not failing build)" 1>&2
exit 1
fi

if ! deployment_id=$(echo "${deployment}" | python -c 'import sys, json; print json.load(sys.stdin)["id"]'); then
echo "Could not extract deployment ID from API response" 1>&2
exit 3
fi

echo "${deployment_id}" > deployment
51 changes: 51 additions & 0 deletions scripts/deployment_stop.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/sh

set -eu

token=${GITHUB_DEPLOYMENTS_TOKEN:?"Missing GITHUB_TOKEN environment variable"}

if ! deployment_id=$(cat deployment); then
echo "Deployment ID was not found" 1>&2
exit 3
fi

if [ "$1" = "error" ]; then
curl -s \
-X POST \
-H "Authorization: bearer ${token}" \
-d "{\"state\": \"error\", \"environment\": \"storybook\"" \
-H "Content-Type: application/json" \
"https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/deployments/${deployment_id}/statuses"
exit 1
fi

if ! repository=$(curl -s \
-X GET \
-H "Authorization: bearer ${token}" \
-d "{}" \
-H "Content-Type: application/json" \
"https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}"); then
echo "Could not fetch repository data" 1>&2
exit 1
fi

if ! repository_id=$(echo "${repository}" | python -c 'import sys, json; print json.load(sys.stdin)["id"]'); then
echo "Could not extract repository ID from API response" 1>&2
exit 3
fi

# does not seem to be necessary for react-uswds, not sure why
# path_to_repo=$(echo "$CIRCLE_WORKING_DIRECTORY" | sed -e
# "s:~:$HOME:g")
path_to_repo=""
url="https://${CIRCLE_BUILD_NUM}-${repository_id}-gh.circle-artifacts.com/0${path_to_repo}/storybook-static/index.html"

if ! deployment=$(curl -s \
-X POST \
-H "Authorization: bearer ${token}" \
-d "{\"state\": \"success\", \"environment\": \"storybook\", \"environment_url\": \"${url}\", \"target_url\": \"${url}\", \"log_url\": \"${url}\"}" \
-H "Content-Type: application/json" \
"https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/deployments/${deployment_id}/statuses"); then
echo "POSTing deployment status failed, exiting (not failing build)" 1>&2
exit 1
fi

0 comments on commit f6783c4

Please sign in to comment.