Skip to content
This repository has been archived by the owner on Aug 29, 2023. It is now read-only.

feat: create gh releases in release-check/releaser workflows #456

Merged
merged 12 commits into from
Feb 7, 2023
126 changes: 82 additions & 44 deletions .github/workflows/release-check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,6 @@ jobs:
fi
echo "version=$v" >> $GITHUB_OUTPUT
fi
- name: Post output
if: steps.tag.outputs.exists == 'false' && steps.prev.outputs.version == ''
uses: marocchino/sticky-pull-request-comment@fcf6fe9e4a0409cd9316a5011435be0f3327f1e1 # v2.3.1
with:
header: release-check
recreate: true
message: |
Suggested version: `${{ steps.version.outputs.version }}`

This is the first release of this module.
- id: git-diff
name: run git diff on go.mod file(s)
if: steps.tag.outputs.exists == 'false' && steps.prev.outputs.version != ''
Expand Down Expand Up @@ -123,41 +113,18 @@ jobs:
output="(empty)"
fi
printf "output<<$EOF\n%s\n$EOF" "$output" >> $GITHUB_OUTPUT
- id: footnote
env:
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
BASE_REF: ${{ github.base_ref }}
GITHUB_TOKEN: ${{ github.token }}
HEAD_LABEL: ${{ github.event.pull_request.head.label }}
run: |
output=''
if [[ "$DEFAULT_BRANCH" != "$BASE_REF" ]]; then
output+='
## Cutting a Release (when not on `'"$DEFAULT_BRANCH"'`)

This PR is targeting `'"$BASE_REF"'`, which is not the default branch.
If you wish to cut a release once this PR is merged, please add the `release` label to this PR.
'
fi
diff="$(gh api -X GET "repos/$GITHUB_REPOSITORY/compare/$BASE_REF...$HEAD_LABEL" --jq '.files | map(.filename) | map(select(test("^(version\\.json|.*\\.md)$") | not)) | .[]')"
if [[ "$diff" != "" ]]; then
output+='
## Cutting a Release (and modifying non-markdown files)

This PR is modifying both `version.json` and non-markdown files.
The Release Checker is not able to analyse files that are not checked in to `'"$BASE_REF"'`. This might cause the above analysis to be inaccurate.
Please consider performing all the code changes in a separate PR before cutting the release.
'
fi
printf "output<<$EOF\n%s\n$EOF" "$output" >> $GITHUB_OUTPUT
- name: Post message on PR
uses: marocchino/sticky-pull-request-comment@fcf6fe9e4a0409cd9316a5011435be0f3327f1e1 # v2.3.1
if: steps.tag.outputs.exists == 'false' && steps.prev.outputs.version != ''
- id: release
if: github.event.pull_request.head.repo.full_name == github.repository
uses: galargh/action-gh-release@25b3878b4c346655a4d3d9bea8b76638f64743cf # https://github.com/softprops/action-gh-release/pull/316
with:
header: release-check
recreate: true
message: |
draft: true
tag_name: ${{ steps.version.outputs.version }}
generate_release_notes: true
- id: message
env:
HEADER: |
Suggested version: `${{ steps.version.outputs.version }}`
BODY: |
Comparing to: [${{ steps.prev.outputs.version }}](${{ github.event.pull_request.base.repo.html_url }}/releases/tag/${{ steps.prev.outputs.version }}) ([diff](${{ github.event.pull_request.base.repo.html_url }}/compare/${{ steps.prev.outputs.version }}..${{ github.event.pull_request.head.label }}))

Changes in `go.mod` file(s):
Expand All @@ -174,4 +141,75 @@ jobs:
```
${{ steps.gocompat.outputs.output }}
```
${{ steps.footnote.outputs.output }}

BODY_ALT: |
This is the first release of this module.

RELEASE_BRANCH_NOTICE: |
## Cutting a Release (when not on `${{ github.event.repository.default_branch }}`)

This PR is targeting `${{ github.base_ref }}`, which is not the default branch.
If you wish to cut a release once this PR is merged, please add the `release` label to this PR.

DIFF_NOTICE: |
## Cutting a Release (and modifying non-markdown files)

This PR is modifying both `version.json` and non-markdown files.
The Release Checker is not able to analyse files that are not checked in to `${{ github.base_ref }}`. This might cause the above analysis to be inaccurate.
Please consider performing all the code changes in a separate PR before cutting the release.

RELEASE_NOTICE: |
## Automatically created GitHub Release

A [draft GitHub Release](${{ steps.release.outputs.url }}) has been created.
It is going to be published when this PR is merged.
You can modify its' body to include any release notes you wish to include with the release.

RELEASE_NOTICE_ALT: |
## Automatically created GitHub Release

Pre-creating GitHub Releases on release PRs initiated from forks is not supported.
If you wish to prepare release notes yourself, you should create a draft GitHub Release for tag `${{ steps.version.outputs.version }}` manually.
The draft GitHub Release is going to be published when this PR is merged.
If you choose not to create a draft GitHub Release, a published GitHub Released is going to be created when this PR is merged.

DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
BASE_REF: ${{ github.base_ref }}
HEAD_LABEL: ${{ github.event.pull_request.head.label }}
HEAD_FULL_NAME: ${{ github.event.pull_request.head.repo.full_name }}
GITHUB_TOKEN: ${{ github.token }}
PREV_VERSION: ${{ steps.prev.outputs.version }}
run: |
echo "output<<$EOF" >> $GITHUB_OUTPUT

echo "$HEADER" >> $GITHUB_OUTPUT

if [[ "$PREV_VERSION" != "" ]]; then
echo "$BODY" >> $GITHUB_OUTPUT
else
echo "$BODY_ALT" >> $GITHUB_OUTPUT
fi

if [[ "$DEFAULT_BRANCH" != "$BASE_REF" ]]; then
echo "$RELEASE_BRANCH_NOTICE" >> $GITHUB_OUTPUT
fi

diff="$(gh api -X GET "repos/$GITHUB_REPOSITORY/compare/$BASE_REF...$HEAD_LABEL" --jq '.files | map(.filename) | map(select(test("^(version\\.json|.*\\.md)$") | not)) | .[]')"
if [[ "$diff" != "" ]]; then
echo "$DIFF_NOTICE" >> $GITHUB_OUTPUT
fi

if [[ "$GITHUB_REPOSITORY" == "$HEAD_FULL_NAME" ]]; then
echo "$RELEASE_NOTICE" >> $GITHUB_OUTPUT
else
echo "$RELEASE_NOTICE_ALT" >> $GITHUB_OUTPUT
fi

echo "$EOF" >> $GITHUB_OUTPUT
- name: Post message on PR
uses: marocchino/sticky-pull-request-comment@fcf6fe9e4a0409cd9316a5011435be0f3327f1e1 # v2.3.1
if: steps.tag.outputs.exists == 'false'
with:
header: release-check
recreate: true
message: ${{ steps.message.outputs.output }}
49 changes: 16 additions & 33 deletions .github/workflows/releaser.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,26 @@ on: [ workflow_call ]
jobs:
releaser:
runs-on: ubuntu-latest
env:
VERSION: ""
CREATETAG: "false"
DEFAULT_BRANCH: ""
steps:
- uses: actions/checkout@v3
- name: Determine version
run: echo "VERSION=$(jq -r .version version.json)" >> $GITHUB_ENV
- name: Determine branch
env:
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
run: echo "DEFAULT_BRANCH=refs/heads/$DEFAULT_BRANCH" >> $GITHUB_ENV
- name: Create a release, if we're on the default branch
run: echo "CREATETAG=true" >> $GITHUB_ENV
if: env.DEFAULT_BRANCH == github.ref
- name: Determine if this commit is a merged PR (if we're not on a default branch)
if: env.DEFAULT_BRANCH != github.ref
id: getmergedpr
- id: version
name: Determine version
run: echo "version=$(jq -r .version version.json)" >> $GITHUB_OUTPUT
- id: pr
name: Determine if this commit is a merged PR (if we're not on a default branch)
if: github.ref != format('refs/heads/{0}', github.event.repository.default_branch)
uses: actions-ecosystem/action-get-merged-pull-request@59afe90821bb0b555082ce8ff1e36b03f91553d9 # v1.0.1
with:
github_token: ${{ github.token }}
- name: Check if the "release" label was set on the PR
if: steps.getmergedpr.outputs.number != '' && env.DEFAULT_BRANCH != github.ref
- id: labels
env:
LABELS: ${{ steps.getmergedpr.outputs.labels }}
run: |
while IFS= read -r label; do
if [[ "$label" == "release" ]]; then
echo "CREATETAG=true" >> $GITHUB_ENV
break
fi
done <<< "$LABELS"
LABELS: ${{ steps.pr.outputs.labels }}
run: echo "labels=$(jq -nc 'env.LABELS | split("\n")')" >> $GITHUB_OUTPUT
- name: Create release
if: env.CREATETAG == 'true'
run: |
git fetch origin --tags
if ! $(git rev-list $VERSION.. &> /dev/null); then
git tag $VERSION
git push --tags
fi
if: github.ref == format('refs/heads/{0}', github.event.repository.default_branch) ||
contains(fromJSON(steps.labels.outputs.labels), 'release')
uses: galargh/action-gh-release@25b3878b4c346655a4d3d9bea8b76638f64743cf # https://github.com/softprops/action-gh-release/pull/316
with:
draft: false
tag_name: ${{ steps.version.outputs.version }}
generate_release_notes: true
8 changes: 6 additions & 2 deletions VERSIONING.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,13 @@ Every Go repository contains a `version.json` file in the root directory:
This version file defines the currently released version.

When cutting a new release, open a Pull Request that bumps the version number and have it review by your team mates.
The [release check workflow](.github/workflows/release-check.yml) will comment on the PR and post useful information (the output of `gorelease`, `gocompat` and a diff of the `go.mod` files(s)).
The [release check workflow](.github/workflows/release-check.yml) will create a draft GitHub Release (_if the workflow was not initiated by a PR from a fork_) and post a link to it along with other useful information (the output of `gorelease`, `gocompat` and a diff of the `go.mod` files(s)).

As soon as the PR is merged into the default branch, the [releaser workflow](.github/workflows/releaser.yml) is run. This workflow cuts a new release on CI and pushes the tag.
As soon as the PR is merged into the default branch, the [releaser workflow](.github/workflows/releaser.yml) is run. This workflow either publishes the draft GitHub Release created by the release check workflow or creates a published GitHub Release if it doesn't exist yet. This, in turn, will create a new Git tag and push it to the repository.

### Modifying GitHub Release

All modification you make to the draft GitHub Release created by the release check workflow will be preserved. You can change its' name and body to describe the release in more detail.

### Using a Release Branch

Expand Down