Skip to content

Commit c4a5f8d

Browse files
committed
build: deploy netlify on merge to master (nrwl#72)
1 parent b718180 commit c4a5f8d

File tree

6 files changed

+2318
-74
lines changed

6 files changed

+2318
-74
lines changed

.circleci/config.yml

Lines changed: 62 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,21 @@ defaults: &defaults
1212
set_env: &set_env
1313
name: Setup Environment Variables
1414
command: |
15-
echo 'export CIRCLE_PR_NUMBER="${CIRCLE_PR_NUMBER:-${CIRCLE_PULL_REQUEST##*/}}"' >> $BASH_ENV
16-
source $BASH_ENV
17-
echo "export CIRCLE_PR_BASE_SHA=`curl -s https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/pulls/${CIRCLE_PR_NUMBER} | jq -r '.base.sha'`" >> $BASH_ENV
18-
echo 'export AFFECTED_ARGS="--base ${CIRCLE_PR_BASE_SHA}"' >> $BASH_ENV
15+
if [[ $CIRCLE_PULL_REQUEST ]]
16+
then
17+
echo 'Fetching Base Commit from GitHub'
18+
echo 'export CIRCLE_PR_NUMBER="${CIRCLE_PR_NUMBER:-${CIRCLE_PULL_REQUEST##*/}}"' >> $BASH_ENV
19+
source $BASH_ENV
20+
echo "export CIRCLE_PR_BASE_SHA=`curl -s https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/pulls/${CIRCLE_PR_NUMBER} | jq -r '.base.sha'`" >> $BASH_ENV
21+
echo 'export AFFECTED_ARGS="--base ${CIRCLE_PR_BASE_SHA}"' >> $BASH_ENV
22+
else
23+
echo 'Fetching Base Commit from Deploy Cache'
24+
if [[ ! -f dist/last-deploy.txt ]]
25+
then
26+
mkdir dist && git rev-parse HEAD~1 > dist/last-deploy.txt
27+
fi
28+
echo 'export AFFECTED_ARGS="--base $(cat dist/last-deploy.txt)"' >> $BASH_ENV
29+
fi
1930
source $BASH_ENV
2031
echo $AFFECTED_ARGS
2132
@@ -25,6 +36,9 @@ yarn_cache: &yarn_cache
2536
# fallback to using the latest cache if no exact match is found
2637
- node-deps-node12-
2738

39+
deploy_cache: &deploy_cache
40+
key: last-deploy-sha
41+
2842
yarn_install: &yarn_install
2943
name: Install Dependencies
3044
command: yarn install --frozen-lockfile --non-interactive
@@ -34,8 +48,6 @@ jobs:
3448
<<: *defaults
3549
steps:
3650
- checkout
37-
- run:
38-
<<: *set_env
3951
- restore_cache:
4052
<<: *yarn_cache
4153
- run:
@@ -49,6 +61,8 @@ jobs:
4961
<<: *defaults
5062
steps:
5163
- checkout
64+
- restore_cache:
65+
<<: *deploy_cache
5266
- run:
5367
<<: *set_env
5468
- restore_cache:
@@ -58,6 +72,8 @@ jobs:
5872
<<: *defaults
5973
steps:
6074
- checkout
75+
- restore_cache:
76+
<<: *deploy_cache
6177
- run:
6278
<<: *set_env
6379
- restore_cache:
@@ -68,17 +84,25 @@ jobs:
6884
<<: *defaults
6985
steps:
7086
- checkout
87+
- restore_cache:
88+
<<: *deploy_cache
7189
- run:
7290
<<: *set_env
7391
- restore_cache:
7492
<<: *yarn_cache
7593
- run: yarn affected:build -- ${AFFECTED_ARGS} --parallel -- --configuration production
94+
- save_cache:
95+
key: build-{{ .Environment.CIRCLE_WORKFLOW_ID }}
96+
paths:
97+
- dist
7698
- store_artifacts:
7799
path: dist
78100
test:
79101
<<: *defaults
80102
steps:
81103
- checkout
104+
- restore_cache:
105+
<<: *deploy_cache
82106
- run:
83107
<<: *set_env
84108
- restore_cache:
@@ -88,6 +112,8 @@ jobs:
88112
<<: *defaults
89113
steps:
90114
- checkout
115+
- restore_cache:
116+
<<: *deploy_cache
91117
- run:
92118
<<: *set_env
93119
- restore_cache:
@@ -96,6 +122,25 @@ jobs:
96122
- store_artifacts:
97123
path: dist/cypress
98124

125+
deploy:
126+
<<: *defaults
127+
steps:
128+
- checkout
129+
- restore_cache:
130+
<<: *deploy_cache
131+
- run:
132+
<<: *set_env
133+
- restore_cache:
134+
<<: *yarn_cache
135+
- restore_cache:
136+
key: build-{{ .Environment.CIRCLE_WORKFLOW_ID }}
137+
- run: yarn affected -- --target deploy ${AFFECTED_ARGS}
138+
- run: git rev-parse HEAD > dist/last-deploy.txt
139+
- save_cache:
140+
key: last-deploy-sha
141+
paths:
142+
- dist/last-deploy.txt
143+
99144
workflows:
100145
version: 2
101146
pr_check:
@@ -116,3 +161,14 @@ workflows:
116161
- e2e:
117162
requires:
118163
- install
164+
- deploy:
165+
filters:
166+
branches:
167+
only:
168+
- master
169+
requires:
170+
- check-formatting
171+
- lint
172+
- test
173+
- build
174+
- e2e

angular.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,16 @@
103103
"tsConfig": "apps/products/tsconfig.spec.json",
104104
"setupFile": "apps/products/src/test-setup.ts"
105105
}
106+
},
107+
"deploy": {
108+
"builder": "@nrwl/workspace:run-commands",
109+
"options": {
110+
"commands": [
111+
{
112+
"command": "npx ts-node --project tools/tsconfig.tools.json tools/scripts/deploy --siteName nrwl-nx-examples-products --outputPath dist/apps/products"
113+
}
114+
]
115+
}
106116
}
107117
}
108118
},
@@ -221,6 +231,16 @@
221231
"tsConfig": "apps/cart/tsconfig.spec.json",
222232
"setupFile": "apps/cart/src/test-setup.ts"
223233
}
234+
},
235+
"deploy": {
236+
"builder": "@nrwl/workspace:run-commands",
237+
"options": {
238+
"commands": [
239+
{
240+
"command": "npx ts-node --project tools/tsconfig.tools.json tools/scripts/deploy --siteName nrwl-nx-examples-cart --outputPath dist/apps/cart"
241+
}
242+
]
243+
}
224244
}
225245
}
226246
},

libs/cart/cart-page/src/lib/cart-cart-page/cart-cart-page.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const StyledUl = styled.ul`
2323
margin: auto;
2424
max-width: 900px;
2525
padding: 10px;
26-
26+
2727
@media screen and (max-width: 900px) {
2828
max-width: 100%;
2929
}
@@ -53,13 +53,13 @@ const StyledLi = styled.li`
5353
flex-grow: 1;
5454
margin-left: 50px;
5555
}
56-
56+
5757
@media screen and (max-width: 900px) {
5858
figure {
5959
width: 50px;
6060
height: 50px;
6161
}
62-
62+
6363
.title {
6464
margin-left: 1em;
6565
}

package.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
"@nrwl/angular": "8.4.0",
4747
"core-js": "^2.5.4",
4848
"document-register-element": "1.13.1",
49+
"firebase": ">= 5.5.7 <7",
4950
"normalize.css": "^8.0.1",
5051
"react": "16.8.6",
5152
"react-dom": "16.8.6",
@@ -54,6 +55,7 @@
5455
"zone.js": "^0.9.1"
5556
},
5657
"devDependencies": {
58+
"@angular-devkit/architect": "<0.900 || ^0.900.0-0 || ^9.0.0-0",
5759
"@angular-devkit/build-angular": "^0.801.1",
5860
"@angular/cli": "8.1.1",
5961
"@angular/compiler-cli": "8.2.0-next.2",
@@ -73,9 +75,14 @@
7375
"codelyzer": "~5.0.1",
7476
"cypress": "3.4.0",
7577
"dotenv": "6.2.0",
78+
"firebase-tools": "^7.2.2",
79+
"fuzzy": "^0.1.3",
80+
"inquirer": "^6.2.2",
81+
"inquirer-autocomplete-prompt": "^1.0.1",
7682
"jest": "24.1.0",
7783
"jest-environment-jsdom-fourteen": "^0.1.0",
7884
"jest-preset-angular": "7.0.0",
85+
"netlify": "^2.4.8",
7986
"prettier": "1.16.4",
8087
"ts-jest": "24.0.0",
8188
"ts-node": "~7.0.0",

tools/scripts/deploy.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { existsSync } from 'fs';
2+
import * as minimist from 'minimist';
3+
import * as NetlifyClient from 'netlify';
4+
import { join } from 'path';
5+
6+
const token = process.env.NETLIFY_AUTH_TOKEN;
7+
8+
const argv = minimist(process.argv.slice(2));
9+
10+
const netlifyClient = new NetlifyClient(token);
11+
const root = join(__dirname, '../..');
12+
const outDir = join(root, argv.outputPath);
13+
14+
if (!existsSync(outDir)) {
15+
throw new Error(`${outDir} does not exist`);
16+
}
17+
18+
(async () => {
19+
try {
20+
const sites = await netlifyClient.listSites();
21+
const site = sites.find(s => argv.siteName === s.name);
22+
if (!site) {
23+
throw Error(`Could not find site ${argv.siteName}`);
24+
}
25+
console.log(`Deploying ${argv.siteName} to Netlify...`);
26+
const deployResult = await netlifyClient.deploy(site.id, outDir);
27+
console.log(
28+
`\n🚀 New version of ${argv.siteName} is running at ${
29+
deployResult.deploy.ssl_url
30+
}!\n`
31+
);
32+
} catch (e) {
33+
console.error('Authentication Failure: Invalid Token');
34+
}
35+
})();

0 commit comments

Comments
 (0)