Skip to content

Instantly share code, notes, and snippets.

@tyom
Last active August 17, 2018 10:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tyom/1858f294240967e5b26f95b91491d42e to your computer and use it in GitHub Desktop.
Save tyom/1858f294240967e5b26f95b91491d42e to your computer and use it in GitHub Desktop.
PR preview and tests with now.sh
version: 2
references:
container_config: &container_config
docker:
- image: circleci/node:10.8
restore_deps_cache: &restore_deps_cache
restore_cache:
keys:
- v1-dependencies-{{ checksum "package.json" }}
- v1-dependencies-
save_deps_cache: &save_deps_cache
save_cache:
key: v1-dependencies-{{ checksum "package.json" }}
paths:
- node_modules
install_deps: &install_deps
run:
name: Dependencies
command: yarn install
jobs:
deploy-to-now:
<<: *container_config
steps:
- checkout
- *restore_deps_cache
- *install_deps
- *save_deps_cache
-
run:
name: Install now.sh
command: yarn global add now
-
run:
name: Deploy to now.sh
command: ./bin/deploy-now.sh
-
persist_to_workspace:
root: workspace
paths:
- deployment.json
run-functional-tests:
<<: *container_config
steps:
- checkout
- *restore_deps_cache
- *install_deps
- *save_deps_cache
-
attach_workspace:
at: workspace
-
run:
name: Run functional tests
command: yarn test-functional:local
workflows:
version: 2
deploy-and-test:
jobs:
- deploy-to-now
- run-functional-tests:
requires:
- deploy-to-now
filters:
branches:
only: master
#!/usr/bin/env bash
set -o nounset
export PATH="$PATH:$HOME/.config/yarn/global/node_modules/.bin"
DEPLOYMENT_URL=''
ALIASED_URL=''
if [ -n "${CIRCLE_PULL_REQUEST:-}" ]; then
PR_NUMBER=${CIRCLE_PULL_REQUEST##*/}
fi
# Post issue comment in PR with deployment URL
function post-github-comment {
curl -X "POST" "https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/issues/${PR_NUMBER}/comments" \
-H "Authorization: token ${GITHUB_TOKEN}" \
-H 'Content-Type: application/json; charset=utf-8' \
-d $'{
"body": "Deployed to: '"${ALIASED_URL}"'"
}'
}
function deploy-pr {
echo "Deploying PR #${PR_NUMBER}"
ALIAS="my-app-pr-${CIRCLE_PULL_REQUEST##*/}"
ALIASED_URL="https://${ALIAS}.now.sh"
DEPLOYMENT_URL=$(now -t ${NOW_TOKEN} -A now.pr.json)
now -t ${NOW_TOKEN} alias set ${DEPLOYMENT_URL} ${ALIAS}
now -t ${NOW_TOKEN} rm my-app-pr --safe --yes
post-github-comment
}
function deploy-branch {
echo "Deploying branch: ${CIRCLE_BRANCH:-}"
ALIAS="my-app-${CIRCLE_BRANCH}"
ALIASED_URL="https://${ALIAS}.now.sh"
DEPLOYMENT_URL=$(now -t ${NOW_TOKEN} -A now.branch.json)
now -t ${NOW_TOKEN} alias set ${DEPLOYMENT_URL} ${ALIAS}
now -t ${NOW_TOKEN} rm my-app-branch --safe --yes
}
function add-deployment-details-to-workspace {
mkdir -p workspace
echo $'{"now": "'${DEPLOYMENT_URL}'", "alias": "'${ALIASED_URL}'"}' > workspace/deployment.json
cat workspace/deployment.json
}
if [ -n "${PR_NUMBER:-}" ]
then
deploy-pr
add-deployment-details-to-workspace
elif [ "${CIRCLE_BRANCH:-}" == "master" ]
then
deploy-branch
add-deployment-details-to-workspace
else
echo "Nothing to do"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment