Skip to content

Commit

Permalink
chore(ci): Allow publish by workflow dispatch or PR merge (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
donmccurdy authored Oct 7, 2024
1 parent d77361e commit 8c854da
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 72 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,15 @@ name: CI

on:
push:
branches: [main]
pull_request:
branches: [main]

jobs:
##########################################################################
# Build
##########################################################################

build:
name: Build [${{ matrix.framework }} - Node ${{ matrix.node-version }}]
runs-on: ubuntu-22.04
Expand Down Expand Up @@ -53,3 +60,16 @@ jobs:
yarn dev --port 4000 &
sleep 30
curl http://localhost:4000/
##########################################################################
# Release precheck
##########################################################################

release_precheck:
name: 'Release (precheck)'
needs: build
if: |
github.ref_name == 'main' &&
startsWith(github.event.head_commit.message, 'chore(release)')
uses: ./.github/workflows/release.yml
secrets: inherit
114 changes: 42 additions & 72 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,104 +1,74 @@
name: Release

on:
push:
tags: ['*']
workflow_call:
secrets:
NPM_CARTODB_AUTH_TOKEN:
required: true
workflow_dispatch:

env:
NODE_VERSION: 20
CI: true

jobs:
release:
name: Release [Node.js ${{ matrix.node-version }}]
name: Release
runs-on: ubuntu-22.04

strategy:
matrix:
node-version: [20.x]

env:
CI: true

steps:
- name: Checkout
uses: actions/checkout@v4

##########################################################################
# Build
##########################################################################
# Read version from 'package.json'; git tags are lost on a merged PR.
- name: Read package version
id: version
run: echo "PKG_VERSION=v$(npm pkg get version -w @carto/create-react | jq '.[]' | xargs)" >> $GITHUB_OUTPUT

- name: Set up Node.js ${{ matrix.node-version }}
- name: Set up Node.js ${{ env.NODE_VERSION }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}

- name: Install
run: |
corepack enable
yarn install
- name: Lint
run: yarn lint

- name: Build
run: yarn build
node-version: ${{ env.NODE_VERSION }}

- name: Test
run: yarn test

##########################################################################
# Publish
##########################################################################

- name: Check tag format
id: check-tag-format
uses: nowsprinting/check-version-format-action@v4

- name: Exit on invalid tag format
if: '!steps.check-tag-format.outputs.is_valid'
run: echo "Tag must follow SemVer convention. Aborting." && exit 1

- name: Get release type
id: get-release-type
# Determine whether to tag the release as 'latest' or 'alpha'.
- name: Assign dist tag
id: dist-tag
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const regex = /(alpha|beta)/
const refName = context.ref.replace('refs/tags/', '')
console.log(`Ref tag: ${refName}`)
const releaseTypeMatch = refName.match(regex)
if (!releaseTypeMatch) {
releaseType = 'latest'
const version = '${{ steps.version.outputs.PKG_VERSION }}'
console.log(`version: ${version}`)
if (version.match(/^v\d+\.\d+\.\d+$/)) {
distTag = 'latest'
} else if (version.match(/^v\d+\.\d+\.\d+/)) {
distTag = 'alpha'
} else {
releaseType = releaseTypeMatch[0]
core.setFailed('Version must follow SemVer convention. Aborting.');
}
console.log(`Release type: ${releaseType}`)
return releaseType
console.log(`npm dist tag: ${distTag}`)
return distTag
- name: Set version from tag to environment variable
run: echo "RELEASE_VERSION=${GITHUB_REF#refs/*/}" >> $GITHUB_ENV
- name: Install
run: |
corepack enable
yarn install
- name: Ignore changes to .yarnrc.yml
run: git update-index --assume-unchanged .yarnrc.yml
# Build. Tests are run automatically by `yarn prepublish`.
- name: Build
run: yarn build

- name: Configure yarn to publish packages
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_CARTODB_AUTH_TOKEN }}
NPM_AUTH_TOKEN: ${{ secrets.NPM_CARTODB_AUTH_TOKEN }}
# Ignore changes to .yarnrc.yml, see https://github.com/lerna/lerna/issues/2329
run: |
# Yarn config
git update-index --assume-unchanged .yarnrc.yml
yarn config set npmPublishRegistry "https://registry.npmjs.org/"
yarn config set npmAuthToken "${NODE_AUTH_TOKEN}"
# Create .npmrc file
echo "@carto:registry=https://registry.npmjs.org/" > .npmrc
echo "//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}" >> .npmrc
# Check configuration
npm config get registry
npm view @carto/create-common versions
yarn config set npmAuthToken "${NPM_AUTH_TOKEN}"
- name: Publish package with Lerna
- name: Publish
env:
RELEASE_TYPE: ${{ steps.get-release-type.outputs.result }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_CARTODB_AUTH_TOKEN }}
DIST_TAG: ${{ steps.dist-tag.outputs.result }}
run: |
set -x
yarn lerna publish from-git --yes --dist-tag ${RELEASE_TYPE} --loglevel verbose
yarn lerna publish from-git --yes --dist-tag ${DIST_TAG}

0 comments on commit 8c854da

Please sign in to comment.