2022-12-15 15:41:30 +02:00
#!/usr/bin/env bash
set -euo pipefail
# shellcheck source=scripts/lib.sh
source " $( dirname " ${ BASH_SOURCE [0] } " ) /lib.sh"
cdroot
2023-01-11 18:38:01 +02:00
usage() {
cat <<EOH
2023-07-12 19:31:13 +03:00
Usage: ./release.sh [--dry-run] [-h | --help] [--ref <ref>] [--major | --minor | --patch] [--force]
2022-12-15 15:41:30 +02:00
2023-01-11 18:38:01 +02:00
This script should be called to create a new release.
When run, this script will display the new version number and optionally a
preview of the release notes. The new version will be selected automatically
based on if the release contains breaking changes or not. If the release
contains breaking changes, a new minor version will be created. Otherwise, a
new patch version will be created.
To mark a release as containing breaking changes, the commit title should
either contain a known prefix with an exclamation mark ("feat!:",
"feat(api)!:") or the PR that was merged can be tagged with the
"release/breaking" label.
2023-01-19 15:13:11 +02:00
GitHub labels that affect release notes:
- release/breaking: Shown under BREAKING CHANGES, prevents patch release.
2023-02-15 16:23:06 +02:00
- release/experimental: Shown at the bottom under Experimental.
2023-01-19 15:13:11 +02:00
- security: Shown under SECURITY.
Flags:
Set --major or --minor to force a larger version bump, even when there are no
breaking changes. By default a patch version will be created, --patch is no-op.
2023-07-12 19:31:13 +03:00
Set --force force the provided increment to be used (e.g. --patch), even if
there are breaking changes, etc.
2023-01-19 15:13:11 +02:00
Set --ref if you need to specify a specific commit that the new version will
be tagged at, otherwise the latest commit will be used.
Set --dry-run to see what this script would do without making actual changes.
2023-01-11 18:38:01 +02:00
EOH
}
branch = main
dry_run = 0
ref =
increment =
2023-07-12 19:31:13 +03:00
force = 0
2023-01-11 18:38:01 +02:00
2023-07-12 19:31:13 +03:00
args = " $( getopt -o h -l dry-run,help,ref:,major,minor,patch,force -- " $@ " ) "
2022-12-15 15:41:30 +02:00
eval set -- " $args "
while true; do
case " $1 " in
2023-01-11 18:38:01 +02:00
--dry-run)
dry_run = 1
shift
;;
-h | --help)
usage
exit 0
;;
2022-12-15 15:41:30 +02:00
--ref)
ref = " $2 "
shift 2
;;
2023-01-11 18:38:01 +02:00
--major | --minor | --patch)
if [[ -n $increment ]] ; then
error "Cannot specify multiple version increments."
fi
increment = ${ 1 #-- }
2022-12-15 15:41:30 +02:00
shift
;;
2023-07-12 19:31:13 +03:00
--force)
force = 1
shift
;;
2022-12-15 15:41:30 +02:00
--)
shift
break
;;
*)
error "Unrecognized option: $1 "
;;
esac
done
# Check dependencies.
dependencies gh sort
2023-01-11 18:38:01 +02:00
if [[ -z $increment ]] ; then
# Default to patch versions.
increment = "patch"
fi
2022-12-15 15:41:30 +02:00
# Make sure the repository is up-to-date before generating release notes.
2023-01-11 18:38:01 +02:00
log "Fetching $branch and tags from origin..."
git fetch --quiet --tags origin " $branch "
2022-12-15 15:41:30 +02:00
# Resolve to the latest ref on origin/main unless otherwise specified.
2023-01-11 18:38:01 +02:00
ref = $( git rev-parse --short " ${ ref :- origin / $branch } " )
2022-12-15 15:41:30 +02:00
# Make sure that we're running the latest release script.
2023-01-11 18:38:01 +02:00
if [[ -n $( git diff --name-status origin/" $branch " -- ./scripts/release.sh) ]] ; then
2022-12-15 15:41:30 +02:00
error "Release script is out-of-date. Please check out the latest version and try again."
fi
# Check the current version tag from GitHub (by number) using the API to
# ensure no local tags are considered.
2023-01-13 20:45:31 +02:00
log "Checking GitHub for latest release..."
2023-02-08 14:40:52 +02:00
versions_out = " $( gh api -H "Accept: application/vnd.github+json" /repos/coder/coder/git/refs/tags -q '.[].ref | split("/") | .[2]' | grep '^v' | sort -r -V) "
mapfile -t versions <<< " $versions_out "
2022-12-15 15:41:30 +02:00
old_version = ${ versions [0] }
2023-01-13 20:45:31 +02:00
log "Latest release: $old_version "
log
2022-12-15 15:41:30 +02:00
2023-01-11 22:14:04 +02:00
trap 'log "Check commit metadata failed, you can try to set \"export CODER_IGNORE_MISSING_COMMIT_METADATA=1\" and try again, if you know what you are doing."' EXIT
2022-12-15 15:41:30 +02:00
# shellcheck source=scripts/release/check_commit_metadata.sh
source " $SCRIPT_DIR /release/check_commit_metadata.sh" " $old_version " " $ref "
2023-01-11 22:14:04 +02:00
trap - EXIT
2022-12-15 15:41:30 +02:00
2023-07-12 19:31:13 +03:00
tag_version_args =( --old-version " $old_version " --ref " $ref " --" $increment " )
if (( force == 1)) ; then
tag_version_args +=( --force)
fi
2023-01-13 20:45:31 +02:00
log "Executing DRYRUN of release tagging..."
2023-07-12 19:31:13 +03:00
new_version = " $( execrelative ./release/tag_version.sh " ${ tag_version_args [@] } " --dry-run) "
2023-01-13 20:45:31 +02:00
log
read -p "Continue? (y/n) " -n 1 -r continue_release
log
if ! [[ $continue_release = ~ ^[ Yy] $ ]] ; then
exit 0
fi
2023-08-03 12:41:54 -05:00
release_notes = " $( execrelative ./release/generate_release_notes.sh --check-for-changelog --old-version " $old_version " --new-version " $new_version " --ref " $ref " ) "
2022-12-15 15:41:30 +02:00
read -p "Preview release notes? (y/n) " -n 1 -r show_reply
2023-01-11 18:38:01 +02:00
log
2022-12-15 15:41:30 +02:00
if [[ $show_reply = ~ ^[ Yy] $ ]] ; then
2023-01-13 20:45:31 +02:00
log
2022-12-15 15:41:30 +02:00
echo -e " $release_notes \n"
fi
2023-01-13 20:45:31 +02:00
read -p "Create release? (y/n) " -n 1 -r create
2023-01-11 18:38:01 +02:00
log
if ! [[ $create = ~ ^[ Yy] $ ]] ; then
exit 0
fi
2023-01-13 20:45:31 +02:00
log
# Run without dry-run to actually create the tag, note we don't update the
# new_version variable here to ensure we're pushing what we showed before.
2023-07-12 19:31:13 +03:00
maybedryrun " $dry_run " execrelative ./release/tag_version.sh " ${ tag_version_args [@] } " >/dev/null
2023-01-13 20:45:31 +02:00
maybedryrun " $dry_run " git push --tags -u origin " $new_version "
2023-01-12 01:32:25 +02:00
2023-01-11 18:38:01 +02:00
if (( dry_run)) ; then
2023-01-13 20:45:31 +02:00
# We can't watch the release.yaml workflow if we're in dry-run mode.
exit 0
2023-01-11 18:38:01 +02:00
fi
log
read -p "Watch release? (y/n) " -n 1 -r watch
log
if ! [[ $watch = ~ ^[ Yy] $ ]] ; then
exit 0
fi
log 'Waiting for job to become "in_progress"...'
# Wait at most 3 minutes (3*60)/3 = 60 for the job to start.
for _ in $( seq 1 60) ; do
2023-02-08 14:40:52 +02:00
output = " $(
2023-01-11 18:38:01 +02:00
# Output:
# 3886828508
# in_progress
gh run list -w release.yaml \
--limit 1 \
--json status,databaseId \
--jq '.[] | (.databaseId | tostring), .status'
2023-02-08 14:40:52 +02:00
) "
mapfile -t run <<< " $output "
2023-01-11 18:38:01 +02:00
if [[ ${ run [1] } != "in_progress" ]] ; then
sleep 3
continue
fi
gh run watch --exit-status " ${ run [0] } "
exit 0
done
error "Waiting for job to start timed out."