Repository navigation
Expand file tree
/
Copy pathci_create_github_release.sh
More file actions
executable file
·70 lines (54 loc) · 2.54 KB
/
Copy pathci_create_github_release.sh
File metadata and controls
executable file
·70 lines (54 loc) · 2.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#!/usr/bin/env bash
set -eo pipefail
# Creates a draft GitHub release for the given tag, using the latest
# CHANGELOG.md entry as the release description.
#
# This is a `gh`-based replacement for travisci-tools/release_github/release_github_v2.sh,
# which relied on the deprecated `hub` CLI. `gh` is preinstalled on GitHub-hosted runners.
#
# Run this in the same directory where CHANGELOG.md lives.
# grep -P below is GNU grep specific, so this is meant to run on Linux.
CHANGELOG="CHANGELOG.md"
# set to true for debugging
debug=false
if [ $# -ne 1 ]; then
echo "example: $0 v3.1.1"
exit 1
fi
# the tag being released, e.g. v4.6.0
GIT_TAG="$1"
# VERSION is the git tag with the "v" prefix removed
VERSION=${GIT_TAG#v}
# The release is often already created (the tag push that triggers CI usually
# comes from a GitHub release created in the UI). In that case there is nothing
# to create -- just let the caller move on to uploading assets.
if gh release view "${GIT_TAG}" >/dev/null 2>&1; then
echo "Release ${GIT_TAG} already exists; skipping creation."
exit 0
fi
if [[ ! -f "${CHANGELOG}" ]]; then
echo "ERROR: ${CHANGELOG} not found in $(pwd)."
exit 1
fi
# check that CHANGELOG.md has been updated
# (first version entry in the changelog should match our VERSION argument)
NEW_VERSION_CHECK=$(grep -P '^## \[\d+\.\d+\.\d+\]' "${CHANGELOG}" | cut -d[ -f2 | cut -d] -f1 | awk 'NR==1')
$debug && echo "NEW_VERSION_CHECK $NEW_VERSION_CHECK"
$debug && echo "VERSION $VERSION"
if [[ "${NEW_VERSION_CHECK}" != "${VERSION}" ]]; then
echo "ERROR: ${CHANGELOG} has not been updated yet."
exit 1
fi
NEW_VERSION=$(grep -P '^## \[\d+\.\d+\.\d+\]' "${CHANGELOG}" | awk 'NR==1' | sed -e 's/\[/\\\[/' | sed -e 's/\]/\\\]/')
LAST_VERSION=$(grep -P '^## \[\d+\.\d+\.\d+\]' "${CHANGELOG}" | awk 'NR==2' | sed -e 's/\[/\\\[/' | sed -e 's/\]/\\\]/')
DESCRIPTION=$(awk "/^${NEW_VERSION}$/,/^${LAST_VERSION:-nothingmatched}$/" "${CHANGELOG}" | grep -v "^${LAST_VERSION:-nothingmatched}$")
$debug && echo "NEW_VERSION $NEW_VERSION"
$debug && echo "LAST_VERSION $LAST_VERSION"
$debug && echo "DESCRIPTION $DESCRIPTION"
# Write the notes to a temp file and pass via --notes-file. This avoids the shell
# re-parsing backticks / $ that can appear in changelog bodies (which --notes would).
NOTES_FILE=$(mktemp)
trap 'rm -f "${NOTES_FILE}"' EXIT
printf '%s\n' "${DESCRIPTION}" > "${NOTES_FILE}"
# --draft creates a draft release; the tag already exists (release is triggered by the tag push)
gh release create "${GIT_TAG}" --draft --title "Release ${VERSION}" --notes-file "${NOTES_FILE}"