Repository navigation
178 lines (159 loc) · 6.68 KB
/
Copy pathpublish.yml
File metadata and controls
178 lines (159 loc) · 6.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
name: Create a tag and publish to npm
on:
workflow_dispatch:
inputs:
branch:
description: 'Branch to release from (master, or a maintenance branch like 5.x)'
type: string
default: 'master'
required: true
bump:
description: 'Version bump type'
type: choice
required: true
default: 'minor'
options:
- patch
- minor
- major
- premajor
- preminor
- prepatch
- prerelease
preid:
description: 'Prerelease identifier (see "npm version") for pre* bumps'
type: string
required: false
npmPreTag:
description: 'NPM tag used for all pre* bumps (default: "next" if newer than the current latest, otherwise "next-<major>")'
type: string
required: false
npmTag:
description: 'NPM tag used for regular bumps (default: "latest" if newer than the current latest, otherwise "latest-<major>")'
type: string
required: false
dryRun:
description: 'Run in "dry run" mode'
type: boolean
default: false
required: true
permissions:
id-token: write
contents: write
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v7
with:
ref: ${{ inputs.branch }}
# Don't leave the token in .git/config where dependency scripts
# (npm install / npm test) could read it; it's passed to the push step only
persist-credentials: false
- name: Set up Node.js
uses: actions/setup-node@v7
with:
node-version: '24'
registry-url: 'https://registry.npmjs.org'
- name: Update npm
run: npm install -g npm@latest
- name: Install dependencies
run: npm install
- name: List of installed dependencies
run: npm ls -a
- name: Verifying provenance attestations
run: npm audit signatures
- name: Run tests
run: npm test
- name: Bump version and create tag
id: bump-version
env:
PREID_FLAG: ${{ startsWith(inputs.bump, 'pre') && inputs.preid && format('--preid {0}', inputs.preid) || '' }}
run: |
git config --global user.name "github-actions[bot]"
git config --global user.email "github-actions[bot]@users.noreply.github.com"
TAG=$(npm version ${{ github.event.inputs.bump }} $PREID_FLAG -m "Release %s")
echo "Created tag: $TAG"
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
- name: Resolve npm dist-tag
id: npm-tag
env:
IS_PRE: ${{ startsWith(inputs.bump, 'pre') }}
NPM_TAG: ${{ inputs.npmTag }}
NPM_PRE_TAG: ${{ inputs.npmPreTag }}
run: |
NAME=$(node -p "require('./package.json').name")
VERSION=$(node -p "require('./package.json').version")
MAJOR="${VERSION%%.*}"
CURRENT_LATEST=$(npm view "$NAME" dist-tags.latest)
if [ -z "$CURRENT_LATEST" ]; then
echo "::error::Could not read the current \"latest\" dist-tag of $NAME"
exit 1
fi
# Use the semver package bundled with npm to compare versions
SEMVER="$(npm root -g)/npm/node_modules/semver"
IS_NEWEST=$(node -e 'const semver = require(process.argv[1]); console.log(semver.gt(process.argv[2], process.argv[3]))' \
"$SEMVER" "$VERSION" "$CURRENT_LATEST")
# Only a version newer than the current "latest" may take the "latest"/"next" tags,
# no matter which branch it is released from (e.g. 6.x while master is the 7.0 candidate)
if [ "$IS_NEWEST" = "true" ]; then
DEFAULT_TAG=latest; DEFAULT_PRE_TAG=next
else
DEFAULT_TAG="latest-$MAJOR"; DEFAULT_PRE_TAG="next-$MAJOR"
fi
if [ "$IS_PRE" = "true" ]; then
DIST_TAG="${NPM_PRE_TAG:-$DEFAULT_PRE_TAG}"
else
DIST_TAG="${NPM_TAG:-$DEFAULT_TAG}"
fi
if [ "$IS_NEWEST" != "true" ] && { [ "$DIST_TAG" = "latest" ] || [ "$DIST_TAG" = "next" ]; }; then
echo "::error::Refusing to publish $VERSION with the \"$DIST_TAG\" dist-tag: it is not newer than the current latest $CURRENT_LATEST"
exit 1
fi
# The GitHub release becomes "Latest" only together with the npm "latest" tag
MAKE_LATEST=false
if [ "$IS_PRE" != "true" ] && [ "$DIST_TAG" = "latest" ]; then
MAKE_LATEST=true
fi
echo "Version: $VERSION, current latest: $CURRENT_LATEST, npm dist-tag: $DIST_TAG, GitHub latest: $MAKE_LATEST"
echo "tag=$DIST_TAG" >> "$GITHUB_OUTPUT"
echo "make_latest=$MAKE_LATEST" >> "$GITHUB_OUTPUT"
# Fail before publishing to npm if the push would be rejected (bad token,
# missing permissions, branch moved since checkout). Runs in dry-run mode too.
# Branch protection is only evaluated on a real push, so it isn't covered here.
- name: Verify push to ${{ inputs.branch }}
env:
BRANCH: ${{ inputs.branch }}
GH_PUSH_TOKEN: ${{ secrets.GH_PUSH_TOKEN }}
run: |
AUTH_HEADER="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$GH_PUSH_TOKEN" | base64 -w0)"
echo "::add-mask::$AUTH_HEADER"
git -c "http.https://github.com/.extraheader=$AUTH_HEADER" \
push --dry-run origin "HEAD:refs/heads/$BRANCH" --follow-tags
- name: Publish to npm
env:
DRY_RUN_FLAG: ${{ inputs.dryRun && '--dry-run' || '' }}
DIST_TAG: ${{ steps.npm-tag.outputs.tag }}
run: npm publish --provenance --access=public $DRY_RUN_FLAG --tag "$DIST_TAG"
- name: Push changes to ${{ inputs.branch }}
if: ${{ !inputs.dryRun }}
env:
BRANCH: ${{ inputs.branch }}
# Required to allow push to the release branch without the checks/PR
GH_PUSH_TOKEN: ${{ secrets.GH_PUSH_TOKEN }}
run: |
AUTH_HEADER="AUTHORIZATION: basic $(printf 'x-access-token:%s' "$GH_PUSH_TOKEN" | base64 -w0)"
echo "::add-mask::$AUTH_HEADER"
git -c "http.https://github.com/.extraheader=$AUTH_HEADER" \
push origin "HEAD:refs/heads/$BRANCH" --follow-tags
- name: Create GitHub Release
if: ${{ !inputs.dryRun }}
uses: softprops/action-gh-release@8a0cd651e5b2365bd640854a3fa5dfc70e4875f3 # v3.0.4
with:
tag_name: ${{ steps.bump-version.outputs.tag }}
target_commitish: ${{ inputs.branch }}
generate_release_notes: true
make_latest: ${{ steps.npm-tag.outputs.make_latest }}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}