Skip to content

Commit

Permalink
feat: enable preview deployments (#42)
Browse files Browse the repository at this point in the history
* feat: enable preview deployments

* feat: post comment to GitHub PR

* feat: add staging deployment notification to PR

* chore: fix comment

* chore: further fixes
  • Loading branch information
ripixel committed Dec 28, 2022
1 parent 9657402 commit 0945224
Show file tree
Hide file tree
Showing 3 changed files with 11,548 additions and 10,740 deletions.
89 changes: 76 additions & 13 deletions .circleci/config.yml
Expand Up @@ -12,12 +12,12 @@ jobs: # a collection of steps
steps:
- checkout
- restore_cache: # special step to restore the dependency cache
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
key: dependency-cache-{{ checksum "package-lock.json" }}
- run:
name: Install NPM Dependencies
command: npm i
- save_cache: # special step to save the dependency cache
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
key: dependency-cache-{{ checksum "package-lock.json" }}
paths:
- ~/tmp/node_modules

Expand All @@ -26,7 +26,7 @@ jobs: # a collection of steps
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
key: dependency-cache-{{ checksum "package-lock.json" }}
- run: # run linter
name: Linting
command: npm run lint:ci
Expand All @@ -40,15 +40,14 @@ jobs: # a collection of steps
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
key: dependency-cache-{{ checksum "package-lock.json" }}
- restore_cache:
key: changelog-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
- run: # run build
name: Build
command: npm run build:dev
- store_artifacts: # for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
path: public
prefix: public
- save_cache: # special step to save the public cache and deploy files
key: deploy-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
paths:
Expand All @@ -62,7 +61,7 @@ jobs: # a collection of steps
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
key: dependency-cache-{{ checksum "package-lock.json" }}
- run:
name: Add Git User details
command: |
Expand All @@ -73,7 +72,6 @@ jobs: # a collection of steps
command: npm run build
- store_artifacts: # for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
path: public
prefix: public
- save_cache: # special step to save the public cache and deploy files
key: deploy-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
paths:
Expand All @@ -87,7 +85,7 @@ jobs: # a collection of steps
steps:
- checkout
- restore_cache:
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
key: dependency-cache-{{ checksum "package-lock.json" }}
- run:
name: Add Git User details
command: |
Expand All @@ -101,7 +99,6 @@ jobs: # a collection of steps
command: npm run build
- store_artifacts: # for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
path: public
prefix: public
- save_cache: # special step to save the public cache and deploy files
key: deploy-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
paths:
Expand All @@ -116,7 +113,7 @@ jobs: # a collection of steps
- restore_cache: # restore the /public folder and associated deploy files
key: deploy-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
- restore_cache: # special step to restore the dependency cache
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
key: dependency-cache-{{ checksum "package-lock.json" }}
- run:
name: Deploy to Firebase Production Hosting
command: ./node_modules/.bin/firebase deploy --token "$FIREBASE_TOKEN" --only hosting:production
Expand All @@ -127,13 +124,69 @@ jobs: # a collection of steps
- restore_cache: # restore the /public folder and associated deploy files
key: deploy-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
- restore_cache: # special step to restore the dependency cache
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
key: dependency-cache-{{ checksum "package-lock.json" }}
- run:
name: Deploy to Firebase Staging Hosting
command: ./node_modules/.bin/firebase deploy --token "$FIREBASE_TOKEN" --only hosting:staging
- run:
name: Post Github PR Comment
command: |
sudo apt-get install jq
pr_response=$(curl --location --request GET "https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/pulls?head=$CIRCLE_PROJECT_USERNAME:$CIRCLE_BRANCH&state=open" \
-u $GH_USER:$GH_TOKEN)
if [ $(echo $pr_response | jq length) -eq 0 ]; then
echo "No PR found to update"
else
pr_comment_url=$(echo $pr_response | jq -r ".[]._links.comments.href")
fi
curl --location --request POST "$pr_comment_url" \
-u $GH_USER:$GH_TOKEN \
--header 'Content-Type: application/json' \
--data-raw '{
"body": "Successfully deployed to Firebase! Go to https://staging.ripixel.co.uk"
}'
deploy_to_firebase_preview: # deploy the project to preview channel - SHOULD ONLY BE RUN ON PR BRANCHES
executor: node-project
steps:
- restore_cache: # restore the /public folder and associated deploy files
key: deploy-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
- restore_cache: # special step to restore the dependency cache
key: dependency-cache-{{ checksum "package-lock.json" }}
- run:
name: Deploy to Firebase Preview Channel
command: ./node_modules/.bin/firebase hosting:channel:deploy $CIRCLE_BRANCH --token "$FIREBASE_TOKEN"
- run:
name: Post Github PR Comment
command: |
sudo apt-get install jq
channels=$(./node_modules/.bin/firebase hosting:channel:list)
regex='(https:\/\/[a-z0-9-]*--'$(echo $CIRCLE_BRANCH | sed "s/\//-/")'-[a-z0-9-]*.web.app)'
[[ $channels =~ $regex ]] && url=${BASH_REMATCH[0]}
if [ $(echo $url | jq length) -eq 0]; then
url="Unable to get URL - check Firebase console"
fi
pr_response=$(curl --location --request GET "https://api.github.com/repos/$CIRCLE_PROJECT_USERNAME/$CIRCLE_PROJECT_REPONAME/pulls?head=$CIRCLE_PROJECT_USERNAME:$CIRCLE_BRANCH&state=open" \
-u $GH_USER:$GH_TOKEN)
if [ $(echo $pr_response | jq length) -eq 0 ]; then
echo "No PR found to update"
else
pr_comment_url=$(echo $pr_response | jq -r ".[]._links.comments.href")
fi
curl --location --request POST "$pr_comment_url" \
-u $GH_USER:$GH_TOKEN \
--header 'Content-Type: application/json' \
--data-raw '{"body": "Successfully deployed to Firebase preview channel! Available at: '"$url"'"}'
workflows:
version: 2
build_and_deploy:
jobs:
- install_deps
Expand All @@ -159,6 +212,15 @@ workflows:
requires:
- lint
- build_prod_with_version
- deploy_to_firebase_preview:
filters:
branches:
ignore:
- master
- staging
requires:
- lint
- build_dev
- deploy_to_firebase_stg:
filters:
branches:
Expand All @@ -169,7 +231,7 @@ workflows:
daily:
triggers:
- schedule:
cron: '55 20 * * *'
cron: "55 20 * * *"
filters:
branches:
only:
Expand All @@ -186,3 +248,4 @@ workflows:
requires:
- lint
- build_prod_no_version
# VS Code Extension Version: 1.5.0

0 comments on commit 0945224

Please sign in to comment.