Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add GitHub workflow to automatically publish to npm #40

Merged
merged 2 commits into from
Nov 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions .github/workflows/publish_npm.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Publish to npm
on:
release:
types: [published] # Run this whenever we create a new release through GitHub releases
jobs:
build:
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- uses: actions/checkout@v4
# Setup .npmrc file to publish to npm
- uses: actions/setup-node@v4
with:
node-version: '22.x'
registry-url: 'https://registry.npmjs.org'
- run: npm run ci-project
- run: npm run build --if-present
- run: npm test
- run: npm publish --provenance --access public
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
38 changes: 38 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

# Contributing

## Code of Conduct

Please see `./CODE_OF_CONDUCT.md`.


## Change requests

Feature branch names should be of the form `feature/<issue-ref>-<issue-summary>`, where `<issue-ref>` is a reference
to the relevant issue number, if any, and `<issue-summary>` is a short summary of the issue in camel case. For example:
`feature/gh-2-add-new-gizmo`. If there is no issue number for the change (e.g. it's a quick one-off change), use the
format `<username>/<date>-<summary>`, for example `mkrause/241129-upgrade-dependencies`.


## Release workflow

To create a new release:

- Create a release branch named `release/vx.y.z`.

- Submit a release PR targeting the `master` branch:
- Bumps the version in `package.json.js`.
- Run `npm run install-project` to update the `package.json` and `package-lock.json` files.
- The commit message should be of the form "Release vx.y.z"
- The title of the release PR should be of the form "Release vx.y.z"

- Once the PR is merged, create a new release:
- Go the GitHub repo, and navigate to ["Releases"](https://github.com/fortanix/baklava/releases).
- Click ["Draft a new release"](https://github.com/fortanix/baklava/releases/new).
- Under "Choose a new tag", create a new tag of the form `vx.y.z`.
- The name of the release should be of the form `vx.y.z`.
- Write the release notes.
- If the version is a pre-release, mark it as such.
- Hit "Publish the release".

- Once the release has been created, a GitHub Actions workflow will automatically run to publish this release to npm.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"//": "NOTE: This is a generated file. Do not edit this file directly, use package.json.js instead.",
"//": "NOTE: This is a generated file. Do not edit this file directly, edit package.json.js instead.",
"name": "@fortanix/baklava",
"version": "1.0.0-beta-20241126",
"license": "MPL-2.0",
Expand Down
31 changes: 23 additions & 8 deletions package.json.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,15 +173,30 @@ const packageConfig = {
},
};

const packageConfigWithComment = {
// http://stackoverflow.com/questions/14221579/how-do-i-add-comments-to-package-json
'//': 'NOTE: This is a generated file. Do not edit this file directly, use package.json.js instead.',

const makePackageJson = () => {
const packageJson = {
// http://stackoverflow.com/questions/14221579/how-do-i-add-comments-to-package-json
'//': 'NOTE: This is a generated file. Do not edit this file directly, edit package.json.js instead.',
...packageConfig,
};

/*
const packageJsonCurrent = (() => {
try {
return JSON.parse(fs.readFileSync('./package.json'));
} catch (error) {
return null;
}
})();
// Inherit the version from the existing `package.json`, to allow facilitate version bumps through CI
if (packageJsonCurrent?.version && typeof packageJson.version !== 'string') {
packageJson.version = packageJsonCurrent.version;
}
*/

...packageConfig,
return packageJson;
};

// Output a valid JSON document (stripped of comments and such)
const packageConfigFormatted = JSON.stringify(packageConfigWithComment, null, 2);

// Write to `package.json`
fs.writeFileSync('./package.json', packageConfigFormatted + '\n');
fs.writeFileSync('./package.json', JSON.stringify(makePackageJson(), null, 2) + '\n');