Skip to content

Commit f84cc5b

Browse files
author
Heimer Nguyen
committed
chore: update GitHub Actions workflow for npm publishing
1 parent 4d78571 commit f84cc5b

File tree

1 file changed

+49
-15
lines changed

1 file changed

+49
-15
lines changed

.github/workflows/publish.yml

Lines changed: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
name: Publish to npm
22

33
on:
4+
push:
5+
tags:
6+
- 'v*.*.*'
47
release:
58
types: [published]
69
workflow_dispatch:
@@ -23,7 +26,7 @@ jobs:
2326
publish:
2427
runs-on: ubuntu-latest
2528
permissions:
26-
contents: read
29+
contents: write
2730
packages: write
2831
steps:
2932
- name: Checkout
@@ -32,14 +35,16 @@ jobs:
3235
- name: Setup Node.js
3336
uses: actions/setup-node@v4
3437
with:
35-
node-version: '18'
38+
node-version: '20'
3639
registry-url: 'https://registry.npmjs.org'
3740

38-
- name: Install Yarn
39-
run: npm install -g yarn
41+
- name: Enable Corepack (Yarn 3)
42+
run: |
43+
corepack enable
44+
corepack prepare [email protected] --activate
4045
4146
- name: Install dependencies
42-
run: yarn install --frozen-lockfile
47+
run: yarn install --immutable
4348

4449
- name: Run tests
4550
run: yarn test
@@ -54,30 +59,59 @@ jobs:
5459
run: yarn build
5560

5661
- name: Set version
57-
if: github.event.inputs.version != ''
58-
run: npm version ${{ github.event.inputs.version }} --no-git-tag-version
62+
if: ${{ github.event_name == 'workflow_dispatch' && inputs.version != '' }}
63+
env:
64+
INPUT_VERSION: ${{ inputs.version }}
65+
run: npm version "$INPUT_VERSION" --no-git-tag-version
5966

6067
- name: Publish to npm
68+
env:
69+
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
70+
NPM_DIST_TAG: ${{ inputs.tag }}
6171
run: |
62-
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
63-
npm publish --tag ${{ github.event.inputs.tag }}
72+
if [ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ]; then
73+
TAG="${NPM_DIST_TAG:-latest}"
74+
npm publish --tag "$TAG"
6475
else
6576
npm publish
6677
fi
78+
79+
- name: Determine release metadata
80+
if: ${{ github.event_name != 'release' }}
81+
id: meta
6782
env:
68-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
83+
INPUT_VERSION: ${{ inputs.version }}
84+
run: |
85+
set -euo pipefail
86+
TAG=""
87+
PRERELEASE="false"
88+
if [ "${GITHUB_EVENT_NAME}" = "push" ]; then
89+
TAG="${GITHUB_REF_NAME}"
90+
elif [ "${GITHUB_EVENT_NAME}" = "workflow_dispatch" ]; then
91+
if [ -n "${INPUT_VERSION:-}" ]; then
92+
TAG="v${INPUT_VERSION}"
93+
else
94+
PKG_VERSION="$(node -p "require('./package.json').version")"
95+
TAG="v${PKG_VERSION}"
96+
fi
97+
fi
98+
if echo "$TAG" | grep -q -- "-"; then
99+
PRERELEASE="true"
100+
fi
101+
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
102+
echo "prerelease=$PRERELEASE" >> "$GITHUB_OUTPUT"
69103
70104
- name: Create GitHub Release Notes
71-
if: github.event_name != 'release'
105+
if: ${{ github.event_name != 'release' }}
72106
uses: actions/create-release@v1
73107
env:
74108
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
75109
with:
76-
tag_name: v${{ github.event.inputs.version || 'package.json version' }}
77-
release_name: Release v${{ github.event.inputs.version || 'package.json version' }}
110+
tag_name: ${{ steps.meta.outputs.tag }}
111+
release_name: Release ${{ steps.meta.outputs.tag }}
78112
body: |
79113
## Changes
80-
114+
81115
See [CHANGELOG.md](https://github.com/nguyenthanhan/react-native-image-code-scanner/blob/main/CHANGELOG.md) for details.
82116
draft: false
83-
prerelease: ${{ github.event.inputs.tag != 'latest' }}
117+
prerelease: ${{ steps.meta.outputs.prerelease == 'true' }}

0 commit comments

Comments
 (0)