From ff71b00145ca62cce04e99e53bd26a611d714d14 Mon Sep 17 00:00:00 2001 From: Taiki Endo Date: Tue, 20 Jul 2021 17:33:26 +0900 Subject: [PATCH] Add branch option --- .github/workflows/release.yml | 1 + CHANGELOG.md | 2 + README.md | 81 +++++++++++++++++++++++++++++------ action.yml | 3 ++ main.sh | 21 ++++++--- 5 files changed, 91 insertions(+), 17 deletions(-) diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 29dd3b8..6671327 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -25,5 +25,6 @@ jobs: with: changelog: CHANGELOG.md title: $version + branch: 'main|v[0-9]+' env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} diff --git a/CHANGELOG.md b/CHANGELOG.md index 7573cab..3160d70 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/README.md b/README.md index 37fe14e..29a7869 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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 @@ -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 }} ``` @@ -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 }} ``` @@ -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 }} ``` diff --git a/action.yml b/action.yml index 7f80b19..a7863db 100644 --- a/action.yml +++ b/action.yml @@ -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 diff --git a/main.sh b/main.sh index f6270e1..34fb492 100755 --- a/main.sh +++ b/main.sh @@ -3,15 +3,16 @@ 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" @@ -19,13 +20,14 @@ if [[ -z "${GITHUB_TOKEN:-}" ]]; then 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 @@ -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})$"; 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*)