Publish Package to npm #34
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Publish Package to npm | |
on: | |
workflow_dispatch: | |
inputs: | |
branch: | |
description: "Git branch to build and publish" | |
required: true | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: write | |
id-token: write | |
steps: | |
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 | |
with: | |
persist-credentials: false | |
ref: ${{ github.event.inputs.branch }} | |
- uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 | |
with: | |
node-version: "22.x" | |
registry-url: "https://registry.npmjs.org" | |
- run: npm install -g npm | |
- run: npm install | |
- run: npm test | |
- name: npm publish | |
run: | | |
version=$(jq -r .version package.json) | |
tag_meta=$(echo "$version" | cut -s -d '-' -f2) | |
# if no meta info on the version (e.g. a '-alpha.1' prefix), publish as a stable release | |
if [[ -z "$tag_meta" ]]; then | |
# get latest version on npm | |
latest=$(npm view @elastic/elasticsearch --json | jq -r '.["dist-tags"].latest') | |
# if $version is higher than the most recently published version, publish as-is | |
if [[ $(yes | npx semver "$version" "$latest" | tail -n1) == "$version" ]]; then | |
npm publish --provenance --access public | |
else | |
# otherwise, publish with "previous" tag | |
npm publish --provenance --access public --tag "previous" | |
fi | |
else | |
# publish as a non-stable release using the meta name (e.g. 'alpha') as the tag | |
tag=$(echo "$tag_meta" | cut -d '.' -f1) | |
npm publish --provenance --access public --tag "$tag" | |
fi | |
env: | |
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | |
- name: Publish version on GitHub | |
run: | | |
version=$(jq -r .version package.json) | |
tag_meta=$(echo "$version" | cut -s -d '-' -f2) | |
if [[ -z "$tag_meta" ]]; then | |
gh release create \ | |
-n "[Changelog](https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/$BRANCH_NAME/changelog-client.html)" | |
--target "$BRANCH_NAME" \ | |
--title "v$version" \ | |
"v$version" | |
else | |
tag_main=$(echo "$version" | cut -d '-' -f1) | |
gh release create \ | |
-n "This is a $tag_main pre-release. Changes may not be stable." \ | |
--latest=false \ | |
--prerelease \ | |
--target "$BRANCH_NAME" \ | |
--title "v$version" \ | |
"v$version" | |
fi | |
env: | |
BRANCH_NAME: ${{ github.event.inputs.branch }} | |
GH_TOKEN: ${{ github.token }} |