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 actions to recipes #1317

Merged
merged 17 commits into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/recipes/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
- [CircleCI 2.0 workflows](circleci-workflows.md)
- [Travis CI](travis.md)
- [GitLab CI](gitlab-ci.md)
- [GitHub Actions](github-actions.md)

## Git hosted services
- [Git authentication with SSH keys](git-auth-ssh-keys.md)
Expand Down
66 changes: 66 additions & 0 deletions docs/recipes/github-actions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
# Using semantic-release with [GitHub Actions](https://help.github.com/en/categories/automating-your-workflow-with-github-actions)

## Environment variables

The [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).

For this example, you'll need to setup a [`NPM_TOKEN`](https://docs.npmjs.com/creating-and-viewing-authentication-tokens) to publish your package to NPM registry. GitHub Actions [automatically adds](https://help.github.com/en/articles/virtual-environments-for-github-actions#github_token-secret) [`GITHUB_TOKEN`](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line) to be used in workflows.

## Node project configuration

[GitHub Actions](https://github.com/features/actions) supports [Workflows](https://help.github.com/en/articles/configuring-workflows) allowing to test on multiple Node versions and publishing a release only when all test pass.

**Note**: The publish pipeline must run a [Node >= 8 version](../support/FAQ.md#why-does-semantic-release-require-node-version--83).

### `.github/workflows/release.yml` configuration for Node projects

This example is a minimal configuration for [`semantic-release`](https://github.com/semantic-release/semantic-release) with a build running Node 12 when receives a new commit at `master` branch. See [Configuring a workflow](https://help.github.com/en/articles/configuring-a-workflow) for additional configuration options.

```yaml
name: Release
on:
push:
branches:
- master
jobs:
install:
name: "Generate release"
runs-on: ubuntu-18.04
steps:
- uses: actions/checkout@v1
- uses: actions/setup-node@master
with:
node-version: 12
- name: Install
run: npm ci
- name: Generate release
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: npx semantic-release
```

## Pushing `package.json` changes to `master` branch

If you want to keep your `package.json` updated in your code versioning with your released version you could use [`@semantic-release/git`](https://github.com/semantic-release/git) plugin. To use it you'll need to generate a [`GITHUB_TOKEN`](https://help.github.com/en/articles/creating-a-personal-access-token-for-the-command-line) with [permission to push changes to `master` branch](https://help.github.com/en/articles/enabling-branch-restrictions).
alissonperez marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can still improve this message. It suggest that users have to create this token manually in this case, but the default one already has these permissions. Do you think we can rephrase it somehow, so it's more clear?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, good point.. I'll think about it..

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem with the provided GITHUB_TOKEN is that it cannot push to master if you have branch protection enabled. For that you need to create a personal access token.

You can rephrase it and say

To use it you'll need to generate a personal access token with permission to push changes to master branch. Store the personal access token as a secret, then set the GITHUB_TOKEN environment variable from it (instead of from the provided GITHUB_TOKEN secret)

        env:
          GITHUB_TOKEN: ${{ secrets.MY_PERSONAL_ACCESS_TOKEN }}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @gr2m , I changed with your changes. Please, take a look what do you think guys. @merlinnot


## Trigger semantic-release on demand

There is a way to trigger semantic-relase on demand, we can just use [`repository_dispatch`](https://help.github.com/en/articles/events-that-trigger-workflows#external-events-repository_dispatch) to have control when you want to generate a release only making an HTTP request, e.g.:

```yaml
name: Release
on:
repository_dispatch:
types: [semantic-release]
jobs:
# ...
```

So just call (with your personal `GITHUB_TOKEN` or from a generic/ci user):

```
$ 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" }'
```

And `Release` workflow will be triggered.
1 change: 1 addition & 0 deletions docs/usage/ci-configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The `semantic-release` command must be executed only after all the tests in the
Here is a few example of the CI services that can be used to achieve this:
- [Travis Build Stages](https://docs.travis-ci.com/user/build-stages)
- [CircleCI Workflows](https://circleci.com/docs/2.0/workflows)
- [GitHub Actions](https://github.com/features/actions)
- [Codeship Deployment Pipelines](https://documentation.codeship.com/basic/builds-and-configuration/deployment-pipelines)
- [GitLab Pipelines](https://docs.gitlab.com/ee/ci/pipelines.html#introduction-to-pipelines-and-jobs)
- [Codefresh Pipelines](https://codefresh.io/docs/docs/configure-ci-cd-pipeline/introduction-to-codefresh-pipelines)
Expand Down