Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add v4-publish-to-npm workflow #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions .github/workflows/v4-publish-to-npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
name: Push to NPM

on:
workflow_call:
inputs:
NODE_VERSION:
required: false
type: string
default: 18
# If you want to push a tag to Github before publishing to npm, set this to a non-empty string.
GITHUB_TAG_PREFIX:
required: false
type: string
default: ''
secrets:
NPM_TOKEN_WRITE:
required: true

# Ensure only a single instance of this workflow is running.
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build-and-publish-to-npm:
environment: master
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Use Node.js
uses: actions/setup-node@v3
with:
node-version: ${{ inputs.NODE_VERSION }}
registry-url: https://registry.npmjs.org
cache: 'npm'

- name: Install
run: |
git config --global url."https://github.com/".insteadOf ssh://[email protected]/
git config --global url."https://".insteadOf git://
npm ci

- name: Build
run: npm run build

- name: Get Version and Name
run: |
VERSION=$(cat package.json | jq -r '.version')
NAME=$(cat package.json | jq -r '.name')
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "NAME=$NAME" >> $GITHUB_ENV

- name: Check If Already Published
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN_WRITE }}
VERSION: ${{ env.VERSION }}
NAME: ${{ env.NAME }}
run: |
if exists=$(test -n "$(npm info $NAME@$VERSION)"); then
echo "PACKAGE_EXISTS=true" >> $GITHUB_ENV
echo "Skipping Publish, package $NAME@$VERSION already published"
fi

- name: Push Tag to Github
if: ${{ !env.PACKAGE_EXISTS && inputs.GITHUB_TAG_PREFIX != '' }}
env:
VERSION: ${{ env.VERSION }}
GITHUB_TAG_PREFIX: ${{ inputs.GITHUB_TAG_PREFIX }}
run: |
git config --global user.email "[email protected]"
git config --global user.name "github_actions"
git tag ${GITHUB_TAG_PREFIX}${VERSION} ${{ github.sha }}
git push --tags origin

- name: Publish to npm
id: publish-npm
if: ${{ !env.PACKAGE_EXISTS }}
env:
VERSION: ${{ env.VERSION }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN_WRITE }}
run: |
if
if [[ $VERSION =~ ^[[:digit:]]+.[[:digit:]]+.[[:digit:]]+-([[:alpha:]]+).[[:digit:]]+$ ]]; then
echo "Publishing $NAME@$VERSION as a pre-release"
npm publish --tag ${BASH_REMATCH[1]}
else
echo "Publishing $NAME@$VERSION as a stable release"
npm publish
fi