Skip to content

Instantly share code, notes, and snippets.

@Morgbn
Last active February 17, 2023 10:42
Show Gist options
  • Save Morgbn/ff80f1228d58310b9cd291d8705c8f71 to your computer and use it in GitHub Desktop.
Save Morgbn/ff80f1228d58310b9cd291d8705c8f71 to your computer and use it in GitHub Desktop.
Auto npm release with semantic-release & github workflow

Create a npm access token

1️⃣ Go to https://www.npmjs.com/settings/npm_account/tokens/new

2️⃣ Follow the instructions (select type "Automation"), and copy the generated token

Config the github repo

3️⃣ Go to https://github.com/github_account/github_repo/settings

4️⃣ Under "Code and automation" > "Actions" > "General" > "Workflow permissions"
→ check "Read and write permissions"

5️⃣ Under "Security" > "Secrets and variables" > "Actions"
→ click the button "New repository secret"
→ add a secret named "NPM_TOKEN" with for value your npm token

6️⃣ Add the two following files to the repo:
(ci.yml in .github/workflows/ci.yml, .releaserc in the root folder)

7️⃣ Check that your package.json includes repository field

{
"branches": [
"main",
{ "name": "alpha", "prerelease": true }
],
"debug": true,
"ci": true,
"dryRun": false,
"plugins": [
"@semantic-release/commit-analyzer",
"@semantic-release/release-notes-generator",
"@semantic-release/changelog",
"@semantic-release/npm",
[
"@semantic-release/git",
{
"assets": ["package.json", "CHANGELOG.md"],
"message": "chore(release): :bookmark: ${nextRelease.version} [skip ci]\n\n${nextRelease.notes}"
}
],
"@semantic-release/github"
]
}
# .github/workflows/ci.yml
name: ci
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-node@v3
with:
node-version: 18
cache: yarn
- run: yarn install
- run: yarn prepack
- run: yarn lint
- run: yarn build
- name: Publish
if: |
github.event_name == 'push' &&
!contains(github.event.head_commit.message, '[skip-release]')
run: |
yarn global add semantic-release @semantic-release/commit-analyzer @semantic-release/release-notes-generator @semantic-release/changelog @semantic-release/npm @semantic-release/git @semantic-release/github &&
semantic-release
env:
GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}
NPM_TOKEN: ${{secrets.NPM_TOKEN}}
NODE_AUTH_TOKEN: ${{secrets.NPM_TOKEN}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment