Skip to content

Commit

Permalink
Add branch option
Browse files Browse the repository at this point in the history
  • Loading branch information
taiki-e committed Jul 20, 2021
1 parent d455a5e commit 58454cb
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 17 deletions.
1 change: 1 addition & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ jobs:
with:
changelog: CHANGELOG.md
title: $version
branch: 'main|v[0-9]+'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ Note: In this file, do not use the hard wrap in the middle of a sentence for com

## [Unreleased]

- [Add `branch` input option to reject releases from commits not contained in specified branches](https://github.com/taiki-e/create-gh-release-action/pull/7)

## [1.1.0] - 2021-06-23

- [Add `draft` input option to create GitHub release as draft.](https://github.com/taiki-e/create-gh-release-action/pull/4)
Expand Down
81 changes: 69 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ GitHub Action for creating GitHub Releases based on changelog.
- [Usage](#usage)
- [Inputs](#inputs)
- [Example workflow: Basic usage](#example-workflow-basic-usage)
- [Example workflow: Create a draft release](#example-workflow-create-a-draft-release)
- [Example workflow: Custom title](#example-workflow-custom-title)
- [Example workflow: No changelog](#example-workflow-no-changelog)
- [Example workflow: Reject releases from outside the main branch](#example-workflow-reject-releases-from-outside-the-main-branch)
- [Other examples](#other-examples)
- [Related Projects](#related-projects)
- [License](#license)
Expand All @@ -34,11 +36,12 @@ Currently, changelog format and supported tag names have the following rule:

### Inputs

| Name | Required | Description | Type | Default |
|-----------|:--------:|----------------------------------------------------------------|--------|---------|
| changelog | false | Path to changelog | String | |
| title | false | Format of title (variables `$tag`, `$version`, and any string) | String | `$tag` |
| draft | false | Create a draft release ('true' or 'false') | String | `false` |
| Name | Required | Description | Type | Default |
|-----------|:--------:|----------------------------------------------------------------|---------|---------|
| changelog | false | Path to changelog | String | |
| title | false | Format of title (variables `$tag`, `$version`, and any string) | String | `$tag` |
| draft | false | Create a draft release (`true` or `false`) | Boolean | `false` |
| branch | false | Reject releases from commits not contained in branches that match the specified pattern (regular expression) | String | |

### Example workflow: Basic usage

Expand All @@ -57,12 +60,37 @@ jobs:
- uses: actions/checkout@v2
- uses: taiki-e/create-gh-release-action@v1
with:
# (optional) Path to changelog.
# (Optional) Path to changelog.
changelog: CHANGELOG.md
# (optional) Create a draft release.
env:
# (Required) GitHub token for creating GitHub Releases.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
### Example workflow: Create a draft release
```yaml
name: Release

on:
push:
tags:
- v[0-9]+.*

jobs:
create-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: taiki-e/create-gh-release-action@v1
with:
# (Optional) Path to changelog.
changelog: CHANGELOG.md
# (Optional) Create a draft release.
# [default value: false]
draft: true
env:
# (required) GitHub token for creating GitHub Releases.
# (Required) GitHub token for creating GitHub Releases.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```
Expand All @@ -87,14 +115,14 @@ jobs:
- uses: actions/checkout@v2
- uses: taiki-e/create-gh-release-action@v1
with:
# (optional)
# (Optional)
changelog: CHANGELOG.md
# (optional) Format of title.
# (Optional) Format of title.
# [default value: $tag]
# [possible values: variables $tag, $version, and any string]
title: $version
env:
# (required)
# (Required) GitHub token for creating GitHub Releases.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

Expand All @@ -119,7 +147,36 @@ jobs:
- uses: actions/checkout@v2
- uses: taiki-e/create-gh-release-action@v1
env:
# (required)
# (Required) GitHub token for creating GitHub Releases.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

### Example workflow: Reject releases from outside the main branch

You can reject releases from commits not contained in branches that match the specified pattern by using `branch` option.

```yaml
name: Release
on:
push:
tags:
- v[0-9]+.*
jobs:
create-release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: taiki-e/create-gh-release-action@v1
with:
# (Optional) Path to changelog.
changelog: CHANGELOG.md
# (Optional) Reject releases from commits not contained in branches
# that match the specified pattern (regular expression)
branch: main
env:
# (Required) GitHub token for creating GitHub Releases.
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
```

Expand Down
3 changes: 3 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ inputs:
description: Create a draft release ('true' or 'false')
required: false
default: 'false'
branch:
description: Reject releases from commits not contained in branches that match the specified pattern (regular expression)
required: false

runs:
using: node12
Expand Down
21 changes: 16 additions & 5 deletions main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,31 @@
set -euo pipefail
IFS=$'\n\t'

parse_changelog_tag="v0.3.0"

error() {
echo "::error::$*"
}

parse_changelog_tag="v0.3.0"

title="${INPUT_TITLE:?}"
changelog="${INPUT_CHANGELOG:-}"
draft="${INPUT_DRAFT:-}"
branch="${INPUT_BRANCH:-}"

if [[ -z "${GITHUB_TOKEN:-}" ]]; then
error "GITHUB_TOKEN not set"
exit 1
fi

if [[ "${GITHUB_REF:?}" != "refs/tags/"* ]]; then
error "GITHUB_REF should start with 'refs/tags/': ${GITHUB_REF}"
error "this action can only be used on 'push' event for 'tags' (GITHUB_REF should start with 'refs/tags/': '${GITHUB_REF}')"
exit 1
fi
tag="${GITHUB_REF#refs/tags/}"

# TODO: Support custom prefix of tags https://github.com/taiki-e/create-gh-release-action/issues/1
if [[ ! "${tag}" =~ ^v?[0-9]+\.[0-9]+\.[0-9]+(-[a-zA-Z_0-9\.-]+)?(\+[a-zA-Z_0-9\.-]+)?$ ]]; then
error "invalid tag format: ${tag}"
error "invalid tag format: '${tag}'"
exit 1
fi
if [[ "${tag}" =~ ^v?[0-9\.]+-[a-zA-Z_0-9\.-]+(\+[a-zA-Z_0-9\.-]+)?$ ]]; then
Expand All @@ -41,11 +43,20 @@ case "${draft}" in
;;
false) ;;
*)
error "'draft' input option must be 'true' or 'false': ${draft}"
error "'draft' input option must be 'true' or 'false': '${draft}'"
exit 1
;;
esac

if [[ -n "${branch}" ]]; then
git fetch &>/dev/null
if ! git branch -r --contains | grep -E "(^|\s)origin/(${branch})$" &>/dev/null; then
git branch -r --contains
error "Creating of release is only allowed on commits contained in branches that match the specified pattern: '${branch}'"
exit 1
fi
fi

if [[ -n "${changelog}" ]]; then
case "${OSTYPE}" in
linux*)
Expand Down

0 comments on commit 58454cb

Please sign in to comment.