From 49d05f88c31c71a48368bf20927cd8ba61a3bf1a Mon Sep 17 00:00:00 2001 From: Benjamin Wang Date: Sat, 26 Nov 2022 19:17:52 +0800 Subject: [PATCH] [3.4] Backport cherry-pick of #14860: Trigger release in current branch for github workflow case Signed-off-by: ArkaSaha30 --- .github/workflows/release.yaml | 2 +- scripts/build-binary.sh | 2 +- scripts/release.sh | 79 ++++++++++++++++++++++++++-------- 3 files changed, 64 insertions(+), 19 deletions(-) diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 4ba2b41cbb3..6f2abd5d8a9 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -23,4 +23,4 @@ jobs: Name-Email: github-action@etcd.io Expire-Date: 0 EOF - DRY_RUN=true ./scripts/release.sh --no-upload --no-docker-push 3.4.99 + DRY_RUN=true ./scripts/release.sh --no-upload --no-docker-push --in-place 3.4.99 diff --git a/scripts/build-binary.sh b/scripts/build-binary.sh index cc757a65481..91352183a19 100755 --- a/scripts/build-binary.sh +++ b/scripts/build-binary.sh @@ -6,7 +6,7 @@ source ./scripts/test_lib.sh VER=$1 PROJ="etcd" -REPOSITORY="${REPOSITORY:-https://github.com/etcd-io/etcd.git}" +REPOSITORY="${REPOSITORY:-git@github.com:etcd-io/etcd.git}" if [ -z "$1" ]; then echo "Usage: ${0} VERSION" >> /dev/stderr diff --git a/scripts/release.sh b/scripts/release.sh index d68f9015a7e..1d8a9fb8e58 100755 --- a/scripts/release.sh +++ b/scripts/release.sh @@ -23,6 +23,7 @@ help() { echo " flags:" echo " --no-upload: skip gs://etcd binary artifact uploads." echo " --no-docker-push: skip docker image pushes." + echo " --in-place: build binaries using current branch." echo "" } @@ -34,7 +35,15 @@ main() { fi RELEASE_VERSION="v${VERSION}" MINOR_VERSION=$(echo "${VERSION}" | cut -d. -f 1-2) - BRANCH="release-${MINOR_VERSION}" + + if [ "${IN_PLACE}" == 1 ]; then + # Trigger release in current branch + REPOSITORY=$(pwd) + BRANCH=$(git rev-parse --abbrev-ref HEAD) + else + REPOSITORY=${REPOSITORY:-"https://github.com/etcd-io/etcd.git"} + BRANCH=${BRANCH:-"release-${MINOR_VERSION}"} + fi log_warning "DRY_RUN=${DRY_RUN}" log_callout "RELEASE_VERSION=${RELEASE_VERSION}" @@ -53,13 +62,17 @@ main() { # Set up release directory. local reldir="/tmp/etcd-release-${VERSION}" log_callout "Preparing temporary directory: ${reldir}" - if [ ! -d "${reldir}/etcd" ]; then + if [ ! -d "${reldir}/etcd" ] && [ "${IN_PLACE}" == 0 ]; then mkdir -p "${reldir}" cd "${reldir}" - git clone https://github.com/etcd-io/etcd.git --branch "${BRANCH}" - fi - cd "${reldir}/etcd" + git clone "${REPOSITORY}" --branch "${BRANCH}" + cd "${reldir}/etcd" || exit 2 + git checkout "${BRANCH}" || exit 2 + git pull origin + git_assert_branch_in_sync || exit 2 + fi + # If a release version tag already exists, use it. log_callout "Checking tag: ${RELEASE_VERSION}" local remote_tag_exists @@ -80,6 +93,7 @@ main() { fi # If the release tag does not already exist remotely, create it. + log_callout "Create tag if not present" if [ "${remote_tag_exists}" -eq 0 ]; then # Bump version/version.go to release version. local source_version @@ -140,22 +154,41 @@ main() { exit 1 fi - # Verify the version tag is on the right branch - # shellcheck disable=SC2155 - local branch=$(git for-each-ref --contains "${RELEASE_VERSION}" --format="%(refname)" 'refs/heads' | cut -d '/' -f 3) - if [ "${branch}" != "release-${MINOR_VERSION}" ]; then - log_error "Error: Git tag ${RELEASE_VERSION} should be on branch release-${MINOR_VERSION} but is on ${branch}" - exit 1 + if [ "${IN_PLACE}" == 0 ]; then + # Tried with `local branch=$(git branch -a --contains tags/"${RELEASE_VERSION}")` + # so as to work with both current branch and main/release-3.X. + # But got error below on current branch mode, + # Error: Git tag v3.6.99 should be on branch '* (HEAD detached at pull/14860/merge)' but is on '* (HEAD detached from pull/14860/merge)' + # + # Verify the version tag is on the right branch + # shellcheck disable=SC2155 + local branch=$(git for-each-ref --contains "${RELEASE_VERSION}" --format="%(refname)" 'refs/heads' | cut -d '/' -f 3) + if [ "${branch}" != "${BRANCH}" ]; then + log_error "Error: Git tag ${RELEASE_VERSION} should be on branch '${BRANCH}' but is on '${branch}'" + exit 1 + fi fi + fi - # Push the tag change if it's not already been pushed. - if [ "$DRY_RUN" != "true" ]; then - read -p "Push etcd ${RELEASE_VERSION} tag [y/N]? " -r confirm - [[ "${confirm,,}" == "y" ]] || exit 1 - git push origin "tags/${RELEASE_VERSION}" - fi + log_callout "Verify the latest commit has the version tag" + # Verify the latest commit has the version tag + # shellcheck disable=SC2155 + local tag="$(git describe --exact-match HEAD)" + if [ "${tag}" != "${RELEASE_VERSION}" ]; then + log_error "Error: Expected HEAD to be tagged with ${RELEASE_VERSION}, but 'git describe --exact-match HEAD' reported: ${tag}" + exit 1 fi + log_callout "Verify the work space is clean" + # Verify the clean working tree + # shellcheck disable=SC2155 + local diff="$(git diff HEAD --stat)" + if [[ "${diff}" != '' ]]; then + log_error "Error: Expected clean working tree, but 'git diff --stat' reported: ${diff}" + exit 1 + fi + + # Build release. # TODO: check the release directory for all required build artifacts. if [ -d release ]; then @@ -261,6 +294,7 @@ main() { POSITIONAL=() NO_UPLOAD=0 NO_DOCKER_PUSH=0 +IN_PLACE=0 while test $# -gt 0; do case "$1" in @@ -269,6 +303,10 @@ while test $# -gt 0; do help exit 0 ;; + --in-place) + IN_PLACE=1 + shift + ;; --no-upload) NO_UPLOAD=1 shift @@ -290,4 +328,11 @@ if [[ ! $# -eq 1 ]]; then exit 1 fi +# Note that we shouldn't upload artifacts in --in-place mode, so it +# must be called with DRY_RUN=true +if [ "${DRY_RUN}" != "true" ] && [ "${IN_PLACE}" == 1 ]; then + log_error "--in-place should only be called with DRY_RUN=true" + exit 1 +fi + main "$1"