diff --git a/.gitignore b/.gitignore index 6dd88c5..e77e190 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ terraform-provider-phpipam - +pkg/ diff --git a/CHANGELOG.md b/CHANGELOG.md index f8ff958..0cf5218 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,12 @@ +## 0.1.2-pre + +Bumped version for dev. + +## 0.1.1 + +Bumping release so that I have a consistent snapshot, and also so that I can +correct some tests on the compat branch. + ## 0.1.0 First release! - diff --git a/Makefile b/Makefile index 9609655..05845b6 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,14 @@ build: deps gox -osarch="linux/amd64 windows/amd64 darwin/amd64" \ -output="pkg/{{.OS}}_{{.Arch}}/terraform-provider-phpipam" . +release: release_bump release_build + +release_bump: + scripts/release_bump.sh + +release_build: + scripts/release_build.sh + deps: go get -u github.com/mitchellh/gox diff --git a/scripts/release_build.sh b/scripts/release_build.sh new file mode 100755 index 0000000..8208613 --- /dev/null +++ b/scripts/release_build.sh @@ -0,0 +1,138 @@ +#!/usr/bin/env bash + +declare -A args=( + # The GPG key to sign the binaries with. + [keyid]="9E2D8AFF3BE44244" + + # The name of the target binary. + [binname]="terraform-provider-phpipam" + + # The build target + [build_target]="build" + + # The OSes to release for. + [target_os]="windows darwin linux" + + # The arches to release for. + [target_arch]="amd64" +) + +# message prints text with a color, redirected to stderr in the event of +# warning or error messages. +message() { + declare -A __colors=( + ["error"]="31" # red + ["warning"]="33" # yellow + ["begin"]="32" # green + ["ok"]="32" # green + ["info"]="1" # bold + ["reset"]="0" # here just to note reset code + ) + local __type="$1" + local __message="$2" + if [ -z "${__colors[$__type]}" ]; then + __type="info" + fi + if [[ ! "${__type}" =~ ^(warning|error)$ ]]; then + echo -e "\e[${__colors[$__type]}m${__message}\e[0m" 1>&2 + else + echo -e "\e[${__colors[$__type]}m${__message}\e[0m" + fi +} + +# get_release fetches the last 10 releases and asks the user to pick one to +# release for. The script then switches to this branch. +get_release() { + # gets the last ten releases. + versions=($(git tag --sort "-v:refname" | egrep '^v[0-9]+\.[0-9]+\.[0-9]+' | head -n 10)) + + if [ -z "${versions[*]}" ]; then + message error "No non-prerelease versions available at this time. Please release a version first" + message error "and ensure the build has a matching vMAJOR.MINOR.PATCH tag." + exit 1 + fi + + message info "Select a build version to release with:" + for (( n=0; n<${#versions[@]}; n+=1 )); do + message info "${n}: ${versions[$n]}" + done + + read -r -p "$(message info "Enter a number (Default 0, CTRL-C Cancels):")" selection_number + + if [ -z "${selection_number}" ]; then + selection_number="0" + fi + + if ! [ "${selection_number}" -ge "0" ] 2>/dev/null || ! [ "${selection_number}" -lt "${#versions[@]}" ] 2>/dev/null; then + message error "Invalid selection. Please enter a valid selection from 0-$((${#versions[@]}-1))." + exit 1 + fi + + git checkout "${versions[$selection_number]}" + echo "${versions[$selection_number]}" +} + +# build performs the build +build() { + ( + make "${args[build_target]}" + ) + local __status=$? + if [ "${__status}" != "0" ]; then + message error "ERROR: Release build exited with code ${__status}" + exit 1 + fi +} + +# move_files moves the files to a dist/ directory within the pkg/ +# directory of the build root, with the release tag supplied to the +# function. The binaries are also compressed. +move_files() { + local __release="$1" + local __os="" + local __arch="" + set -e + rm -rf pkg/dist + mkdir -p pkg/dist + for __os in ${args[target_os]}; do + for __arch in ${args[target_arch]}; do + if [ "${__os}" == "windows" ]; then + zip -j "pkg/dist/${args[binname]}_${__release}_${__os}_${__arch}.zip" "pkg/${__os}_${__arch}/${args[binname]}.exe" + else + zip -j "pkg/dist/${args[binname]}_${__release}_${__os}_${__arch}.zip" "pkg/${__os}_${__arch}/${args[binname]}" + fi + done + done + set +e +} + +# sign_files creates a SHA256SUMS files for the releases, and creates +# a detatched GPG signature. +sign_files() { + local __release="$1" + ( + set -e + cd pkg/dist + shasum -a256 -- * > "./${args[binname]}_${__release}_SHA256SUMS" + gpg --default-key "${args[keyid]}" --detach-sig "./${args[binname]}_${__release}_SHA256SUMS" + ) + local __status=$? + if [ "${__status}" != "0" ]; then + message error "ERROR: Release signing exited with code ${__status}" + exit 1 + fi +} + +## Main +if [ "$(git rev-parse HEAD)" != "$(git rev-parse master)" ]; then + message error "ERROR: Current HEAD does not match master." + message error "Please switch HEAD back to master before running this script." + exit 1 +fi + +release="$(get_release)" + +build "${release}" +move_files "${release}" +sign_files "${release}" +git checkout master diff --git a/scripts/release_bump.sh b/scripts/release_bump.sh new file mode 100755 index 0000000..a8c6ba7 --- /dev/null +++ b/scripts/release_bump.sh @@ -0,0 +1,75 @@ +#!/usr/bin/env bash + +# The release from CHANGELOG.md. +release=$(head -n 1 CHANGELOG.md | awk '{print $2}') + +# message prints text with a color, redirected to stderr in the event of +# warning or error messages. +message() { + declare -A __colors=( + ["error"]="31" # red + ["warning"]="33" # yellow + ["begin"]="32" # green + ["ok"]="32" # green + ["info"]="1" # bold + ["reset"]="0" # here just to note reset code + ) + local __type="$1" + local __message="$2" + if [ -z "${__colors[$__type]}" ]; then + __type="info" + fi + if [[ ! "${__type}" =~ ^(warning|error)$ ]]; then + echo -e "\e[${__colors[$__type]}m${__message}\e[0m" 1>&2 + else + echo -e "\e[${__colors[$__type]}m${__message}\e[0m" + fi +} + +if [[ "${release}" == *"-pre" ]]; then + message error "This is a pre-release - release aborted." >&2 + message error "Please update the first line in CHANGELOG.md to a version without the -pre tag." + exit 1 +fi + +semver=(${release//./ }) + +for n in 0 1 2; do + if ! [ "${semver[$n]}" -eq "${semver[$n]}" ]; then + message error "${release} is not a proper semantic-versioned release." >&2 + message error "Please update the first line in CHANGELOG.md to a numeric MAJOR.MINOR.PATCH version." + exit 1 + fi +done + +changelog_status=$(git status -s | grep CHANGELOG.md) + +set -e + +if [ "${changelog_status}" == " M CHANGELOG.md" ]; then + git add CHANGELOG.md + changelog_status=$(git status -s | grep CHANGELOG.md) +fi + +if [ "${changelog_status}" == "M CHANGELOG.md" ]; then + message begin "==> Committing CHANGELOG.md <==" + git commit -m "$(echo -e "Release v${release}\n\nSee CHANGELOG.md for more details.")" +fi + + +message begin "==> Tagging Release v${release} <==" +git tag "v${release}" -m "$(echo -e "Release v${release}\n\nSee CHANGELOG.md for more details.")" + +new_prerelease="${semver[0]}.${semver[1]}.$((semver[2]+1))-pre" + +message begin "==> Bumping CHANGELOG.md to Release v${new_prerelease} <==" +echo -e "## ${new_prerelease}\n\nBumped version for dev.\n\n$(cat CHANGELOG.md)" > CHANGELOG.md + +git add CHANGELOG.md +git commit -m "Bump CHANGELOG.md to v${new_prerelease}" + +message begin "==> Pushing Commits and Tags <==" +git push origin master +git push origin --tags + +message ok "\nRelease v${release} successful."