Release: Scripts to change versions in all go.mod files and push tags upstream.

Examplar invocations:

Edit go.mod files such that all etcd modules are pointing on given version:

```
% DRY_RUN=false TARGET_VERSION="v3.5.13" ./scripts/release_mod.sh update_versions
```

Tag latest commit with current version number for all the modules and push upstream:
```
% DRY_RUN=true REMOTE_REPO="origin" ./scripts/release_mod.sh push_mod_tags
```
release-3.5
Piotr Tabor 2020-11-04 14:59:00 +01:00
parent 6e800b9b01
commit fd2f34fd13
2 changed files with 164 additions and 14 deletions

124
scripts/release_mod.sh Executable file
View File

@ -0,0 +1,124 @@
#!/usr/bin/env bash
# Examples:
# Edit go.mod files such that all etcd modules are pointing on given version:
#
# % DRY_RUN=false TARGET_VERSION="v3.5.13" ./scripts/release_mod.sh update_versions
# Tag latest commit with current version number for all the modules and push upstream:
#
# % DRY_RUN=false REMOTE_REPO="origin" ./scripts/release_mod.sh push_mod_tags
set -e
DRY_RUN=${DRY_RUN:-true}
if ! [[ "$0" =~ scripts/release_mod.sh ]]; then
echo "must be run from repository root"
exit 255
fi
source ./scripts/test_lib.sh
# _cmd prints help message
function _cmd() {
log_error "Command required: ${0} [cmd]"
log_info "Available commands:"
log_info " - update_versions - Updates all cross-module versions to \${TARGET_VERSION} in the local client."
log_info " - push_mod_tags - Tags HEAD with all modules versions tags and pushes it to \${REMOTE_REPO}."
}
# maybe_run [cmd...] runs given command depending on the DRY_RUN flag.
function maybe_run() {
if ${DRY_RUN}; then
log_warning -e "# DRY_RUN:\n % ${*}"
else
run "${@}"
fi
}
# update_module_version [v2version] [v3version]
# Updates versions of cross-references in all internal references in current module.
function update_module_version() {
local v3version="${1}"
local v2version="${2}"
local modules
modules=$(run go list -f '{{if not .Main}}{{if not .Indirect}}{{.Path}}{{end}}{{end}}' -m all)
v3deps=$(echo "${modules}" | grep -E "${REPO}/.*/v3")
for dep in ${v3deps}; do
maybe_run go mod edit -require "${dep}@${v3version}"
done
v2deps=$(echo "${modules}" | grep -E "${REPO}/.*/v2")
for dep in ${v2deps}; do
maybe_run go mod edit -require "${dep}@${v2version}"
done
}
# Updates all cross-module versions to ${TARGET_VERSION} in local client.
function update_versions_cmd() {
assert_no_git_modifications || return 2
if [ -z "${TARGET_VERSION}" ]; then
log_error "TARGET_VERSION environment variable not set. Set it to e.g. v3.5.10-alpha.0"
return 2
fi
local v3version="${TARGET_VERSION}"
local v2version
# converts e.g. v3.5.0-alpha.0 --> v2.305.0-alpha.0
# shellcheck disable=SC2001
v2version="$(echo "${TARGET_VERSION}" | sed 's|^v3.\([0-9]*\).|v2.30\1.|g')"
log_info "DRY_RUN : ${DRY_RUN}"
log_info "TARGET_VERSION: ${TARGET_VERSION}"
log_info ""
log_info "v3version: ${v3version}"
log_info "v2version: ${v2version}"
run_for_modules update_module_version "${v3version}" "${v2version}"
}
function push_mod_tags_cmd {
assert_no_git_modifications || return 2
if [ -z "${REMOTE_REPO}" ]; then
log_error "REMOTE_REPO environment variable not set"
return 2
fi
log_info "REMOTE_REPO: ${REMOTE_REPO}"
# Any module ccan be used for this
local master_version
master_version=$(go list -f '{{.Version}}' -m "${REPO}/api/v3")
local tags=()
for module in $(modules); do
local version
version=$(go list -f '{{.Version}}' -m "${module}")
local path
path=$(go list -f '{{.Path}}' -m "${module}")
local subdir="${path//${REPO}\//}"
local tag
if [ -z "${version}" ]; then
tag="${master_version}"
version="${master_version}"
else
tag="${subdir///v[23]/}/${version}"
fi
log_info "Tags for: ${module} version:${version} tag:${tag}"
maybe_run git tag -f "${tag}"
tags=("${tags[@]}" "${tag}")
done
maybe_run git push -f "${REMOTE_REPO}" "${tags[@]}"
}
"${1}_cmd"
if "${DRY_RUN}"; then
log_info
log_warning "WARNING: It was a DRY_RUN. No files were modified."
fi

View File

@ -1,11 +1,13 @@
#!/usr/bin/env bash
if [[ "$(go list)" != "go.etcd.io/etcd/v3" ]]; then
echo "must be run from 'go.etcd.io/etcd/v3' module directory"
REPO="go.etcd.io/etcd"
if [[ "$(go list)" != "${REPO}/v3" ]]; then
echo "must be run from '${REPO}/v3' module directory"
exit 255
fi
ETCD_ROOT_DIR=$(go list -f '{{.Dir}}' "go.etcd.io/etcd/v3")
ETCD_ROOT_DIR=$(go list -f '{{.Dir}}' "${REPO}/v3")
#### Convenient IO methods #####
@ -48,6 +50,12 @@ function log_success {
>&2 echo -n -e "${COLOR_NONE}"
}
function log_info {
>&2 echo -n -e "${COLOR_NONE}"
>&2 echo "$@"
>&2 echo -n -e "${COLOR_NONE}"
}
# From http://stackoverflow.com/a/12498485
function relativePath {
# both $1 and $2 are absolute paths beginning with /
@ -105,6 +113,11 @@ function pkgs_in_module {
go list -mod=mod "${1:-./...}";
}
# Prints subdirectory (from the repo root) for the current module.
function module_subdir {
relativePath "${ETCD_ROOT_DIR}" "${PWD}"
}
#### Running actions against multiple modules ####
# run [command...] - runs given command, printing it first and
@ -114,11 +127,11 @@ function pkgs_in_module {
function run {
local rpath
local command
rpath=$(relativePath "${ETCD_ROOT_DIR}" "${PWD}")
rpath=$(module_subdir)
# Quoting all components as the commands are fully copy-parsable:
command=("${@}")
command=("${command[@]@Q}")
if [ "${rpath}" != "." ]; then
if [[ "${rpath}" != "." && "${rpath}" != "" ]]; then
repro="(cd ${rpath} && ${command[*]})"
else
repro="${command[*]}"
@ -147,15 +160,15 @@ function run_for_module {
function modules() {
modules=(
go.etcd.io/etcd/api/v3
go.etcd.io/etcd/pkg/v3
go.etcd.io/etcd/raft/v3
go.etcd.io/etcd/client/v2
go.etcd.io/etcd/client/v3
go.etcd.io/etcd/server/v3
go.etcd.io/etcd/etcdctl/v3
go.etcd.io/etcd/tests/v3
go.etcd.io/etcd/v3)
"${REPO}/api/v3"
"${REPO}/pkg/v3"
"${REPO}/raft/v3"
"${REPO}/client/v2"
"${REPO}/client/v3"
"${REPO}/server/v3"
"${REPO}/etcdctl/v3"
"${REPO}/tests/v3"
"${REPO}/v3")
echo "${modules[@]}"
}
@ -313,3 +326,16 @@ function run_go_tool {
run "${cmdbin}" "$@" || return 2
}
# assert_no_git_modifications fails if there are any uncommited changes.
function assert_no_git_modifications {
log_callout "Making sure everything is committed."
if ! git diff --cached --exit-code; then
log_error "Found staged by uncommited changes. Do commit/stash your changes first."
return 2
fi
if ! git diff --exit-code; then
log_error "Found unstaged and uncommited changes. Do commit/stash your changes first."
return 2
fi
}