Skip to content

Commit 694c780

Browse files
authored
chore: update release process (#100)
* fix: create one single commit and point all tags to it * chore: add EOF * chore: exit release if there are no changes * chore: extract module URL to variable * chore: extract next-tag path calculation to a function * fix: parse version file correctly * chore: clean build dir * feat: enable local release before creating the real one * chore: wording and comments
1 parent 998c10e commit 694c780

File tree

8 files changed

+85
-33
lines changed

8 files changed

+85
-33
lines changed

.github/scripts/common.sh

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# find_latest_tag - Find latest tag for a given module
1616
# get_modules - Get list of modules from go.work file
1717
# get_script_dir - Get directory of the calling script
18+
# portable_sed - Portable in-place sed editing
1819
#
1920
# Constants:
2021
# ROOT_DIR - Root directory of the repository
@@ -36,6 +37,7 @@ get_script_dir() {
3637

3738
readonly CURRENT_DIR="$(get_script_dir)"
3839
readonly ROOT_DIR="$(dirname $(dirname "${CURRENT_DIR}"))"
40+
readonly BUILD_DIR="${ROOT_DIR}/.github/scripts/.build"
3941
readonly GITHUB_REPO="github.com/docker/go-sdk"
4042
readonly DRY_RUN="${DRY_RUN:-true}"
4143

@@ -50,12 +52,9 @@ function curlGolangProxy() {
5052
# github.com/docker/go-sdk/client/v0.0.1.info
5153
# github.com/docker/go-sdk/client/v0.1.0.info
5254
# github.com/docker/go-sdk/client/v1.0.0.info
53-
if [[ "${DRY_RUN}" == "true" ]]; then
54-
echo "[DRY RUN] Would execute: curl https://proxy.golang.org/${GITHUB_REPO}/${module}/@v/${module_version}.info"
55-
return
56-
fi
55+
local module_url="https://proxy.golang.org/${GITHUB_REPO}/${module}/@v/${module_version}.info"
5756

58-
curl "https://proxy.golang.org/${GITHUB_REPO}/${module}/@v/${module_version}.info"
57+
execute_or_echo curl "${module_url}"
5958
}
6059

6160

@@ -79,16 +78,18 @@ find_latest_tag() {
7978
git tag --list | grep -E "${module}/v[0-9]+\.[0-9]+\.[0-9]+.*" | sort -V | tail -n 1
8079
}
8180

81+
# Function to get the next tag for a module
82+
get_next_tag() {
83+
local module="$1"
84+
local next_tag_path="${BUILD_DIR}/${module}-next-tag"
85+
echo "${next_tag_path}"
86+
}
87+
8288
# Portable in-place sed editing that works on both BSD (macOS) and GNU (Linux) sed
8389
portable_sed() {
8490
local pattern="$1"
8591
local file="$2"
8692

87-
if [[ "$DRY_RUN" == "true" ]]; then
88-
echo "[DRY RUN] Would execute: sed '$pattern' in $file"
89-
return
90-
fi
91-
9293
# Detect sed version and use appropriate syntax
9394
if sed --version >/dev/null 2>&1; then
9495
# GNU sed (Linux)
@@ -97,4 +98,4 @@ portable_sed() {
9798
# BSD sed (macOS)
9899
sed -i '' "$pattern" "$file"
99100
fi
100-
}
101+
}

.github/scripts/pre-release.sh

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,13 @@ MODULE=$(echo "$MODULE" | tr '[:upper:]' '[:lower:]')
110110
if [[ "$DRY_RUN" == "true" ]]; then
111111
echo "[DRY RUN] Would update ${ROOT_DIR}/${MODULE}/version.go with version: ${NEXT_VERSION}"
112112
else
113-
portable_sed "s/version = \"[^\"]*\"/version = \"${NEXT_VERSION}\"/" "${ROOT_DIR}/${MODULE}/version.go"
113+
# Regex explanation:
114+
# ^([[:space:]]*version[[:space:]]*=[[:space:]]*)\"[^\"]*\"
115+
# - ^ : Start of line
116+
# - ([[:space:]]*version[[:space:]]*=[[:space:]]*) : Group 1 matches any leading whitespace, the word 'version', optional whitespace, '=', optional whitespace
117+
# - \"[^\"]*\" : Matches a quoted string (the version value)
118+
# The replacement keeps the left part and replaces the quoted value with the new version.
119+
portable_sed "s/^\([[:space:]]*version[[:space:]]*=[[:space:]]*\)\"[^\"]*\"/\1\"${NEXT_VERSION}\"/" "${ROOT_DIR}/${MODULE}/version.go"
114120
fi
115121

116122
# if next version does not start with v, add it
@@ -133,14 +139,10 @@ echo "Next tag: ${NEXT_TAG}"
133139
MODULES=$(go work edit -json | jq -r '.Use[] | "\(.DiskPath | ltrimstr("./"))"' | tr '\n' ' ' && echo)
134140

135141
# Save the next tag for the module to a file so that the release script can use it
136-
execute_or_echo echo "${NEXT_vVERSION}" > "${ROOT_DIR}/.github/scripts/.${MODULE}-next-tag"
142+
echo "${NEXT_vVERSION}" > "$(get_next_tag "${MODULE}")"
137143

138144
for m in $MODULES; do
139-
if [[ "$DRY_RUN" == "true" ]]; then
140-
echo "[DRY RUN] Would update ${ROOT_DIR}/${m}/go.mod: ${GITHUB_REPO}/${MODULE} v${NEXT_VERSION}"
141-
else
142-
portable_sed "s|${GITHUB_REPO}/${MODULE} v[^[:space:]]*|${GITHUB_REPO}/${MODULE} v${NEXT_VERSION}|g" "${ROOT_DIR}/${m}/go.mod"
143-
# Update the go.sum file
144-
(cd "${ROOT_DIR}/${m}" && execute_or_echo go mod tidy)
145-
fi
145+
portable_sed "s|${GITHUB_REPO}/${MODULE} v[^[:space:]]*|${GITHUB_REPO}/${MODULE} v${NEXT_VERSION}|g" "${ROOT_DIR}/${m}/go.mod"
146+
# Update the go.sum file
147+
(cd "${ROOT_DIR}/${m}" && go mod tidy)
146148
done

.github/scripts/release.sh

Lines changed: 47 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
#
1313
# Environment Variables:
1414
# DRY_RUN - Enable dry run mode (default: true)
15-
# When true, shows git commands without executing them
15+
# When true, shows commands without executing them, except for git commands
16+
# that are executed but before pushing the changes to the remote repository.
1617
#
1718
# Examples:
1819
# ./.github/scripts/release.sh
@@ -41,28 +42,65 @@ set -e
4142
readonly SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
4243
source "${SCRIPT_DIR}/common.sh"
4344

45+
# Collect and stage changes across modules, then create a single commit
4446
MODULES=$(go work edit -json | jq -r '.Use[] | "\(.DiskPath | ltrimstr("./"))"' | tr '\n' ' ' && echo)
47+
48+
commit_title="chore(release): bump module versions"
49+
commit_body=""
50+
4551
for m in $MODULES; do
52+
next_tag_path=$(get_next_tag "${m}")
4653
# if the module version file does not exist, skip it
47-
if [[ ! -f "${ROOT_DIR}/.github/scripts/.${m}-next-tag" ]]; then
54+
if [[ ! -f "${next_tag_path}" ]]; then
4855
echo "Skipping ${m} because the pre-release script did not run"
4956
continue
5057
fi
5158

52-
execute_or_echo git add "${ROOT_DIR}/${m}/version.go"
53-
execute_or_echo git add "${ROOT_DIR}/${m}/go.mod"
59+
git add "${ROOT_DIR}/${m}/version.go"
60+
git add "${ROOT_DIR}/${m}/go.mod"
61+
if [[ -f "${ROOT_DIR}/${m}/go.sum" ]]; then
62+
git add "${ROOT_DIR}/${m}/go.sum"
63+
fi
5464

55-
nextTag=$(cat "${ROOT_DIR}/.github/scripts/.${m}-next-tag")
65+
nextTag=$(cat "${next_tag_path}")
5666
echo "Next tag for ${m}: ${nextTag}"
57-
execute_or_echo git commit -m "chore(${m}): bump version to ${nextTag}"
67+
commit_body="${commit_body}\n - ${m}: ${nextTag}"
68+
done
69+
70+
# Create a single commit if there are staged changes
71+
if [[ -n "$(git diff --cached)" ]]; then
72+
git commit -m "${commit_title}" -m "$(echo -e "${commit_body}")"
73+
else
74+
echo "No changes detected in modules. Release process aborted."
75+
exit 1 # exit with error code 1 to not proceed with the release
76+
fi
5877

59-
execute_or_echo git tag "${m}/${nextTag}"
78+
# Create all tags after the single commit
79+
for m in $MODULES; do
80+
next_tag_path=$(get_next_tag "${m}")
81+
if [[ -f "${next_tag_path}" ]]; then
82+
nextTag=$(cat "${next_tag_path}")
83+
git tag "${m}/${nextTag}"
84+
fi
6085
done
6186

87+
if [[ "${DRY_RUN}" == "true" ]]; then
88+
echo "Remote operations will be skipped."
89+
# show the last commit, including the patch
90+
echo "Last commit:"
91+
git_log_format='%C(auto)%h%C(reset) %s%nAuthor: %an <%ae>%nDate: %ad'
92+
git -C "${ROOT_DIR}" --no-pager log -1 --pretty=format:"${git_log_format}" --date=iso-local
93+
git -C "${ROOT_DIR}" --no-pager show -1 --format= --patch --stat
94+
# list the new tags, that should point to the same last commit
95+
echo "New tags:"
96+
git -C "${ROOT_DIR}" --no-pager tag --list --points-at HEAD
97+
fi
98+
99+
echo "Pushing changes and tags to remote repository..."
100+
62101
execute_or_echo git push origin main --tags
63102

64103
for m in $MODULES; do
65-
nextTag=$(cat "${ROOT_DIR}/.github/scripts/.${m}-next-tag")
104+
nextTag=$(cat $(get_next_tag "${m}"))
66105
curlGolangProxy "${m}" "${nextTag}"
67-
execute_or_echo rm "${ROOT_DIR}/.github/scripts/.${m}-next-tag"
68106
done

.github/workflows/release-modules.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ jobs:
6666
env:
6767
DRY_RUN: ${{ inputs.dry_run }}
6868
BUMP_TYPE: ${{ inputs.bump_type }}
69+
LOCAL_RELEASE: false
6970
run: |
7071
echo "Starting release process..."
7172
make release-all

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
.github/scripts/.*-next-tag
1+
.github/scripts/.build
22
coverage.out
33
output.txt
44
TEST-unit.xml

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ tidy-all:
1515
@echo "Running tidy in all modules..."
1616
$(call for-all-modules,go mod tidy)
1717

18+
clean-build-dir:
19+
@echo "Cleaning build directory..."
20+
@rm -rf .github/scripts/.build
21+
@mkdir -p .github/scripts/.build
22+
1823
# Release version for all modules
19-
release-all:
24+
release-all: clean-build-dir
2025
@echo "Preparing releasing versions for all modules..."
2126
$(call for-all-modules,make pre-release)
2227

RELEASING.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ DRY_RUN=false make release-all
6767
```
6868

6969
#### Environment Variables
70-
- `DRY_RUN`: `true` (default) or `false`
70+
- `DRY_RUN`: `true` (default) or `false`. It generates the release (commit and tags) locally, without pushing changes to the remote repository.
7171
- `BUMP_TYPE`: `prerelease` (default), `patch`, `minor`, or `major`. To know more about the bump type values, please read more [here](https://github.com/fsaintjacques/semver-tool).
7272

7373
## Release Types
@@ -102,6 +102,7 @@ DRY_RUN=false make release-all
102102

103103
### 2. File Updates
104104
For each module:
105+
- Writes the next version to a file in the build directory, located at `.github/scripts/.build/<module>-next-tag`. This is a temporary file that is used to store the next version for the module.
105106
- Updates `<module>/version.go` with new version
106107
- Updates all `go.mod` files with new cross-module dependencies
107108
- Runs `go mod tidy` to update `go.sum` files
@@ -111,10 +112,14 @@ For each module:
111112
- Creates git tags for each module (e.g., `client/v0.1.0-alpha006`)
112113
- Pushes changes and tags to GitHub
113114

115+
If `DRY_RUN` is `true`, the script does not push changes and tags to the remote repository.
116+
114117
### 4. Go Proxy Registration
115118
- Triggers Go proxy to fetch new module versions
116119
- Makes modules immediately available for download
117120

121+
If `DRY_RUN` is `true`, the script does not trigger the Go proxy.
122+
118123
## Troubleshooting
119124

120125
### Common Issues

commons-test.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,4 +84,4 @@ pre-release:
8484
exit 1; \
8585
fi
8686
@echo "Releasing version for module: $(MODULE_DIR)"
87-
@$(ROOT_DIR)/.github/scripts/pre-release.sh "$(MODULE_DIR)"
87+
@$(ROOT_DIR)/.github/scripts/pre-release.sh "$(MODULE_DIR)"

0 commit comments

Comments
 (0)