Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to automated "dot" releases in release.sh #310

Merged
merged 3 commits into from
Dec 12, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
84 changes: 80 additions & 4 deletions scripts/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ function tag_images_in_yaml() {
# Parameters: $1 - yaml file to copy.
function publish_yaml() {
function verbose_gsutil_cp {
local DEST=gs://${RELEASE_GCS_BUCKET}/$2/
local DEST="gs://${RELEASE_GCS_BUCKET}/$2/"
echo "Publishing $1 to ${DEST}"
gsutil cp $1 ${DEST}
}
Expand All @@ -66,6 +66,29 @@ RELEASE_GCS_BUCKET=""
KO_FLAGS=""
export KO_DOCKER_REPO=""

# Convenience function to run the hub tool.
# Parameters: $1..$n - arguments to hub.
function hub_tool() {
run_go_tool github.com/github/hub hub $@
}

# Return the master version of a release.
# For example, "v0.2.1" returns "0.2"
# Parameters: $1 - release version label.
function master_version() {
local release="${1//v/}"
local tokens=(${release//\./ })
echo "${tokens[0]}.${tokens[1]}"
}

# Return the release build number of a release.
# For example, "v0.2.1" returns "1".
# Parameters: $1 - release version label.
function release_build_number() {
local tokens=(${1//\./ })
echo "${tokens[2]}"
}

# Parses flags and sets environment variables accordingly.
function parse_flags() {
TAG=""
Expand All @@ -77,16 +100,25 @@ function parse_flags() {
RELEASE_GCS_BUCKET="knative-nightly/$(basename ${REPO_ROOT_DIR})"
local has_gcr_flag=0
local has_gcs_flag=0
local is_dot_release=0

cd ${REPO_ROOT_DIR}
while [[ $# -ne 0 ]]; do
local parameter=$1
case $parameter in
case ${parameter} in
--skip-tests) SKIP_TESTS=1 ;;
--tag-release) TAG_RELEASE=1 ;;
--notag-release) TAG_RELEASE=0 ;;
--publish) PUBLISH_RELEASE=1 ;;
--nopublish) PUBLISH_RELEASE=0 ;;
--dot-release) is_dot_release=1 ;;
--github-token)
shift
[[ $# -ge 1 ]] || abort "missing token file after --github-token"
[[ ! -f "$1" ]] && abort "file $1 doesn't exist"
export GITHUB_TOKEN="$(cat $1)"
[[ -n "${GITHUB_TOKEN}" ]] || abort "file $1 is empty"
;;
--release-gcr)
shift
[[ $# -ge 1 ]] || abort "missing GCR after --release-gcr"
Expand Down Expand Up @@ -122,6 +154,51 @@ function parse_flags() {
shift
done

# Setup dot releases
if (( is_dot_release )); then
echo "Dot release requested"
TAG_RELEASE=1
PUBLISH_RELEASE=1
adrcunha marked this conversation as resolved.
Show resolved Hide resolved
# List latest release
local releases
adrcunha marked this conversation as resolved.
Show resolved Hide resolved
releases="$(hub_tool release)"
[[ $? -eq 0 ]] || abort "cannot list releases"
# If --release-branch passed, restrict to that release
if [[ -n "${RELEASE_BRANCH}" ]]; then
local version_filter="v${RELEASE_BRANCH##release-}"
echo "Dot release will be generated for ${version_filter}"
releases="$(echo "${releases}" | grep ^${version_filter})"
fi
local last_version="$(echo "${releases}" | grep '^v[0-9]\+\.[0-9]\+\.[0-9]\+$' | sort -r | head -1)"
[[ -n "${last_version}" ]] || abort "no previous release exist"
if [[ -z "${RELEASE_BRANCH}" ]]; then
echo "Last release is ${last_version}"
# Determine branch
local major_minor_version="$(master_version ${last_version})"
RELEASE_BRANCH="release-${major_minor_version}"
echo "Last release branch is ${RELEASE_BRANCH}"
fi
# Ensure there are new commits in the branch, otherwise we don't create a new release
local last_release_commit="$(git rev-list -n 1 ${last_version})"
local release_branch_commit="$(git rev-list -n 1 ${RELEASE_BRANCH})"
if [[ "${last_release_commit}" == "${release_branch_commit}" ]]; then
echo "*** Branch ${RELEASE_BRANCH} is at commit ${release_branch_commit}"
echo "*** Branch ${RELEASE_BRANCH} has no new cherry-picks since release ${last_version}"
echo "*** No dot release will be generated, as no changes exist"
exit 0
fi
# Create new release version number
local last_build="$(release_build_number ${last_version})"
RELEASE_VERSION="${major_minor_version}.$(( last_build + 1 ))"
echo "Will create release ${RELEASE_VERSION} at commit ${release_branch_commit}"
# If --release-notes not used, copy from the latest release
if [[ -z "${RELEASE_NOTES}" ]]; then
RELEASE_NOTES="$(mktemp)"
hub_tool release show -f "%b" ${last_version} > ${RELEASE_NOTES}
echo "Release notes from ${last_version} copied to ${RELEASE_NOTES}"
fi
fi

# Update KO_DOCKER_REPO and KO_FLAGS if we're not publishing.
if (( ! PUBLISH_RELEASE )); then
(( has_gcr_flag )) && echo "Not publishing the release, GCR flag is ignored"
Expand Down Expand Up @@ -173,7 +250,6 @@ function run_validation_tests() {
# Initialize everything (flags, workspace, etc) for a release.
function initialize() {
parse_flags $@

# Log what will be done and where.
banner "Release configuration"
echo "- Destination GCR: ${KO_DOCKER_REPO}"
Expand Down Expand Up @@ -219,7 +295,7 @@ function branch_release() {
fi
git tag -a ${TAG} -m "${title}"
git push $(git remote get-url upstream) tag ${TAG}
run_go_tool github.com/github/hub hub release create \
hub_tool release create \
--prerelease \
${attachments[@]} \
--file=${description} \
Expand Down
7 changes: 7 additions & 0 deletions test/unit/release-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ function call_function_post() {
eval ${post}
}

echo ">> Testing helper functions"

test_function 0 "0.2" master_version "v0.2.1"
test_function 0 "0.2" master_version "0.2.1"
test_function 0 "1" release_build_number "v0.2.1"
test_function 0 "1" release_build_number "0.2.1"

echo ">> Testing initialization"

test_function 1 "error: missing version" initialize --version
Expand Down