Skip to content

Commit a4b23a6

Browse files
committed
deploy versioned netlify builds from gh-action
1 parent d8fd4b1 commit a4b23a6

File tree

5 files changed

+134
-24
lines changed

5 files changed

+134
-24
lines changed

.github/workflows/build.yml

+112-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,41 @@ name: Build
33
on: [push]
44

55
jobs:
6+
config:
7+
runs-on: ubuntu-latest
8+
outputs:
9+
canUseSauce: ${{ steps.check_sauce_access.outputs.result == 'true' }}
10+
tag: ${{ steps.extract_tag.outputs.result }}
11+
isMainBranch: ${{ github.ref == 'refs/heads/master' }}
12+
steps:
13+
- name: check sauce access
14+
id: check_sauce_access
15+
run: |
16+
if ! [[ -z "$SAUCE_USERNAME" ]] && ! [[ -z "$SAUCE_ACCESS_KEY" ]]; then
17+
echo "::set-output name=result::true"
18+
fi
19+
env:
20+
CI: true
21+
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
22+
SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
23+
- name: extract tag
24+
id: extract_tag
25+
uses: actions/github-script@v3
26+
with:
27+
script: |
28+
const prefix = '/refs/tags/';
29+
const ref = context.payload.ref;
30+
return ref.startsWith(prefix) ? ref.substring(prefix.length) : '';
31+
result-encoding: string
32+
633
build:
34+
needs: config
735
runs-on: ubuntu-latest
836

937
steps:
1038
- uses: actions/checkout@v2
39+
with:
40+
fetch-depth: 0
1141

1242
- name: cache node_modules
1343
uses: actions/cache@v2
@@ -26,9 +56,21 @@ jobs:
2656
with:
2757
node-version: "12"
2858

29-
- name: install and build
59+
- name: install
3060
run: |
3161
npm ci
62+
env:
63+
CI: true
64+
65+
- name: set version
66+
run: |
67+
node ./scripts/set-package-version.js
68+
env:
69+
CI: true
70+
TAG: ${{ needs.config.outputs.tag }}
71+
72+
- name: build
73+
run: |
3274
npm run lint
3375
npm run type-check
3476
npm run build
@@ -76,15 +118,70 @@ jobs:
76118
with:
77119
name: build
78120

79-
- name: run unit tests
121+
- name: install
80122
run: |
81123
npm ci
124+
env:
125+
CI: true
126+
127+
- name: run unit tests
128+
run: |
82129
npm run test:unit
83130
env:
84131
CI: true
85132

133+
netlify:
134+
needs: [config, test_unit]
135+
if: needs.config.outputs.tag || needs.config.outputs.isMainBranch == 'true'
136+
runs-on: ubuntu-latest
137+
steps:
138+
- uses: actions/checkout@v2
139+
140+
- name: cache node_modules
141+
uses: actions/cache@v2
142+
env:
143+
cache-name: cache-node-modules
144+
with:
145+
path: ~/.npm
146+
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
147+
restore-keys: |
148+
${{ runner.os }}-build-${{ env.cache-name }}-
149+
${{ runner.os }}-build-
150+
${{ runner.os }}-
151+
152+
- name: use Node.js
153+
uses: actions/setup-node@v1
154+
with:
155+
node-version: "12"
156+
157+
- name: download build
158+
uses: actions/download-artifact@v2
159+
with:
160+
name: build
161+
162+
- name: install
163+
run: |
164+
npm ci
165+
env:
166+
CI: true
167+
168+
- name: build netlify
169+
run: |
170+
./scripts/build-netlify.sh
171+
env:
172+
CI: true
173+
174+
- name: deploy netlify
175+
run: |
176+
./scripts/deploy-netlify.sh
177+
env:
178+
CI: true
179+
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }}
180+
NETLIFY_ACCESS_TOKEN: ${{ secrets.NETLIFY_ACCESS_TOKEN }}
181+
86182
test_functional_required:
87-
needs: test_unit
183+
needs: [config, test_unit]
184+
if: needs.config.outputs.canUseSauce == 'true'
88185
runs-on: ubuntu-latest
89186
strategy:
90187
fail-fast: true
@@ -129,9 +226,14 @@ jobs:
129226
accessKey: ${{ secrets.SAUCE_ACCESS_KEY }}
130227
tunnelIdentifier: ${{ github.run_id }}-${{ matrix.config }}
131228

132-
- name: run functional tests
229+
- name: install
133230
run: |
134231
npm ci
232+
env:
233+
CI: true
234+
235+
- name: run functional tests
236+
run: |
135237
npm run test:func
136238
env:
137239
CI: true
@@ -218,9 +320,14 @@ jobs:
218320
accessKey: ${{ secrets.SAUCE_ACCESS_KEY }}
219321
tunnelIdentifier: ${{ github.run_id }}-${{ matrix.config }}
220322

221-
- name: run functional tests
323+
- name: install
222324
run: |
223325
npm ci
326+
env:
327+
CI: true
328+
329+
- name: run functional tests
330+
run: |
224331
npm run test:func
225332
env:
226333
CI: true

netlify.toml

+1-5
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@
22

33
[build]
44
publish = "netlify"
5-
command = "./scripts/travis.sh"
5+
command = "./scripts/netlify.sh"
66

77
[build.environment]
88
NODE_VERSION = "lts/*"
9-
TRAVIS_MODE = "netlifyBranch"
10-
11-
[context.deploy-preview.environment]
12-
TRAVIS_MODE = "netlifyPr"

scripts/deploy-netlify.sh

+1-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,9 @@
11
#!/bin/bash
22
set -e
33

4-
# GITHUB_TOKEN and NETLIFY_ACCESS_TOKEN set in travis
5-
6-
# ensure we have fetched origin/master
7-
git remote set-branches origin master
8-
git fetch
4+
# GITHUB_TOKEN and NETLIFY_ACCESS_TOKEN required
95

106
currentCommit=$(git rev-parse HEAD)
11-
masterLatestCommit=$(git rev-parse origin/master)
127

138
id=$currentCommit
149
root="./netlify"

scripts/netlify.sh

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/bin/bash
2+
set -e
3+
4+
if [[ $(git rev-parse --is-shallow-repository) = "true" ]]; then
5+
# make sure everything is fetched
6+
git fetch --unshallow
7+
fi
8+
9+
npm ci
10+
node ./scripts/set-package-version.js
11+
npm run lint
12+
npm run type-check
13+
npm run build:ci
14+
npm run docs
15+
./scripts/build-netlify.sh

scripts/set-package-version.js

+5-8
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ const versionParser = require('./version-parser.js');
55
const packageJson = require('../package.json');
66
const { isValidStableVersion, incrementPatch } = require('./version-parser.js');
77

8-
const TRAVIS_MODE = process.env.TRAVIS_MODE;
98
const latestVersion = getLatestVersionTag();
109
let newVersion = '';
1110

1211
try {
13-
if (TRAVIS_MODE === 'release') {
12+
if (process.env.TAG) {
1413
// write the version field in the package json to the version in the git tag
15-
const tag = process.env.TRAVIS_TAG;
14+
const tag = process.env.TAG;
1615
if (!versionParser.isValidVersion(tag)) {
1716
throw new Error(`Unsupported tag for release: "${tag}"`);
1817
}
@@ -25,7 +24,7 @@ try {
2524
// 1.2.3-0.aaalpha.custom.0.alpha.503 => now lower than 1.2.3-0.alpha.501
2625
throw new Error(`It's possible that "${newVersion}" has a lower precedense than an existing alpha version which is not allowed.`);
2726
}
28-
} else if (TRAVIS_MODE === 'releaseAlpha' || TRAVIS_MODE === 'netlifyPr' || TRAVIS_MODE === 'netlifyBranch') {
27+
} else {
2928
// bump patch in version from latest git tag
3029
let intermediateVersion = latestVersion;
3130
const isStable = isValidStableVersion(intermediateVersion);
@@ -40,15 +39,13 @@ try {
4039
// remove v
4140
intermediateVersion = intermediateVersion.substring(1);
4241

43-
const suffix = TRAVIS_MODE === 'netlifyPr'
42+
const suffix = process.env.NETLIFY && process.env.CONTEXT === 'deploy-preview'
4443
? `pr.${process.env.REVIEW_ID/* set by netlify */}.${getCommitHash().substr(0, 8)}`
45-
: TRAVIS_MODE === 'netlifyBranch'
44+
: process.env.NETLIFY && process.env.CONTEXT === 'branch-deploy'
4645
? `branch.${process.env.BRANCH/* set by netlify */.replace(/[^a-zA-Z0-9]/g, '-')}.${getCommitHash().substr(0, 8)}`
4746
: `0.alpha.${getCommitNum()}`;
4847

4948
newVersion = `${intermediateVersion}${isStable ? '-' : '.'}${suffix}`;
50-
} else {
51-
throw new Error('Unsupported travis mode: ' + TRAVIS_MODE);
5249
}
5350

5451
if (!versionParser.isGreaterOrEqual(newVersion, latestVersion)) {

0 commit comments

Comments
 (0)