Skip to content

Commit 6102ef0

Browse files
authored
[chore] add update version automation workflow for artifacts release (#684)
* squash commits * apply suggestions from code review
1 parent 635c4e2 commit 6102ef0

File tree

2 files changed

+279
-0
lines changed

2 files changed

+279
-0
lines changed
Lines changed: 237 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,237 @@
1+
#!/bin/bash
2+
set -e
3+
# This script reads current versions and takes optional next versions, and updates the
4+
# version in the specified files. If next version is not provided, it will infer the
5+
# next semantic version (e.g. v0.110.0 -> v0.111.0 or v1.16.0 -> v1.17.0) based on the
6+
# current version(s) read in.
7+
8+
# List of files to update
9+
manifest_files=(
10+
"distributions/otelcol-contrib/manifest.yaml"
11+
"distributions/otelcol/manifest.yaml"
12+
"distributions/otelcol-k8s/manifest.yaml"
13+
"distributions/otelcol-otlp/manifest.yaml"
14+
)
15+
16+
# Function to display usage
17+
usage() {
18+
echo "Usage: $0 [--commit] [--pull-request] [--next-beta-core <next-beta-core>] [--next-beta-contrib <next-beta-contrib>] [--next-stable <next-stable>]"
19+
echo " --next-beta-core: Next beta version of the core component (e.g., v0.111.0)"
20+
echo " --next-beta-contrib: Next beta version of the contrib component (e.g., v0.111.0)"
21+
echo " --next-stable: Next stable version of the core component (e.g., v1.17.0)"
22+
echo
23+
echo " --commit: Commit the changes to a new branch"
24+
echo " --pull-request: Push the changes to the repo and create a draft PR (requires --commit)"
25+
exit 1
26+
}
27+
28+
# Function to validate semantic version and strip leading 'v'
29+
validate_and_strip_version() {
30+
local var_name=$1
31+
local version=${!var_name}
32+
# Strip leading 'v' if present
33+
version=${version#v}
34+
if [[ ! $version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
35+
echo "Invalid version: $version. Must be a semantic version (e.g., 1.2.3)."
36+
exit 1
37+
fi
38+
eval "$var_name='$version'"
39+
}
40+
commit_changes=false
41+
create_pr=false
42+
# Parse named arguments
43+
current_beta_core=$(awk '/^.*go\.opentelemetry\.io\/collector\/.* v0/ {print $4; exit}' distributions/otelcol/manifest.yaml)
44+
current_beta_contrib=$(awk '/^.*github\.com\/open-telemetry\/opentelemetry-collector-contrib\/.* v0/ {print $4; exit}' distributions/otelcol-contrib/manifest.yaml)
45+
current_stable=$(awk '/^.*go\.opentelemetry\.io\/collector\/.* v1/ {print $4; exit}' distributions/otelcol/manifest.yaml)
46+
while [[ "$#" -gt 0 ]]; do
47+
case $1 in
48+
--next-beta-core) next_beta_core="$2"; shift ;;
49+
--next-beta-contrib) next_beta_contrib="$2"; shift ;;
50+
--next-stable) next_stable="$2"; shift ;;
51+
--commit) commit_changes=true ;;
52+
--pull-request) create_pr=true ;;
53+
*) echo "Unknown parameter passed: $1"; usage ;;
54+
esac
55+
shift
56+
done
57+
58+
# Check if --pull-request is passed without --commit
59+
if [ "$create_pr" = true ] && [ "$commit_changes" = false ]; then
60+
echo "--pull-request requires --commit"
61+
usage
62+
fi
63+
64+
# Validate and strip versions
65+
if [ -n "$current_beta_core" ]; then
66+
validate_and_strip_version current_beta_core
67+
fi
68+
if [ -n "$current_beta_contrib" ]; then
69+
validate_and_strip_version current_beta_contrib
70+
fi
71+
if [ -n "$current_stable" ]; then
72+
validate_and_strip_version current_stable
73+
fi
74+
if [ -n "$next_beta_core" ]; then
75+
validate_and_strip_version next_beta_core
76+
fi
77+
if [ -n "$next_beta_contrib" ]; then
78+
validate_and_strip_version next_beta_contrib
79+
fi
80+
if [ -n "$next_stable" ]; then
81+
validate_and_strip_version next_stable
82+
fi
83+
84+
# Function to compare two semantic versions and return the maximum
85+
max_version() {
86+
local version1=$1
87+
local version2=$2
88+
89+
# Strip leading 'v' if present
90+
version1=${version1#v}
91+
version2=${version2#v}
92+
93+
# Split versions into components
94+
IFS='.' read -r -a ver1 <<< "$version1"
95+
IFS='.' read -r -a ver2 <<< "$version2"
96+
97+
# Compare major, minor, and patch versions
98+
for i in {0..2}; do
99+
if [[ ${ver1[i]} -gt ${ver2[i]} ]]; then
100+
echo "$version1"
101+
return
102+
elif [[ ${ver1[i]} -lt ${ver2[i]} ]]; then
103+
echo "$version2"
104+
return
105+
fi
106+
done
107+
108+
# If versions are equal, return either
109+
echo "$version1"
110+
}
111+
112+
# Function to bump the minor version and reset patch version to 0
113+
bump_version() {
114+
local version=$1
115+
local major
116+
major=$(echo "$version" | cut -d. -f1)
117+
local minor
118+
minor=$(echo "$version" | cut -d. -f2)
119+
local new_minor
120+
new_minor=$((minor + 1))
121+
echo "$major.$new_minor.0"
122+
}
123+
124+
# Infer the next beta version if not supplied
125+
if [ -n "$current_beta_core" ] && [ -z "$next_beta_core" ]; then
126+
next_beta_core=$(bump_version "$current_beta_core")
127+
fi
128+
if [ -n "$current_beta_contrib" ] && [ -z "$next_beta_contrib" ]; then
129+
next_beta_contrib=$(bump_version "$current_beta_contrib")
130+
fi
131+
132+
# Determine the maximum of next_beta_core and next_beta_contrib
133+
next_distribution_version=$(max_version "$next_beta_core" "$next_beta_contrib")
134+
validate_and_strip_version next_distribution_version
135+
136+
# Infer the next stable version if current_stable provided and next version not supplied
137+
if [ -n "$current_stable" ] && [ -z "$next_stable" ]; then
138+
next_stable=$(bump_version "$current_stable")
139+
fi
140+
141+
# add escape characters to the current versions to work with sed
142+
escaped_current_beta_core=${current_beta_core//./\\.}
143+
escaped_current_beta_contrib=${current_beta_contrib//./\\.}
144+
escaped_current_stable=${current_stable//./\\.}
145+
146+
# Determine the OS and set the sed command accordingly
147+
if [[ "$OSTYPE" == "darwin"* ]]; then
148+
# macOS
149+
sed_command="sed -i ''"
150+
else
151+
# Linux
152+
sed_command="sed -i"
153+
fi
154+
155+
# Update versions in each manifest file
156+
echo "Updating core beta version from $current_beta_core to $next_beta_core,"
157+
echo "core stable version from $current_stable to $next_stable,"
158+
echo "contrib beta version from $current_beta_contrib to $next_beta_contrib,"
159+
echo "and distribution version to $next_distribution_version"
160+
for file in "${manifest_files[@]}"; do
161+
if [ -f "$file" ]; then
162+
$sed_command "s/\(^.*go\.opentelemetry\.io\/collector\/.*\) v$escaped_current_beta_core/\1 v$next_beta_core/" "$file"
163+
$sed_command "s/\(^.*github\.com\/open-telemetry\/opentelemetry-collector-contrib\/.*\) v$escaped_current_beta_contrib/\1 v$next_beta_contrib/" "$file"
164+
$sed_command "s/\(^.*go\.opentelemetry\.io\/collector\/.*\) v$escaped_current_stable/\1 v$next_stable/" "$file"
165+
$sed_command "s/version: .*/version: $next_distribution_version/" "$file"
166+
else
167+
echo "File $file does not exist"
168+
fi
169+
done
170+
171+
# Update Makefile OCB version
172+
$sed_command "s/OTELCOL_BUILDER_VERSION ?= $escaped_current_beta_core/OTELCOL_BUILDER_VERSION ?= $next_beta_core/" Makefile
173+
174+
echo "Version update completed."
175+
176+
# Make a new changelog update
177+
make chlog-update VERSION="v$next_distribution_version"
178+
179+
# Commit changes and draft PR
180+
if [ "$commit_changes" = false ]; then
181+
echo "Changes not committed and PR not created."
182+
exit 0
183+
fi
184+
185+
git config --global user.name "github-actions[bot]"
186+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
187+
188+
commit_changes() {
189+
local current_version=$1
190+
local next_version=$2
191+
shift 2
192+
local branch_name="update-version-${next_version}"
193+
194+
git checkout -b "$branch_name"
195+
git add .
196+
git commit -m "Update version from $current_version to $next_version"
197+
git push -u origin "$branch_name"
198+
}
199+
200+
create_pr() {
201+
local current_version=$1
202+
local next_version=$2
203+
shift 2
204+
local branch_name="update-version-${next_version}"
205+
206+
gh pr create --title "[chore] Prepare release $next_version" \
207+
--body "This PR updates the version from $current_version to $next_version" \
208+
--base main --head "$branch_name" --draft
209+
}
210+
211+
# TODO: Once Collector 1.0 is released, we can consider removing the
212+
# beta version check for commit and PR creation
213+
if [ -n "$current_beta_core" ]; then
214+
if [ "$commit_changes" = true ]; then
215+
commit_changes "$current_beta_core" "$next_beta_core"
216+
fi
217+
if [ "$create_pr" = true ]; then
218+
create_pr "$current_beta_core" "$next_beta_core"
219+
fi
220+
elif [ -n "$current_beta_contrib" ]; then
221+
if [ "$commit_changes" = true ]; then
222+
commit_changes "$current_beta_contrib" "$next_beta_contrib"
223+
fi
224+
if [ "$create_pr" = true ]; then
225+
create_pr "$current_beta_contrib" "$next_beta_contrib"
226+
fi
227+
else
228+
if [ "$commit_changes" = true ]; then
229+
commit_changes "$current_stable" "$next_stable"
230+
fi
231+
if [ "$create_pr" = true ]; then
232+
create_pr "$current_stable" "$next_stable"
233+
fi
234+
fi
235+
236+
echo "Changes committed and PR created:"
237+
gh pr view
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Update Version in Distributions and Prepare PR
2+
on:
3+
workflow_dispatch:
4+
inputs:
5+
next_beta_core:
6+
description: 'Next collector core beta version (optional)'
7+
required: false
8+
default: ''
9+
next_beta_contrib:
10+
description: 'Next collector contrib beta version (optional)'
11+
required: false
12+
default: ''
13+
next_stable:
14+
description: 'Next stable version (optional)'
15+
required: false
16+
default: ''
17+
create_pr:
18+
description: 'Create a draft pull request with the changes'
19+
required: true
20+
type: boolean
21+
default: true
22+
23+
jobs:
24+
update-version:
25+
runs-on: ubuntu-24.04
26+
env:
27+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
28+
steps:
29+
- name: Checkout repository
30+
uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4.2.2
31+
32+
- name: Run bump-versions.sh
33+
run: |
34+
args=""
35+
if [ "${{ github.event.inputs.create_pr }}" = "true" ]; then
36+
args="--commit --pull-request"
37+
fi
38+
.github/workflows/scripts/bump-versions.sh $args
39+
env:
40+
next_beta_core: ${{ github.event.inputs.next_beta_core }}
41+
next_beta_contrib: ${{ github.event.inputs.next_beta_contrib }}
42+
next_stable: ${{ github.event.inputs.next_stable }}

0 commit comments

Comments
 (0)