Skip to content

Commit 294d635

Browse files
committed
chore(ci): Move off Travis and use github actions for releases
1 parent 92185b0 commit 294d635

10 files changed

+202
-36
lines changed

.github/workflows/build-and-test.yml

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
name: Build and test
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
branches: [master]
8+
9+
jobs:
10+
build-and-test:
11+
runs-on: ubuntu-latest
12+
strategy:
13+
matrix:
14+
node-version: [10.x, 12.x, 14.x]
15+
16+
steps:
17+
- uses: actions/checkout@v2
18+
- name: Use Node.js ${{ matrix.node-version }}
19+
uses: actions/setup-node@v1
20+
with:
21+
node-version: ${{ matrix.node-version }}
22+
- run: script/ci/build-and-test.sh
23+
env:
24+
NODE_VERSION: ${{ matrix.node-version }}

.github/workflows/publish.yml

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
name: Publish and release
2+
3+
on:
4+
repository_dispatch:
5+
types:
6+
- release-triggered
7+
8+
jobs:
9+
release:
10+
runs-on: ubuntu-latest
11+
steps:
12+
- uses: actions/checkout@v2
13+
with:
14+
fetch-depth: 0
15+
- uses: actions/setup-node@v1
16+
with:
17+
node-version: 12
18+
registry-url: 'https://registry.npmjs.org'
19+
- id: publish
20+
run: script/ci/release.sh
21+
env:
22+
NODE_AUTH_TOKEN: ${{secrets.NPM_AUTOMATION_TOKEN}}
23+
- name: Create Release
24+
id: create_release
25+
uses: actions/create-release@v1
26+
env:
27+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
28+
with:
29+
tag_name: v${{ steps.publish.outputs.version }}
30+
release_name: Release v${{ steps.publish.outputs.version }}
31+
body: ${{steps.publish.outputs.notes}}
32+
draft: false
33+
prerelease: false

.travis.yml

-36
This file was deleted.

script/ci/build-and-test.sh

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash -eu
2+
3+
npm ci --ignore-scripts
4+
npm test
5+
npm run download-checksums

script/ci/lib/create_npmrc_file.sh

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/bash -e
2+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)" # Figure out where the script is running
3+
. "$SCRIPT_DIR"/robust-bash.sh
4+
5+
require_env_var NODE_AUTH_TOKEN
6+
7+
set +x #Don't echo the NPM key
8+
9+
NPMRC_FILE=.npmrc
10+
echo "@pact-foundation:registry=https://registry.npmjs.org/" > $NPMRC_FILE
11+
echo "//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}" >> $NPMRC_FILE
12+
echo "//registry.npmjs.org/:username=pact-foundation" >> $NPMRC_FILE
13+
echo "//registry.npmjs.org/:[email protected]" >> $NPMRC_FILE
14+
echo "//registry.npmjs.org/:always-auth=true" >> $NPMRC_FILE
15+
16+
set -x

script/ci/lib/get-version.sh

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#!/bin/bash -eu
2+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)" # Figure out where the script is running
3+
. "$SCRIPT_DIR"/robust-bash.sh
4+
5+
require_binary grep
6+
7+
VERSION="$(grep '\"version\"' package.json | grep -E -o "([0-9\.]+(-[a-z\.0-9]+)?)")"
8+
echo "$VERSION"

script/ci/lib/publish.sh

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#!/bin/bash -eu
2+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)" # Figure out where the script is running
3+
. "$SCRIPT_DIR"/robust-bash.sh
4+
5+
require_binary npm
6+
7+
VERSION="$("$SCRIPT_DIR/get-version.sh")"
8+
9+
echo "--> Preparing npmrc file"
10+
"$SCRIPT_DIR"/create_npmrc_file.sh
11+
12+
echo "--> Releasing version ${VERSION}"
13+
14+
echo "--> Releasing artifacts"
15+
echo " Publishing pact-node@${VERSION}..."
16+
npm publish --access public --tag latest
17+
echo " done!"

script/ci/lib/robust-bash.sh

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#!/bin/bash -eu
2+
if [ -z "${LIB_ROBUST_BASH_SH:-}" ]; then
3+
LIB_ROBUST_BASH_SH=included
4+
5+
function error {
6+
echo "${1:-}"
7+
}
8+
9+
function log {
10+
echo "🔵 ${1:-}"
11+
}
12+
13+
# Check to see that we have a required binary on the path
14+
function require_binary {
15+
if [ -z "${1:-}" ]; then
16+
error "${FUNCNAME[0]} requires an argument"
17+
exit 1
18+
fi
19+
20+
if ! [ -x "$(command -v "$1")" ]; then
21+
error "The required executable '$1' is not on the path."
22+
exit 1
23+
fi
24+
}
25+
26+
function require_env_var {
27+
var_name="${1:-}"
28+
if [ -z "${!var_name:-}" ]; then
29+
error "The required environment variable ${var_name} is empty"
30+
if [ ! -z "${2:-}" ]; then
31+
echo " - $2"
32+
fi
33+
exit 1
34+
fi
35+
}
36+
fi

script/ci/release.sh

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/bin/bash -eu
2+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")"; pwd)" # Figure out where the script is running
3+
. "$SCRIPT_DIR"/lib/robust-bash.sh
4+
5+
require_env_var CI "This script must be run from CI. If you are running locally, note that it stamps your repo git settings."
6+
require_env_var GITHUB_ACTOR
7+
require_env_var NODE_AUTH_TOKEN
8+
9+
# Setup git for github actions
10+
git config user.email "${GITHUB_ACTOR}@users.noreply.github.com"
11+
git config user.name "${GITHUB_ACTOR}"
12+
13+
# It's easier to read the release notes
14+
# from the standard version tool before it runs
15+
RELEASE_NOTES="$(npx standard-version --dry-run | awk 'BEGIN { flag=0 } /^---$/ { if (flag == 0) { flag=1 } else { flag=2 }; next } flag == 1')"
16+
# Don't release if there are no changes
17+
if [ "$(echo "$RELEASE_NOTES" | wc -l)" -eq 1 ] ; then
18+
echo "ERROR: This release would have no release notes. Does it include changes?"
19+
echo " - You must have at least one fix / feat commit to generate release notes"
20+
echo "*** STOPPING RELEASE PROCESS ***"
21+
exit 1
22+
fi
23+
# This is github actions' method for emitting multi-line values
24+
RELEASE_NOTES="${RELEASE_NOTES//'%'/'%25'}"
25+
RELEASE_NOTES="${RELEASE_NOTES//$'\n'/'%0A'}"
26+
RELEASE_NOTES="${RELEASE_NOTES//$'\r'/'%0D'}"
27+
echo "::set-output name=notes::$RELEASE_NOTES"
28+
29+
"$SCRIPT_DIR"/build-and-test.sh
30+
npm run release
31+
32+
# Emit version to next step
33+
VERSION="$("$SCRIPT_DIR/lib/get-version.sh")"
34+
echo "::set-output name=version::$VERSION"
35+
36+
"$SCRIPT_DIR"/lib/publish.sh
37+
38+
# Push the new commit back to the repo.
39+
git push --follow-tags

script/trigger-release.sh

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/bin/bash
2+
3+
# Script to trigger release of the repository
4+
# Requires a Github API token with repo scope stored in the
5+
# environment variable GITHUB_ACCESS_TOKEN_FOR_PF_RELEASES
6+
# Borrowed from Beth Skurrie's excellent script at
7+
# https://github.com/pact-foundation/pact-ruby/blob/master/script/trigger-release.sh
8+
9+
: "${GITHUB_ACCESS_TOKEN_FOR_PF_RELEASES:?Please set environment variable GITHUB_ACCESS_TOKEN_FOR_PF_RELEASES}"
10+
11+
repository_slug=$(git remote get-url $(git remote show) | cut -d':' -f2 | sed 's/\.git//')
12+
13+
output=$(curl -v -X POST https://api.github.com/repos/${repository_slug}/dispatches \
14+
-H 'Accept: application/vnd.github.everest-preview+json' \
15+
-H "Authorization: Bearer $GITHUB_ACCESS_TOKEN_FOR_PF_RELEASES" \
16+
-d "{\"event_type\": \"release-triggered\"}" 2>&1)
17+
18+
if ! echo "${output}" | grep "HTTP\/1.1 204" > /dev/null; then
19+
echo "$output" | sed "s/${GITHUB_ACCESS_TOKEN_FOR_PF_RELEASES}/********/g"
20+
echo "Failed to trigger release"
21+
exit 1
22+
else
23+
echo "Release workflow triggered"
24+
fi

0 commit comments

Comments
 (0)