From 38ae1ac5b16068e81559efd00609ab9056b61878 Mon Sep 17 00:00:00 2001 From: Dan Allen Date: Sun, 29 Oct 2023 05:38:42 -0600 Subject: [PATCH] add CI workflows for GitHub Actions; add automated release script --- .github/workflows/ci.yml | 23 ++++++++++++++ .github/workflows/release.yml | 30 ++++++++++++++++++ .travis.yml | 22 ------------- release.sh | 60 +++++++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 22 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/release.yml delete mode 100644 .travis.yml create mode 100755 release.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 0000000..00ce8a9 --- /dev/null +++ b/.github/workflows/ci.yml @@ -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 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..649ce9c --- /dev/null +++ b/.github/workflows/release.yml @@ -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 diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index b75f1e6..0000000 --- a/.travis.yml +++ /dev/null @@ -1,22 +0,0 @@ -sudo: false -dist: trusty -git: - depth: 2 -language: node_js -node_js: -- &release_node 4.4 -- 6 -- 7 -- 8 -notifications: - email: - on_success: never - on_failure: change -deploy: - provider: npm - email: ${NPM_EMAIL} - api_key: ${NPM_AUTH_TOKEN} - on: - tags: true - repo: bespokejs/generator-bespoke - node: *release_node diff --git a/release.sh b/release.sh new file mode 100755 index 0000000..ac0b2cb --- /dev/null +++ b/release.sh @@ -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