UNPKG

5.2 kBMarkdownView Raw
1# Using semantic-release with [GitHub Actions](https://help.github.com/en/categories/automating-your-workflow-with-github-actions)
2
3## Environment variables
4
5The [Authentication](../usage/ci-configuration.md#authentication) environment variables can be configured with [Secret Variables](https://help.github.com/en/articles/virtual-environments-for-github-actions#creating-and-using-secrets-encrypted-variables).
6
7In this example a publish type [`NPM_TOKEN`](https://docs.npmjs.com/creating-and-viewing-authentication-tokens) is required to publish a package to the npm registry. GitHub Actions [automatically populate](https://help.github.com/en/articles/virtual-environments-for-github-actions#github_token-secret) a [`GITHUB_TOKEN`](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line) environment variable which can be used in Workflows.
8
9## Node project configuration
10
11[GitHub Actions](https://github.com/features/actions) support [Workflows](https://help.github.com/en/articles/configuring-workflows), allowing to run tests on multiple Node versions and publish a release only when all test pass.
12
13**Note**: The publish pipeline must run on [Node version >= 10.18](../support/FAQ.md#why-does-semantic-release-require-node-version--1018).
14
15### `.github/workflows/release.yml` configuration for Node projects
16
17The following is a minimal configuration for [`semantic-release`](https://github.com/semantic-release/semantic-release) with a build running on Node 12 when a new commit is pushed to a `master` branch. See [Configuring a Workflow](https://help.github.com/en/articles/configuring-a-workflow) for additional configuration options.
18
19```yaml
20name: Release
21on:
22 push:
23 branches:
24 - master
25jobs:
26 release:
27 name: Release
28 runs-on: ubuntu-18.04
29 steps:
30 - name: Checkout
31 uses: actions/checkout@v2
32 with:
33 fetch-depth: 0
34 - name: Setup Node.js
35 uses: actions/setup-node@v1
36 with:
37 node-version: 12
38 - name: Install dependencies
39 run: npm ci
40 - name: Release
41 env:
42 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43 NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
44 run: npx semantic-release
45```
46
47## Pushing `package.json` changes to a `master` branch
48
49To keep `package.json` updated in the `master` branch, [`@semantic-release/git`](https://github.com/semantic-release/git) plugin can be used.
50
51**Note**: Automatically populated `GITHUB_TOKEN` cannot be used if branch protection is enabled for the target branch. It is **not** advised to mitigate this limitation by overriding an automatically populated `GITHUB_TOKEN` variable with a [Personal Access Tokens](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line), as it poses a security risk. Since Secret Variables are available for Workflows triggered by any branch, it becomes a potential vector of attack, where a Workflow triggered from a non-protected branch can expose and use a token with elevated permissions, yielding branch protection insignificant. One can use Personal Access Tokens in trusted environments, where all developers should have the ability to perform administrative actions in the given repository and branch protection is enabled solely for convenience purposes, to remind about required reviews or CI checks.
52
53If the risk is acceptable, some extra configuration is needed. The [actions/checkout `persist-credentials`](https://github.com/marketplace/actions/checkout#usage) option needs to be `false`, otherwise the generated `GITHUB_TOKEN` will interfere with the custom one. Example:
54
55```yaml
56- name: Checkout
57 uses: actions/checkout@v2
58 with:
59 fetch-depth: 0
60 persist-credentials: false # <--- this
61```
62
63## Trigger semantic-release on demand
64
65### Using GUI:
66You can use [Manual Triggers](https://github.blog/changelog/2020-07-06-github-actions-manual-triggers-with-workflow_dispatch/) for GitHub Actions.
67
68### Using HTTP:
69Use [`repository_dispatch`](https://docs.github.com/en/actions/reference/events-that-trigger-workflows#repository_dispatch) event to have control on when to generate a release by making an HTTP request, e.g.:
70
71```yaml
72name: Release
73on:
74 repository_dispatch:
75 types: [semantic-release]
76jobs:
77# ...
78```
79
80To trigger a release, call (with a [Personal Access Tokens](https://help.github.com/en/github/authenticating-to-github/creating-a-personal-access-token-for-the-command-line) stored in `GITHUB_TOKEN` environment variable):
81
82```
83$ curl -v -H "Accept: application/vnd.github.everest-preview+json" -H "Authorization: token ${GITHUB_TOKEN}" https://api.github.com/repos/[org-name-or-username]/[repository]/dispatches -d '{ "event_type": "semantic-release" }'
84```
85
86### Using 3rd party apps:
87If you'd like to use a GitHub app to manage this instead of creating a personal access token, you could consider using a project like:
88
89* [Actions Panel](https://www.actionspanel.app/) - A declaratively configured way for triggering GitHub Actions
90* [Action Button](https://github-action-button.web.app/#details) - A simple badge based mechanism for triggering GitHub Actions