Skip to content

Commit

Permalink
deploy versioned netlify builds from gh-action
Browse files Browse the repository at this point in the history
  • Loading branch information
tjenkinson committed Oct 31, 2020
1 parent d8fd4b1 commit a4b23a6
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 24 deletions.
117 changes: 112 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,41 @@ name: Build
on: [push]

jobs:
config:
runs-on: ubuntu-latest
outputs:
canUseSauce: ${{ steps.check_sauce_access.outputs.result == 'true' }}
tag: ${{ steps.extract_tag.outputs.result }}
isMainBranch: ${{ github.ref == 'refs/heads/master' }}
steps:
- name: check sauce access
id: check_sauce_access
run: |
if ! [[ -z "$SAUCE_USERNAME" ]] && ! [[ -z "$SAUCE_ACCESS_KEY" ]]; then
echo "::set-output name=result::true"
fi
env:
CI: true
SAUCE_USERNAME: ${{ secrets.SAUCE_USERNAME }}
SAUCE_ACCESS_KEY: ${{ secrets.SAUCE_ACCESS_KEY }}
- name: extract tag
id: extract_tag
uses: actions/github-script@v3
with:
script: |
const prefix = '/refs/tags/';
const ref = context.payload.ref;
return ref.startsWith(prefix) ? ref.substring(prefix.length) : '';
result-encoding: string

build:
needs: config
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
with:
fetch-depth: 0

- name: cache node_modules
uses: actions/cache@v2
Expand All @@ -26,9 +56,21 @@ jobs:
with:
node-version: "12"

- name: install and build
- name: install
run: |
npm ci
env:
CI: true

- name: set version
run: |
node ./scripts/set-package-version.js
env:
CI: true
TAG: ${{ needs.config.outputs.tag }}

- name: build
run: |
npm run lint
npm run type-check
npm run build
Expand Down Expand Up @@ -76,15 +118,70 @@ jobs:
with:
name: build

- name: run unit tests
- name: install
run: |
npm ci
env:
CI: true

- name: run unit tests
run: |
npm run test:unit
env:
CI: true

netlify:
needs: [config, test_unit]
if: needs.config.outputs.tag || needs.config.outputs.isMainBranch == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- name: cache node_modules
uses: actions/cache@v2
env:
cache-name: cache-node-modules
with:
path: ~/.npm
key: ${{ runner.os }}-build-${{ env.cache-name }}-${{ hashFiles('**/package-lock.json') }}
restore-keys: |
${{ runner.os }}-build-${{ env.cache-name }}-
${{ runner.os }}-build-
${{ runner.os }}-
- name: use Node.js
uses: actions/setup-node@v1
with:
node-version: "12"

- name: download build
uses: actions/download-artifact@v2
with:
name: build

- name: install
run: |
npm ci
env:
CI: true

- name: build netlify
run: |
./scripts/build-netlify.sh
env:
CI: true

- name: deploy netlify
run: |
./scripts/deploy-netlify.sh
env:
CI: true
GITHUB_TOKEN: ${{ secrets.CI_GITHUB_TOKEN }}
NETLIFY_ACCESS_TOKEN: ${{ secrets.NETLIFY_ACCESS_TOKEN }}

test_functional_required:
needs: test_unit
needs: [config, test_unit]
if: needs.config.outputs.canUseSauce == 'true'
runs-on: ubuntu-latest
strategy:
fail-fast: true
Expand Down Expand Up @@ -129,9 +226,14 @@ jobs:
accessKey: ${{ secrets.SAUCE_ACCESS_KEY }}
tunnelIdentifier: ${{ github.run_id }}-${{ matrix.config }}

- name: run functional tests
- name: install
run: |
npm ci
env:
CI: true

- name: run functional tests
run: |
npm run test:func
env:
CI: true
Expand Down Expand Up @@ -218,9 +320,14 @@ jobs:
accessKey: ${{ secrets.SAUCE_ACCESS_KEY }}
tunnelIdentifier: ${{ github.run_id }}-${{ matrix.config }}

- name: run functional tests
- name: install
run: |
npm ci
env:
CI: true

- name: run functional tests
run: |
npm run test:func
env:
CI: true
Expand Down
6 changes: 1 addition & 5 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

[build]
publish = "netlify"
command = "./scripts/travis.sh"
command = "./scripts/netlify.sh"

[build.environment]
NODE_VERSION = "lts/*"
TRAVIS_MODE = "netlifyBranch"

[context.deploy-preview.environment]
TRAVIS_MODE = "netlifyPr"
7 changes: 1 addition & 6 deletions scripts/deploy-netlify.sh
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
#!/bin/bash
set -e

# GITHUB_TOKEN and NETLIFY_ACCESS_TOKEN set in travis

# ensure we have fetched origin/master
git remote set-branches origin master
git fetch
# GITHUB_TOKEN and NETLIFY_ACCESS_TOKEN required

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

id=$currentCommit
root="./netlify"
Expand Down
15 changes: 15 additions & 0 deletions scripts/netlify.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/bash
set -e

if [[ $(git rev-parse --is-shallow-repository) = "true" ]]; then
# make sure everything is fetched
git fetch --unshallow
fi

npm ci
node ./scripts/set-package-version.js
npm run lint
npm run type-check
npm run build:ci
npm run docs
./scripts/build-netlify.sh
13 changes: 5 additions & 8 deletions scripts/set-package-version.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ const versionParser = require('./version-parser.js');
const packageJson = require('../package.json');
const { isValidStableVersion, incrementPatch } = require('./version-parser.js');

const TRAVIS_MODE = process.env.TRAVIS_MODE;
const latestVersion = getLatestVersionTag();
let newVersion = '';

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

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

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

if (!versionParser.isGreaterOrEqual(newVersion, latestVersion)) {
Expand Down

0 comments on commit a4b23a6

Please sign in to comment.