Skip to content

Commit

Permalink
Merge pull request codfish#150 from codfish/options-update
Browse files Browse the repository at this point in the history
options
  • Loading branch information
codfish authored Jan 22, 2021
2 parents 325861f + b1d4220 commit 94885a8
Show file tree
Hide file tree
Showing 8 changed files with 3,854 additions and 4,013 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,34 @@ jobs:
steps:
- uses: actions/checkout@v1

# dry-run testing
- name: testing action with all inputs
id: semantic
uses: ./
with:
dry_run: true
branches: |
[
'+([0-9])?(.{+([0-9]),x}).x',
'master',
'next',
'next-major',
{
name: 'beta',
prerelease: true
},
{
name: 'alpha',
prerelease: true
}
]
repository_url: https://github.com/codfish/semantic-release-action.git
tag_format: 'v${version}'
plugins: |
['@semantic-release/commit-analyzer', '@semantic-release/release-notes-generator', '@semantic-release/npm', '@semantic-release/github']
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: semantic-release
id: semantic
uses: ./
Expand Down
24 changes: 22 additions & 2 deletions .github/workflows/validate.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,31 @@ jobs:

- run: npm run lint

# dog-food testing. Should not release here at all, cause it's not on
# master.
# dog-food testing. Should not release here at all, cause it runs on a PR.
- name: semantic-release
id: semantic
uses: ./
with:
dry_run: true
branches: |
[
'+([0-9])?(.{+([0-9]),x}).x',
'master',
'next',
'next-major',
{
name: 'beta',
prerelease: true
},
{
name: 'alpha',
prerelease: true
}
]
repository_url: https://github.com/codfish/semantic-release-action.git
tag_format: 'v${version}'
plugins: |
['@semantic-release/commit-analyzer', '@semantic-release/release-notes-generator', '@semantic-release/npm', '@semantic-release/github']
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM node:12
FROM node:12.20.1-slim

# nice clean home for our action files
RUN mkdir /action
Expand Down
130 changes: 94 additions & 36 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,39 +1,59 @@
# semantic-release-action

> GitHub Action for running `semantic-release`. Respects any .releaserc.js configuration file in
> your repo. Exports [environment variables](#outputs) for you to use in subsequent actions
> containing version numbers.
> GitHub Action for running `semantic-release`. Respects any semantic-release configuration file in
> your repo or the `release` prop in your `package.json`. Exports [environment variables](#outputs)
> for you to use in subsequent actions containing version numbers.
<!-- START doctoc generated TOC please keep comment here to allow auto update -->
<!-- DON'T EDIT THIS SECTION, INSTEAD RE-RUN doctoc TO UPDATE -->

- [Usage](#usage)
- [Why](#why)
- [Configuration](#configuration)
- [Example with all inputs](#example-with-all-inputs)
- [Outputs](#outputs)

<!-- END doctoc generated TOC please keep comment here to allow auto update -->

## Usage

See [action.yml](action.yml)
See [action.yml](action.yml).

- Use major version
([recommended by GitHub](https://github.com/actions/toolkit/blob/master/docs/action-versioning.md)):
`uses: codfish/semantic-release-action@v1`
- Use latest version: `uses: codfish/semantic-release-action@master`
- Use latest version: `uses: codfish/semantic-release-action@v1`
- Use specific version: `uses: codfish/[email protected]`

Basic Usage:
**Note**: You can speed up runs by using pre-built docker images as well.

```yml
- uses: docker://codfish/semantic-release-action:v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
```
**Basic Usage:**
```yml
steps:
- uses: actions/checkout@master

- uses: codfish/semantic-release-action@master
- uses: codfish/semantic-release-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
```
Using output variables set by `semantic-release-action`:
**Using output variables set by `semantic-release-action`:**

```yml
steps:
- uses: actions/checkout@master
# you'll need to add an `id` in order to access output variables
- uses: codfish/semantic-release-action@master
- uses: codfish/semantic-release-action@v1
id: semantic
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -45,19 +65,19 @@ steps:
env:
OUTPUTS: ${{ toJson(steps.semantic.outputs) }}

- uses: codfish/some-other-action@master
- uses: codfish/some-other-action@v1
with:
release-version: ${{ steps.semantic.outputs.release-version }}
```
Only run an action if a new version was created:
**Only run an action if a new version was created:**
```yml
steps:
- uses: actions/checkout@master

# you'll need to add an `id` in order to access output variables
- uses: codfish/semantic-release-action@master
- uses: codfish/semantic-release-action@v1
id: semantic
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
Expand All @@ -72,13 +92,13 @@ steps:
TAG: v$RELEASE_VERSION
```
Using environment variables set by `semantic-release-action`:
**Using environment variables set by `semantic-release-action`:**

```yml
steps:
- uses: actions/checkout@master
- uses: codfish/semantic-release-action@master
- uses: codfish/semantic-release-action@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
Expand All @@ -90,23 +110,8 @@ steps:
echo $RELEASE_PATCH
```

If you're _not_ publishing to npm and only want to use this action for GitHub releases, you need to
include a `.releaserc.js` file in your repo, instructing `semantic-release` to not publish to the
`npm` registry.

Example `.releaserc.js` if you're not publishing to npm:

```js
module.exports = {
branch: 'master',
plugins: [
'@semantic-release/commit-analyzer',
'@semantic-release/release-notes-generator',
['@semantic-release/npm', { npmPublish: false }],
'@semantic-release/github',
],
};
```
If you're _not_ publishing to npm and only want to use this action for GitHub releases, the easiest
approach would simply be to add `"private": true,` to your `package.json`.

## Why

Expand Down Expand Up @@ -142,13 +147,66 @@ new release number is.
This also scales well, just in case I want to add some flexibility and functionality to this action,
I can easily leverage it across any project.

## Inputs
## Configuration

You can pass in `semantic-release` configuration options via GitHub Action inputs using `with`.

It's important to note, **NONE** of these inputs are required. The action will automatically use any
[`semantic-release` configuration](https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#configuration-file)
found in your repo (`.releaserc`, `release.config.js`, `release` prop in `package.json`)

> NOTE: Each input **will take precedence** over options configured in the configuration file and
> shareable configurations.

**Docs:** https://help.github.com/en/articles/metadata-syntax-for-github-actions#inputs
| Input Variable | Description |
| -------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------- |
| branches | The branches on which releases should happen. |
| plugins | Define the list of plugins to use. Plugins will run in series, in the order defined, for each steps if they implement it |
| extends | List of modules or file paths containing a shareable configuration. |
| dry_run | The objective of the dry-run mode is to get a preview of the pending release. Dry-run mode skips the following steps: prepare, publish, success and fail. |
| repository_url | The git repository URL |
| tag_format | The Git tag format used by semantic-release to identify releases. |

| Input Variable | Description |
| -------------- | ------------------------------------------- |
| branch | The branch on which releases should happen. |
**Note**: The `branch` input is **DEPRECATED**. Will continue to be supported for v1. Use `branches`
instead. Previously used in semantic-release v15 to set a single branch on which releases should
happen.

- **GitHub Actions Inputs:**
https://help.github.com/en/articles/metadata-syntax-for-github-actions#inputs
- **Semantic Release Configuration:**
https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md

### Example with all inputs

```yml
steps:
- run: codfish/semantic-release-action@v1
with:
dry_run: true
branches: |
[
'+([0-9])?(.{+([0-9]),x}).x',
'master',
'next',
'next-major',
{
name: 'beta',
prerelease: true
},
{
name: 'alpha',
prerelease: true
}
]
repository_url: https://github.com/codfish/semantic-release-action.git
tag_format: 'v${version}'
extends: '@semantic-release/apm-config'
plugins: |
['@semantic-release/commit-analyzer', '@semantic-release/release-notes-generator', '@semantic-release/npm', '@semantic-release/github']
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
```

### Outputs

Expand Down
38 changes: 36 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,44 @@ branding:
icon: 'arrow-up-right'
color: 'orange'
inputs:
branches:
description:
'The branches on which releases should happen.
https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#branches'
required: false
plugins:
description:
'Define the list of plugins to use. Plugins will run in series, in the order defined, for each
steps if they implement it.
https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#plugins'
required: false
extends:
description:
'List of modules or file paths containing a shareable configuration.
https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#extends'
required: false
dry_run:
description:
'The objective of the dry-run mode is to get a preview of the pending release. Dry-run mode
skips the following steps: prepare, publish, success and fail.
https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#dryrun'
required: false
repository_url:
description:
'The git repository URL.
https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#repositoryurl'
required: false
tag_format:
description:
'The Git tag format used by semantic-release to identify releases.
https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#tagformat'
required: false
branch:
description:
'The branch on which releases should happen.
https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#branch'
'DEPRECATED. Will continue to be supported for v1. Use `branches` instead. Previously used in
semantic-release v15 to set a single branch on which releases should happen.
https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#branches'
required: false
outputs:
new-release-published:
description:
Expand Down
44 changes: 39 additions & 5 deletions entrypoint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
const core = require('@actions/core');
const semanticRelease = require('semantic-release');
const JSON5 = require('json5');

const parseInput = input => {
try {
return JSON5.parse(input);
} catch (err) {
return input;
}
};

/**
* Run semantic-release.
Expand All @@ -8,9 +17,36 @@ const semanticRelease = require('semantic-release');
* @see https://github.com/semantic-release/semantic-release/blob/master/docs/usage/configuration.md#options
*/
async function run() {
const branch = core.getInput('branch', { required: false }) || 'master';
const result = await semanticRelease({ branch });
const branch = parseInput(core.getInput('branch', { required: false }));
const branches = parseInput(core.getInput('branches', { required: false }));
const plugins = parseInput(core.getInput('plugins', { required: false }));
const extendsInput = parseInput(core.getInput('extends', { required: false }));
const dryRun = core.getInput('dry_run', { required: false }) === 'true';
const repositoryUrl = core.getInput('repository_url', { required: false });
const tagFormat = core.getInput('tag_format', { required: false });

core.debug(`branch input: ${branch}`);
core.debug(`branches input: ${branches}`);
core.debug(`plugins input: ${plugins}`);
core.debug(`extends input: ${extendsInput}`);
core.debug(`dry_run input: ${dryRun}`);
core.debug(`repository_url input: ${repositoryUrl}`);
core.debug(`tag_format input: ${tagFormat}`);

// build options object
const branchOption = branch ? { branches: branch } : { branches };
const options = {
...branchOption,
plugins,
extends: extendsInput,
dryRun,
repositoryUrl,
tagFormat,
};
// remove undefined options
Object.keys(options).forEach(key => options[key] === undefined && delete options[key]);

const result = await semanticRelease(options);
if (!result) {
core.debug('No release published');

Expand All @@ -30,11 +66,9 @@ async function run() {
core.debug(`The last release was "${lastRelease.version}".`);
}

// outputs
// set outputs
const { version } = nextRelease;
const [major, minor, patch] = version.split('.');

// set outputs
core.exportVariable('NEW_RELEASE_PUBLISHED', 'true');
core.exportVariable('RELEASE_VERSION', version);
core.exportVariable('RELEASE_MAJOR', major);
Expand Down
Loading

0 comments on commit 94885a8

Please sign in to comment.