Skip to content

Commit

Permalink
add CI workflows for GitHub Actions; add automated release script
Browse files Browse the repository at this point in the history
  • Loading branch information
mojavelinux committed Oct 29, 2023
1 parent ca449f9 commit 38ae1ac
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 22 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: CI
on:
push:
branches: [main]
pull_request:
branches: [main]
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm t
30 changes: 30 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Release
run-name: ${{ github.workflow }} ${{ github.event.inputs.release-version }}
on:
workflow_dispatch:
inputs:
release-version:
description: Enter version to release (e.g., 1.0.1).
required: false
jobs:
perform:
if: github.repository_owner == 'bespokejs' && github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
environment: releases
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install Node.js
uses: actions/setup-node@v3
with:
node-version: '16'
- name: Install dependencies
run: npm ci
- name: Run tests
run: npm t
- name: Set up release environment
run: |
echo RELEASE_VERSION=${{ github.event.inputs.release-version }} >> $GITHUB_ENV
echo RELEASE_NPM_TOKEN=${{ secrets[format('NPM_TOKEN_{0}', github.actor)] }} >> $GITHUB_ENV
- name: Build, tag, and publish npm package
run: ./release.sh
22 changes: 0 additions & 22 deletions .travis.yml

This file was deleted.

60 changes: 60 additions & 0 deletions release.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#!/bin/bash

# required packages (for ubuntu:kinetic): curl git jq nodejs npm

if [ -z "$RELEASE_NPM_TOKEN" ]; then
echo No npm token specified for publishing to npmjs.com. Stopping release.
exit 1
fi
export RELEASE_BRANCH=${GITHUB_REF_NAME:-main}
if [ ! -v RELEASE_USER ]; then
export RELEASE_USER=$GITHUB_ACTOR
fi
RELEASE_GIT_NAME=$(curl -s https://api.github.com/users/$RELEASE_USER | jq -r .name)
RELEASE_GIT_EMAIL=$RELEASE_USER@users.noreply.github.com

# RELEASE_VERSION can be a version number (exact) or increment keyword (next in sequence)
if [ -z "$RELEASE_VERSION" ]; then RELEASE_VERSION=prerelease; fi
if [ -z "$RELEASE_NPM_TAG" ]; then
if case $RELEASE_VERSION in major|minor|patch) ;; *) false;; esac; then
RELEASE_NPM_TAG=latest
elif case $RELEASE_VERSION in pre*) ;; *) false;; esac; then
RELEASE_NPM_TAG=testing
elif [ "$RELEASE_VERSION" != "${RELEASE_VERSION/-/}" ] && [ "${RELEASE_VERSION#*-}" != "stable" ]; then
RELEASE_NPM_TAG=testing
else
RELEASE_NPM_TAG=latest
fi
fi

# configure git to push changes
git config --local user.name "$RELEASE_GIT_NAME"
git config --local user.email "$RELEASE_GIT_EMAIL"

# configure npm client for publishing
echo -e "//registry.npmjs.org/:_authToken=$RELEASE_NPM_TOKEN" > $HOME/.npmrc

# release!
(
set -e
npm version --no-git-tag-version $RELEASE_VERSION
RELEASE_VERSION=$(npm exec -c 'echo -n $npm_package_version')
if case $RELEASE_VERSION in 1.0.0-*) ;; *) false;; esac; then
RELEASE_NPM_TAG=latest
fi
git commit -a -m "release $RELEASE_VERSION [no ci]"
git tag -m "version $RELEASE_VERSION" v$RELEASE_VERSION
git push origin $(git describe --tags --exact-match)
npm publish --access public --tag $RELEASE_NPM_TAG
git push origin $RELEASE_BRANCH
)

exit_code=$?

# nuke npm settings
rm -f $HOME/.npmrc

# check for any uncommitted files
git status -s -b

exit $exit_code

0 comments on commit 38ae1ac

Please sign in to comment.