Skip to content

Commit 0945224

Browse files
authored
feat: enable preview deployments (#42)
* feat: enable preview deployments * feat: post comment to GitHub PR * feat: add staging deployment notification to PR * chore: fix comment * chore: further fixes
1 parent 9657402 commit 0945224

File tree

3 files changed

+11548
-10740
lines changed

3 files changed

+11548
-10740
lines changed

.circleci/config.yml

Lines changed: 76 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@ jobs: # a collection of steps
1212
steps:
1313
- checkout
1414
- restore_cache: # special step to restore the dependency cache
15-
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
15+
key: dependency-cache-{{ checksum "package-lock.json" }}
1616
- run:
1717
name: Install NPM Dependencies
1818
command: npm i
1919
- save_cache: # special step to save the dependency cache
20-
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
20+
key: dependency-cache-{{ checksum "package-lock.json" }}
2121
paths:
2222
- ~/tmp/node_modules
2323

@@ -26,7 +26,7 @@ jobs: # a collection of steps
2626
steps:
2727
- checkout
2828
- restore_cache:
29-
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
29+
key: dependency-cache-{{ checksum "package-lock.json" }}
3030
- run: # run linter
3131
name: Linting
3232
command: npm run lint:ci
@@ -40,15 +40,14 @@ jobs: # a collection of steps
4040
steps:
4141
- checkout
4242
- restore_cache:
43-
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
43+
key: dependency-cache-{{ checksum "package-lock.json" }}
4444
- restore_cache:
4545
key: changelog-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
4646
- run: # run build
4747
name: Build
4848
command: npm run build:dev
4949
- store_artifacts: # for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
5050
path: public
51-
prefix: public
5251
- save_cache: # special step to save the public cache and deploy files
5352
key: deploy-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
5453
paths:
@@ -62,7 +61,7 @@ jobs: # a collection of steps
6261
steps:
6362
- checkout
6463
- restore_cache:
65-
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
64+
key: dependency-cache-{{ checksum "package-lock.json" }}
6665
- run:
6766
name: Add Git User details
6867
command: |
@@ -73,7 +72,6 @@ jobs: # a collection of steps
7372
command: npm run build
7473
- store_artifacts: # for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
7574
path: public
76-
prefix: public
7775
- save_cache: # special step to save the public cache and deploy files
7876
key: deploy-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
7977
paths:
@@ -87,7 +85,7 @@ jobs: # a collection of steps
8785
steps:
8886
- checkout
8987
- restore_cache:
90-
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
88+
key: dependency-cache-{{ checksum "package-lock.json" }}
9189
- run:
9290
name: Add Git User details
9391
command: |
@@ -101,7 +99,6 @@ jobs: # a collection of steps
10199
command: npm run build
102100
- store_artifacts: # for display in Artifacts: https://circleci.com/docs/2.0/artifacts/
103101
path: public
104-
prefix: public
105102
- save_cache: # special step to save the public cache and deploy files
106103
key: deploy-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
107104
paths:
@@ -116,7 +113,7 @@ jobs: # a collection of steps
116113
- restore_cache: # restore the /public folder and associated deploy files
117114
key: deploy-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
118115
- restore_cache: # special step to restore the dependency cache
119-
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
116+
key: dependency-cache-{{ checksum "package-lock.json" }}
120117
- run:
121118
name: Deploy to Firebase Production Hosting
122119
command: ./node_modules/.bin/firebase deploy --token "$FIREBASE_TOKEN" --only hosting:production
@@ -127,13 +124,69 @@ jobs: # a collection of steps
127124
- restore_cache: # restore the /public folder and associated deploy files
128125
key: deploy-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
129126
- restore_cache: # special step to restore the dependency cache
130-
key: dependency-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
127+
key: dependency-cache-{{ checksum "package-lock.json" }}
131128
- run:
132129
name: Deploy to Firebase Staging Hosting
133130
command: ./node_modules/.bin/firebase deploy --token "$FIREBASE_TOKEN" --only hosting:staging
131+
- run:
132+
name: Post Github PR Comment
133+
command: |
134+
sudo apt-get install jq
135+
136+
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" \
137+
-u $GH_USER:$GH_TOKEN)
138+
139+
if [ $(echo $pr_response | jq length) -eq 0 ]; then
140+
echo "No PR found to update"
141+
else
142+
pr_comment_url=$(echo $pr_response | jq -r ".[]._links.comments.href")
143+
fi
144+
145+
curl --location --request POST "$pr_comment_url" \
146+
-u $GH_USER:$GH_TOKEN \
147+
--header 'Content-Type: application/json' \
148+
--data-raw '{
149+
"body": "Successfully deployed to Firebase! Go to https://staging.ripixel.co.uk"
150+
}'
151+
152+
deploy_to_firebase_preview: # deploy the project to preview channel - SHOULD ONLY BE RUN ON PR BRANCHES
153+
executor: node-project
154+
steps:
155+
- restore_cache: # restore the /public folder and associated deploy files
156+
key: deploy-cache-{{ .Environment.CIRCLE_WORKFLOW_ID }}
157+
- restore_cache: # special step to restore the dependency cache
158+
key: dependency-cache-{{ checksum "package-lock.json" }}
159+
- run:
160+
name: Deploy to Firebase Preview Channel
161+
command: ./node_modules/.bin/firebase hosting:channel:deploy $CIRCLE_BRANCH --token "$FIREBASE_TOKEN"
162+
- run:
163+
name: Post Github PR Comment
164+
command: |
165+
sudo apt-get install jq
166+
167+
channels=$(./node_modules/.bin/firebase hosting:channel:list)
168+
regex='(https:\/\/[a-z0-9-]*--'$(echo $CIRCLE_BRANCH | sed "s/\//-/")'-[a-z0-9-]*.web.app)'
169+
[[ $channels =~ $regex ]] && url=${BASH_REMATCH[0]}
170+
171+
if [ $(echo $url | jq length) -eq 0]; then
172+
url="Unable to get URL - check Firebase console"
173+
fi
174+
175+
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" \
176+
-u $GH_USER:$GH_TOKEN)
177+
178+
if [ $(echo $pr_response | jq length) -eq 0 ]; then
179+
echo "No PR found to update"
180+
else
181+
pr_comment_url=$(echo $pr_response | jq -r ".[]._links.comments.href")
182+
fi
183+
184+
curl --location --request POST "$pr_comment_url" \
185+
-u $GH_USER:$GH_TOKEN \
186+
--header 'Content-Type: application/json' \
187+
--data-raw '{"body": "Successfully deployed to Firebase preview channel! Available at: '"$url"'"}'
134188
135189
workflows:
136-
version: 2
137190
build_and_deploy:
138191
jobs:
139192
- install_deps
@@ -159,6 +212,15 @@ workflows:
159212
requires:
160213
- lint
161214
- build_prod_with_version
215+
- deploy_to_firebase_preview:
216+
filters:
217+
branches:
218+
ignore:
219+
- master
220+
- staging
221+
requires:
222+
- lint
223+
- build_dev
162224
- deploy_to_firebase_stg:
163225
filters:
164226
branches:
@@ -169,7 +231,7 @@ workflows:
169231
daily:
170232
triggers:
171233
- schedule:
172-
cron: '55 20 * * *'
234+
cron: "55 20 * * *"
173235
filters:
174236
branches:
175237
only:
@@ -186,3 +248,4 @@ workflows:
186248
requires:
187249
- lint
188250
- build_prod_no_version
251+
# VS Code Extension Version: 1.5.0

0 commit comments

Comments
 (0)