Skip to content

Commit 737bba1

Browse files
authored
Create a GitHub Release when publishing a new buildpack version (#1663)
Previously when a new buildpack version was published only a Git tag was created. Now a GitHub release will be too, which contains the release's changelog entry extracted from `CHANGELOG.md`. This change makes it possible for people to subscribe to just the repository release event notifications, rather than needing to subscribe to all pull requests events and manually spot release PRs being opened/merged. The GitHub CLI is used to create the release, which also creates the Git tag - meaning the manual Git revision/tag/push steps are no longer required. In addition, some environment/release validation checks have been added, which: - Improve the onboarding UX if required tools aren't installed. - Prevent accidentally running the publish step before having triggered/merged the prepare release PR workflow. - Prevent version mismatch issues if the buildpack were ever rolled back on the registry. See: https://cli.github.com/manual/gh_release_view https://cli.github.com/manual/gh_release_create https://github.com/heroku/libcnb.rs/blob/ba5e8231b88f6f72533e8a9ba01d9745b3f40301/.github/workflows/release.yml#L52-L60 GUS-W-16959313.
1 parent a2cf258 commit 737bba1

File tree

1 file changed

+52
-15
lines changed

1 file changed

+52
-15
lines changed

etc/publish.sh

Lines changed: 52 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,65 @@
11
#!/usr/bin/env bash
2-
# shellcheck disable=SC2250 # TODO: Use braces around variable references even when not strictly required.
32

43
set -euo pipefail
54
shopt -s inherit_errexit
65

7-
BP_NAME=${1:-"heroku/python"}
6+
buildpack_registry_name="heroku/python"
87

9-
curVersion=$(heroku buildpacks:versions "$BP_NAME" | awk 'FNR == 3 { print $1 }')
10-
newVersion="v$((curVersion + 1))"
8+
function abort() {
9+
echo
10+
echo "Error: ${1}" >&2
11+
exit 1
12+
}
1113

12-
read -r -p "Deploy as version: $newVersion [y/n]? " choice
13-
case "$choice" in
14-
y | Y) echo "" ;;
14+
echo "Checking environment..."
15+
16+
if ! command -v gh >/dev/null; then
17+
abort "Install the GitHub CLI first: https://cli.github.com"
18+
fi
19+
20+
if ! heroku buildpacks:publish --help >/dev/null; then
21+
abort "Install the Buildpack Registry plugin first: https://github.com/heroku/plugin-buildpack-registry"
22+
fi
23+
24+
# Explicitly check the CLI is logged in, since the Buildpack Registry plugin doesn't handle re-authing
25+
# expired logins properly, which can otherwise lead to the release aborting partway through.
26+
if ! heroku whoami >/dev/null; then
27+
abort "Log into the Heroku CLI first: heroku login"
28+
fi
29+
30+
echo "Checking buildpack versions in sync..."
31+
current_github_release_version=$(gh release view --json tagName --jq '.tagName' | tr --delete 'v')
32+
current_registry_version="$(heroku buildpacks:versions "${buildpack_registry_name}" | awk 'FNR == 3 { print $1 }')"
33+
34+
if [[ "${current_github_release_version}" != "${current_registry_version}" ]]; then
35+
abort "The current GitHub release version (v${current_github_release_version}) does not match the registry version (v${current_registry_version}), likely due to a registry rollback. Fix this first!"
36+
fi
37+
38+
new_version="$((current_github_release_version + 1))"
39+
new_git_tag="v${new_version}"
40+
41+
echo "Extracting changelog entry for this release..."
42+
git fetch origin
43+
# Using `git show` to avoid having to disrupt the current branch/working directory.
44+
changelog_entry="$(git show origin/main:CHANGELOG.md | awk "/^## \[v${new_version}\]/{flag=1; next} /^## /{flag=0} flag")"
45+
46+
if [[ -n "${changelog_entry}" ]]; then
47+
echo -e "${changelog_entry}\n"
48+
else
49+
abort "Unable to find changelog entry for v${new_version}. Has the prepare release PR been triggered/merged?"
50+
fi
51+
52+
read -r -p "Deploy as ${new_git_tag} [y/n]? " choice
53+
case "${choice}" in
54+
y | Y) ;;
1555
n | N) exit 0 ;;
1656
*) exit 1 ;;
1757
esac
1858

19-
git fetch origin
20-
originMain=$(git rev-parse origin/main)
21-
echo "Tagging commit $originMain with $newVersion... "
22-
git tag "$newVersion" "${originMain:?}"
23-
git push origin "refs/tags/${newVersion}"
59+
echo -e "\nCreating GitHub release..."
60+
gh release create "${new_git_tag}" --title "${new_git_tag}" --notes "${changelog_entry}"
2461

25-
echo -e "\nPublishing to the buildpack registry..."
26-
heroku buildpacks:publish "$BP_NAME" "$newVersion"
62+
echo -e "\nPublishing to the Heroku Buildpack Registry..."
63+
heroku buildpacks:publish "${buildpack_registry_name}" "${new_git_tag}"
2764
echo
28-
heroku buildpacks:versions "${BP_NAME}" | head -n 3
65+
heroku buildpacks:versions "${buildpack_registry_name}" | head -n 3

0 commit comments

Comments
 (0)