Skip to content

Commit

Permalink
build: deploy netlify on merge to master (nrwl#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrozenPandaz committed Aug 12, 2019
1 parent b718180 commit c4a5f8d
Show file tree
Hide file tree
Showing 6 changed files with 2,318 additions and 74 deletions.
68 changes: 62 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,21 @@ defaults: &defaults
set_env: &set_env
name: Setup Environment Variables
command: |
echo 'export CIRCLE_PR_NUMBER="${CIRCLE_PR_NUMBER:-${CIRCLE_PULL_REQUEST##*/}}"' >> $BASH_ENV
source $BASH_ENV
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
echo 'export AFFECTED_ARGS="--base ${CIRCLE_PR_BASE_SHA}"' >> $BASH_ENV
if [[ $CIRCLE_PULL_REQUEST ]]
then
echo 'Fetching Base Commit from GitHub'
echo 'export CIRCLE_PR_NUMBER="${CIRCLE_PR_NUMBER:-${CIRCLE_PULL_REQUEST##*/}}"' >> $BASH_ENV
source $BASH_ENV
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
echo 'export AFFECTED_ARGS="--base ${CIRCLE_PR_BASE_SHA}"' >> $BASH_ENV
else
echo 'Fetching Base Commit from Deploy Cache'
if [[ ! -f dist/last-deploy.txt ]]
then
mkdir dist && git rev-parse HEAD~1 > dist/last-deploy.txt
fi
echo 'export AFFECTED_ARGS="--base $(cat dist/last-deploy.txt)"' >> $BASH_ENV
fi
source $BASH_ENV
echo $AFFECTED_ARGS
Expand All @@ -25,6 +36,9 @@ yarn_cache: &yarn_cache
# fallback to using the latest cache if no exact match is found
- node-deps-node12-

deploy_cache: &deploy_cache
key: last-deploy-sha

yarn_install: &yarn_install
name: Install Dependencies
command: yarn install --frozen-lockfile --non-interactive
Expand All @@ -34,8 +48,6 @@ jobs:
<<: *defaults
steps:
- checkout
- run:
<<: *set_env
- restore_cache:
<<: *yarn_cache
- run:
Expand All @@ -49,6 +61,8 @@ jobs:
<<: *defaults
steps:
- checkout
- restore_cache:
<<: *deploy_cache
- run:
<<: *set_env
- restore_cache:
Expand All @@ -58,6 +72,8 @@ jobs:
<<: *defaults
steps:
- checkout
- restore_cache:
<<: *deploy_cache
- run:
<<: *set_env
- restore_cache:
Expand All @@ -68,17 +84,25 @@ jobs:
<<: *defaults
steps:
- checkout
- restore_cache:
<<: *deploy_cache
- run:
<<: *set_env
- restore_cache:
<<: *yarn_cache
- run: yarn affected:build -- ${AFFECTED_ARGS} --parallel -- --configuration production
- save_cache:
key: build-{{ .Environment.CIRCLE_WORKFLOW_ID }}
paths:
- dist
- store_artifacts:
path: dist
test:
<<: *defaults
steps:
- checkout
- restore_cache:
<<: *deploy_cache
- run:
<<: *set_env
- restore_cache:
Expand All @@ -88,6 +112,8 @@ jobs:
<<: *defaults
steps:
- checkout
- restore_cache:
<<: *deploy_cache
- run:
<<: *set_env
- restore_cache:
Expand All @@ -96,6 +122,25 @@ jobs:
- store_artifacts:
path: dist/cypress

deploy:
<<: *defaults
steps:
- checkout
- restore_cache:
<<: *deploy_cache
- run:
<<: *set_env
- restore_cache:
<<: *yarn_cache
- restore_cache:
key: build-{{ .Environment.CIRCLE_WORKFLOW_ID }}
- run: yarn affected -- --target deploy ${AFFECTED_ARGS}
- run: git rev-parse HEAD > dist/last-deploy.txt
- save_cache:
key: last-deploy-sha
paths:
- dist/last-deploy.txt

workflows:
version: 2
pr_check:
Expand All @@ -116,3 +161,14 @@ workflows:
- e2e:
requires:
- install
- deploy:
filters:
branches:
only:
- master
requires:
- check-formatting
- lint
- test
- build
- e2e
20 changes: 20 additions & 0 deletions angular.json
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,16 @@
"tsConfig": "apps/products/tsconfig.spec.json",
"setupFile": "apps/products/src/test-setup.ts"
}
},
"deploy": {
"builder": "@nrwl/workspace:run-commands",
"options": {
"commands": [
{
"command": "npx ts-node --project tools/tsconfig.tools.json tools/scripts/deploy --siteName nrwl-nx-examples-products --outputPath dist/apps/products"
}
]
}
}
}
},
Expand Down Expand Up @@ -221,6 +231,16 @@
"tsConfig": "apps/cart/tsconfig.spec.json",
"setupFile": "apps/cart/src/test-setup.ts"
}
},
"deploy": {
"builder": "@nrwl/workspace:run-commands",
"options": {
"commands": [
{
"command": "npx ts-node --project tools/tsconfig.tools.json tools/scripts/deploy --siteName nrwl-nx-examples-cart --outputPath dist/apps/cart"
}
]
}
}
}
},
Expand Down
6 changes: 3 additions & 3 deletions libs/cart/cart-page/src/lib/cart-cart-page/cart-cart-page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const StyledUl = styled.ul`
margin: auto;
max-width: 900px;
padding: 10px;
@media screen and (max-width: 900px) {
max-width: 100%;
}
Expand Down Expand Up @@ -53,13 +53,13 @@ const StyledLi = styled.li`
flex-grow: 1;
margin-left: 50px;
}
@media screen and (max-width: 900px) {
figure {
width: 50px;
height: 50px;
}
.title {
margin-left: 1em;
}
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"@nrwl/angular": "8.4.0",
"core-js": "^2.5.4",
"document-register-element": "1.13.1",
"firebase": ">= 5.5.7 <7",
"normalize.css": "^8.0.1",
"react": "16.8.6",
"react-dom": "16.8.6",
Expand All @@ -54,6 +55,7 @@
"zone.js": "^0.9.1"
},
"devDependencies": {
"@angular-devkit/architect": "<0.900 || ^0.900.0-0 || ^9.0.0-0",
"@angular-devkit/build-angular": "^0.801.1",
"@angular/cli": "8.1.1",
"@angular/compiler-cli": "8.2.0-next.2",
Expand All @@ -73,9 +75,14 @@
"codelyzer": "~5.0.1",
"cypress": "3.4.0",
"dotenv": "6.2.0",
"firebase-tools": "^7.2.2",
"fuzzy": "^0.1.3",
"inquirer": "^6.2.2",
"inquirer-autocomplete-prompt": "^1.0.1",
"jest": "24.1.0",
"jest-environment-jsdom-fourteen": "^0.1.0",
"jest-preset-angular": "7.0.0",
"netlify": "^2.4.8",
"prettier": "1.16.4",
"ts-jest": "24.0.0",
"ts-node": "~7.0.0",
Expand Down
35 changes: 35 additions & 0 deletions tools/scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { existsSync } from 'fs';
import * as minimist from 'minimist';
import * as NetlifyClient from 'netlify';
import { join } from 'path';

const token = process.env.NETLIFY_AUTH_TOKEN;

const argv = minimist(process.argv.slice(2));

const netlifyClient = new NetlifyClient(token);
const root = join(__dirname, '../..');
const outDir = join(root, argv.outputPath);

if (!existsSync(outDir)) {
throw new Error(`${outDir} does not exist`);
}

(async () => {
try {
const sites = await netlifyClient.listSites();
const site = sites.find(s => argv.siteName === s.name);
if (!site) {
throw Error(`Could not find site ${argv.siteName}`);
}
console.log(`Deploying ${argv.siteName} to Netlify...`);
const deployResult = await netlifyClient.deploy(site.id, outDir);
console.log(
`\n🚀 New version of ${argv.siteName} is running at ${
deployResult.deploy.ssl_url
}!\n`
);
} catch (e) {
console.error('Authentication Failure: Invalid Token');
}
})();
Loading

0 comments on commit c4a5f8d

Please sign in to comment.