Skip to content

Commit aebfbbc

Browse files
committed
Update the CHANGELOG entry using npm version hook
Update the CHANGELOG entry using the "version" lifecycle hook which will execute after the `npm version` is completed. > If preversion, version, or postversion are in the scripts property of the package.json, they will be executed as part of running npm version. > The exact order of execution is as follows: > 1. Check to make sure the git working directory is clean before we get started. Your scripts may add files to the commit in future steps. This step is skipped if the --force flag is set. > 2. Run the preversion script. These scripts have access to the old version in package.json. A typical use would be running your full test suite before deploying. Any files you want added to the commit should be explicitly added using git add. > 3. Bump version in package.json as requested (patch, minor, major, etc). > 4. Run the version script. These scripts have access to the new version in package.json (so they can incorporate it into file headers in generated files for example). Again, scripts should explicitly add generated files to the commit using git add. > 5. Commit and tag. > 6. Run the postversion script. Use it to clean up the file system or automatically push the commit and/or tag. https://docs.npmjs.com/cli/v11/commands/npm-version#description Uses undocumented environment variables to get both the old and new versions: https://github.com/npm/cli/blob/c97b39b1e3436cd20a67ab5f4012a5f395c538b9/workspaces/libnpmversion/lib/version.js#L100-L103
1 parent e64f0e1 commit aebfbbc

3 files changed

Lines changed: 19 additions & 18 deletions

File tree

.github/workflows/build-release.yml

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -64,31 +64,17 @@ jobs:
6464
- name: Install dependencies
6565
run: npm ci
6666

67-
- name: Save current version
68-
run: echo "PREVIOUS_PACKAGE_VERSION=$(npm run get-version --silent --workspace govuk-frontend)" >> $GITHUB_ENV
69-
70-
- name: Update package version
71-
run: |
72-
npm version --no-git-tag-version --workspace govuk-frontend "$RELEASE_TYPE" --preid "$PREID"
73-
74-
# Save the new version number for access in future steps
75-
echo "PACKAGE_VERSION=$(npm run get-version --silent --workspace govuk-frontend)" >> $GITHUB_ENV
76-
77-
- name: Update CHANGELOG
78-
uses: actions/github-script@v9.0.0
79-
with:
80-
script: |
81-
const { updateChangelog } = await import('${{ github.workspace }}/shared/github-scripts/changelog-release-helper.mjs')
82-
83-
updateChangelog(process.env.PACKAGE_VERSION, process.env.PREVIOUS_PACKAGE_VERSION)
67+
- name: Update package version and update the CHANGELOG
68+
run: npm version --no-git-tag-version --workspace govuk-frontend "$RELEASE_TYPE" --preid "$PREID"
8469

8570
- name: Generate release notes
8671
uses: actions/github-script@v9.0.0
8772
with:
8873
script: |
8974
const { generateReleaseNotes } = await import('${{ github.workspace }}/shared/github-scripts/changelog-release-helper.mjs')
9075
91-
generateReleaseNotes(process.env.PACKAGE_VERSION, { actor: '${{ github.actor }}', runId: '${{ github.run_id }}' })
76+
PACKAGE_VERSION=$(npm run get-version --silent --workspace govuk-frontend)
77+
generateReleaseNotes(PACKAGE_VERSION, { actor: '${{ github.actor }}', runId: '${{ github.run_id }}' })
9278
9379
- name: Build release
9480
run: npm run build:release

bin/update-changelog.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { updateChangelog } from '../shared/github-scripts/changelog-release-helper.mjs'
2+
3+
const { npm_old_version: previousVersion, npm_new_version: newVersion } =
4+
process.env
5+
6+
if (!previousVersion || !newVersion) {
7+
throw new Error('Both previous and new version must be set to continue.')
8+
}
9+
10+
console.log(
11+
`Updating changelog from version ${previousVersion} to ${newVersion}...`
12+
)
13+
14+
updateChangelog(newVersion, previousVersion)

packages/govuk-frontend/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
"clean:package": "del-cli *.tsbuildinfo dist govuk-prototype-kit.config.json",
6464
"clean:release": "del-cli ../../dist --force",
6565
"postbuild:package": "npm run build:stats && govuk-prototype-kit validate-plugin .",
66+
"version": "../../bin/update-changelog.mjs",
6667
"get-version": "echo $npm_package_version"
6768
},
6869
"devDependencies": {

0 commit comments

Comments
 (0)