Skip to content

Instantly share code, notes, and snippets.

@mskrajnowski
Created December 2, 2021 12:46
Show Gist options
  • Save mskrajnowski/13d9058163d51662132b6051451b04e9 to your computer and use it in GitHub Desktop.
Save mskrajnowski/13d9058163d51662132b6051451b04e9 to your computer and use it in GitHub Desktop.
Create version tags and GitHub releases based on merges to a main branch
#!/usr/bin/env bash
set -euo pipefail
branch=main
initial_sha=SHA_OF_THE_FIRST_RELEASE
initial_version=1
version_tag () {
version=$1
echo "v$version"
}
version_tag_message () {
previous_version_sha=$1
version_sha=$2
git log \
--oneline \
--reverse \
--merges \
--format='%s' \
$previous_version_sha..$version_sha \
| egrep -o 'Merge pull request #[0-9]+' \
| egrep -o '#[0-9]+' \
| sed 's/^/- /'
}
add_version_tag () {
commit_sha=$1
tag=$2
message=$3
commit_date=$(git show --oneline --format='%cD' $commit_sha)
git tag -d $tag
set -x
GIT_COMMITTER_DATE="$commit_date" \
git tag \
--annotate \
--message="$message" \
$tag $commit_sha
set +x
}
add_release () {
tag=$1
message="$(git tag -l --format='%(contents)' $tag)"
set -x
gh release delete -y $tag
gh release create $tag \
--title "$tag" \
--notes "$message"
set +x
}
deploy_shas () {
git log \
$initial_sha..$branch \
--merges \
--first-parent $branch \
--reverse \
--format='%h'
}
main () {
initial_tag=$(version_tag $initial_version)
add_version_tag $initial_sha $initial_tag "Initial production deployment"
version_tags="$initial_tag"
previous_sha=$initial_sha
previous_version=$initial_version
for deploy_sha in $(deploy_shas); do
deploy_version=$(expr $previous_version + 1)
deploy_tag=$(version_tag $deploy_version)
deploy_message=$(version_tag_message $previous_sha $deploy_sha)
add_version_tag $deploy_sha $deploy_tag "$deploy_message"
previous_sha=$deploy_sha
previous_version=$deploy_version
version_tags="$version_tags $deploy_tag"
done
git push --tags --force
for version_tag in $version_tags; do
add_release $version_tag
done
}
main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment