Skip to content

Commit 676bdb7

Browse files
chore: enable prerelease npm action to actually publish to npm (#1466)
Co-authored-by: Grady Ellison <ellisong1990@gmail.com>
1 parent 93f0fd0 commit 676bdb7

File tree

8 files changed

+125
-76
lines changed

8 files changed

+125
-76
lines changed

.github/actions/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"handlebars": "^4.7.8",
1818
"mime-types": "^2.1.35",
1919
"node-fetch": "^3.3.2",
20+
"semver": "^7.7.1",
2021
"uuid": "^9.0.1",
2122
"yargs": "^17.7.2"
2223
}

.github/actions/prerelease-npm-version/action.yml

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,34 +3,20 @@
33
name: Prerelease NPM Version
44

55
inputs:
6-
version-type:
7-
description: "The type of version bump to make. One of: major, minor, patch, premajor, preminor, prepatch, or prerelease."
8-
type: choice
9-
required: true
10-
default: preminor
11-
options:
12-
- major
13-
- minor
14-
- patch
15-
- premajor
16-
- preminor
17-
- prepatch
18-
- prerelease
19-
- from-git
206
preid:
217
description: "The prerelease identifier to use for the version number when bumping a prerelease version."
228
type: string
239
required: true
2410
default: rc
25-
tag:
26-
description: "The NPM package distribution tag to use when publishing the prerelease."
11+
version-override:
12+
description: "If not supplied, the version in package.json will be used. Whichever version is greater between local and NPM will take precedence -- we can only create prerelease versions *ahead* of the current version in NPM."
2713
type: string
28-
required: true
29-
default: rc
14+
required: false
3015
dry_run:
3116
description: "Indicates whether to dry run the npm publish command."
3217
type: boolean
33-
required: true
18+
required: false
19+
default: 'false'
3420

3521
outputs:
3622
version:
@@ -40,6 +26,9 @@ outputs:
4026
runs:
4127
using: "composite"
4228
steps:
29+
- name: Install dependencies
30+
run: npm install --silent --no-progress --prefix $GITHUB_ACTION_PATH/..
31+
shell: bash
4332
- name: Get version number
4433
id: agent-loader-version
4534
shell: bash
@@ -48,12 +37,9 @@ runs:
4837
id: bump-prerelease
4938
shell: bash
5039
run: |
51-
pattern=".*-rc\.[0-9]+$"
52-
if [[ ${{ steps.agent-loader-version.outputs.results }} =~ $pattern ]]; then
53-
echo "results=$(npm version prerelease --preid=${{ inputs.preid }})" >> $GITHUB_OUTPUT
54-
else
55-
echo "results=$(npm version ${{ inputs.version-type }} --preid=${{ inputs.preid }})" >> $GITHUB_OUTPUT
56-
fi
40+
node $GITHUB_ACTION_PATH/index.cjs \
41+
--version-override ${{ inputs.version-override }} \
42+
--preid ${{ inputs.preid }}
5743
- name: Install project dependencies
5844
shell: bash
5945
run: npm ci
@@ -62,4 +48,4 @@ runs:
6248
run: npm run build:npm
6349
- name: Publish npm package
6450
shell: bash
65-
run: npm publish ${{ inputs.dry_run && '--dry-run' || '' }} --tag=${{ inputs.tag }}
51+
run: npm publish ${{ ((inputs.dry_run == true || inputs.dry_run == 'true') && '--dry-run') || '' }} --tag=${{ inputs.preid }}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import process from 'process'
2+
import yargs from 'yargs'
3+
import { hideBin } from 'yargs/helpers'
4+
5+
export const args = yargs(hideBin(process.argv))
6+
.usage('$0 [options]')
7+
8+
.string('preid')
9+
.describe('preid', 'The preid to use for the pre-release version')
10+
.default('preid', 'rc')
11+
12+
.string('version-override')
13+
.describe('version-override', 'A version to use instead of the current package version')
14+
15+
.demandOption(['preid'])
16+
.argv
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
const { execSync } = require('child_process')
2+
const { writeFileSync } = require('fs')
3+
const semver = require('semver')
4+
const core = require('@actions/core')
5+
6+
const lineBreak = () => {
7+
console.log('----------------------------------')
8+
}
9+
10+
lineBreak()
11+
console.log('Running pre-release version bump script')
12+
13+
import('./args.js').then(({ args }) => {
14+
const preid = args.preid || 'rc'
15+
16+
lineBreak()
17+
console.log(`Fetching tag ${preid} from NPM`)
18+
fetch(`https://registry.npmjs.com/@newrelic/browser-agent/${preid}`)
19+
.then((response) => response.json())
20+
.then((data) => {
21+
22+
const versionJson = require(process.cwd() + '/package.json')
23+
24+
const npmVersion = data.version ? `${semver.major(data.version)}.${semver.minor(data.version)}.${semver.patch(data.version)}` : `0.0.0`
25+
const [_, npmPreid] = data.version && !!semver.prerelease(data.version) ? semver.prerelease(data.version) : [undefined, -1]
26+
27+
lineBreak()
28+
console.log('NPM Version, preid value', npmVersion, npmPreid)
29+
30+
const pkgSrc = args.versionOverride || versionJson.version
31+
const pkgVersion = pkgSrc ? `${semver.major(pkgSrc)}.${semver.minor(pkgSrc)}.${semver.patch(pkgSrc)}` : `0.0.0`
32+
let [pkgPreidTag, pkgPreid] = pkgSrc && !!semver.prerelease(pkgSrc) ? semver.prerelease(pkgSrc) : [undefined, -1]
33+
if (pkgPreidTag !== preid) pkgPreid = -1
34+
35+
lineBreak()
36+
console.log('Package Version, preid value', pkgVersion, pkgPreid)
37+
38+
if (semver.gte(npmVersion, pkgVersion)) {
39+
lineBreak()
40+
console.log(`NPM Version is greater than or the same, setting local pkg version to NPM version (${preid}) + 1`)
41+
versionJson.version = npmVersion + `-${preid}.` + (Math.max(parseInt(npmPreid), parseInt(pkgPreid)) + 1)
42+
43+
} else {
44+
lineBreak()
45+
console.log('NPM Version is less than local pkg version, incrementing local pkg version')
46+
versionJson.version = pkgVersion + `-${preid}.` + (parseInt(pkgPreid) + 1)
47+
}
48+
49+
lineBreak()
50+
console.log('New Pre-release Package Version', versionJson.version)
51+
52+
lineBreak()
53+
writeFileSync(process.cwd() + '/package.json', JSON.stringify(versionJson, undefined, 2))
54+
console.log('Updated package.json')
55+
56+
lineBreak()
57+
core.setOutput('results', versionJson.version)
58+
})
59+
.catch((error) => {
60+
console.error('Error fetching data:', error);
61+
})
62+
})

.github/workflows/prerelease-npm-version.yml

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,20 @@ name: Prerelease NPM Version
66
on:
77
workflow_dispatch:
88
inputs:
9-
version-type:
10-
description: "The type of version bump to make. One of: major, minor, patch, premajor, preminor, prepatch, or prerelease."
11-
type: choice
12-
required: true
13-
default: preminor
14-
options:
15-
- major
16-
- minor
17-
- patch
18-
- premajor
19-
- preminor
20-
- prepatch
21-
- prerelease
22-
- from-git
239
preid:
2410
description: "The prerelease identifier to use when bumping a prerelease version."
2511
type: string
2612
required: true
2713
default: rc
28-
tag:
29-
description: "The NPM package distribution tag to use when publishing the prerelease."
14+
version-override:
15+
description: "If not supplied, the version in package.json will be used. Whichever version is greater between local and NPM will take precedence -- we can only create prerelease versions *ahead* of the current version in NPM."
3016
type: string
31-
required: true
32-
default: rc
17+
required: false
3318
dry_run:
3419
description: "Indicates whether to dry run the npm publish command."
3520
type: boolean
36-
required: true
21+
required: false
22+
default: "false"
3723

3824
jobs:
3925
prerelease-npm-version:
@@ -61,7 +47,6 @@ jobs:
6147
- uses: ./.github/actions/prerelease-npm-version
6248
id: prerelease-npm-version
6349
with:
64-
version-type: ${{ github.event.inputs.version-type }}
50+
version-override: ${{ github.event.inputs.version-override }}
6551
preid: ${{ github.event.inputs.preid }}
66-
tag: ${{ github.event.inputs.tag }}
6752
dry_run: ${{ github.event.inputs.dry_run }}

.github/workflows/publish-dev.yml

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -215,31 +215,4 @@ jobs:
215215
aws_bucket_name: ${{ secrets.AWS_BUCKET }}
216216
fastly_key: ${{ secrets.FASTLY_PURGE_KEY }}
217217

218-
# Bump a new prerelease version and publish to NPM.
219-
prerelease-npm-version:
220-
runs-on: ubuntu-latest
221-
env:
222-
NPM_TOKEN: ${{ secrets.BROWSER_NPM_TOKEN }}
223-
GITHUB_LOGIN: ${{ secrets.BROWSER_GITHUB_BOT_NAME }}
224-
GITHUB_EMAIL: ${{ secrets.BROWSER_GITHUB_BOT_EMAIL }}
225-
steps:
226-
- uses: actions/checkout@v4
227-
- uses: actions/setup-node@v4
228-
with:
229-
node-version: 22.11.0 # See package.json for the stable node version that works with our testing. Do not change this unless you know what you are doing as some node versions do not play nicely with our testing server.
230-
- name: Authenticate npm
231-
shell: bash
232-
run: |
233-
npm config set '//registry.npmjs.org/:_authToken' "${NPM_TOKEN}"
234-
- name: Set up git
235-
id: git-setup
236-
run: |
237-
git config --global user.name "${GITHUB_LOGIN}"
238-
git config --global user.email "${GITHUB_EMAIL}"
239-
- uses: ./.github/actions/prerelease-npm-version
240-
id: prerelease-npm-version
241-
with:
242-
version-type: preminor
243-
preid: rc
244-
tag: rc
245-
dry_run: true
218+

.github/workflows/release-please.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,29 @@ jobs:
3131
{"type":"fix","section":"Bug Fixes"},
3232
{"type":"security","section":"Security Fixes"}
3333
]
34+
# Bump a new prerelease version and publish to NPM.
35+
prerelease-npm-version:
36+
needs: release-please
37+
runs-on: ubuntu-latest
38+
env:
39+
NPM_TOKEN: ${{ secrets.BROWSER_NPM_TOKEN }}
40+
GITHUB_LOGIN: ${{ secrets.BROWSER_GITHUB_BOT_NAME }}
41+
GITHUB_EMAIL: ${{ secrets.BROWSER_GITHUB_BOT_EMAIL }}
42+
steps:
43+
- uses: actions/checkout@v4
44+
- uses: actions/setup-node@v4
45+
with:
46+
node-version: 22.11.0 # See package.json for the stable node version that works with our testing. Do not change this unless you know what you are doing as some node versions do not play nicely with our testing server.
47+
- name: Authenticate npm
48+
shell: bash
49+
run: |
50+
npm config set '//registry.npmjs.org/:_authToken' "${NPM_TOKEN}"
51+
- name: Set up git
52+
id: git-setup
53+
run: |
54+
git config --global user.name "${GITHUB_LOGIN}"
55+
git config --global user.email "${GITHUB_EMAIL}"
56+
- uses: ./.github/actions/prerelease-npm-version
57+
id: prerelease-npm-version
58+
with:
59+
dry_run: false

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,4 @@
294294
"README.md",
295295
"CHANGELOG.md"
296296
]
297-
}
297+
}

0 commit comments

Comments
 (0)