Skip to content

Commit

Permalink
Remove increment input from workflow
Browse files Browse the repository at this point in the history
- Autodetect if we need to increment the version or not on the release branch
Expected behaviour :
- Running the workflow multiple times without a new commit will rebuild and republish the same artifacts
- Running the workflow after a new commit was made to the releases branch will increment version_minor by 1
  - the version_minor is updated in CMakeLists.txt and committed to the release branch
  - a new tag is created and pushed with the incremented version_minor
- Make logic for both release and other branches more obvious

 --> TODO?: instead of force pushing, we could remove -f option if branch or tag already exist
  • Loading branch information
gr0vity-dev committed Jul 11, 2023
1 parent b7e979a commit 2e887b4
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 31 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/artifacts_build_deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ on:
schedule:
- cron: "0 0 * * 3,6"
workflow_dispatch:
inputs:
increment:
description: "increment version_minor (releases/v{} branch) or version_pre_release (all other branches)"
default: "1"
required: true

env:
artifact: 1

Expand All @@ -25,7 +21,7 @@ jobs:
id: tag_set
run: |
output_var_file="variable_list.txt"
ci/actions/generate_next_git_tag.sh -c -o "${output_var_file}" -i ${{ github.event.inputs.increment }}
ci/actions/generate_next_git_tag.sh -c -o "${output_var_file}"
CI_TAG=$(grep 'build_tag' ${output_var_file} | cut -d= -f2)
echo "CI_TAG=${CI_TAG}" >> $GITHUB_OUTPUT
TAG_CREATED=$(grep 'tag_created' ${output_var_file} | cut -d= -f2)
Expand Down
66 changes: 41 additions & 25 deletions ci/actions/generate_next_git_tag.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@
# ${branch_name} is converted to "DB" if the script operates on develop branch. (e.g first tag for V26: V26.0DB1)
# if -c flag is provided, version_pre_release in CMakeLists.txt is incremented and a new tag is created and pushed to origin
# if -o is provided, "build_tag" , "version_pre_release" and "tag_created" are written to file
# -i flag defines the incrementing. If the script runs on a "releases/v{Major}" branch, it increments minor_version.
# for all other non release branches it increments pre_release_vesrion.
# If it executes on a release-branch :
# --> if there is no new commit, the same tag is generated again
# --> If there is a new commit compared to the previous tag, we would increment the minor version by 1 and build the new binaries & docker images

#!/bin/bash

Expand All @@ -16,19 +17,15 @@ set -x
output=""
create=false
tag_created="false"
increment=1

while getopts ":o:ci:" opt; do
while getopts ":o:c" opt; do
case ${opt} in
o )
output=$OPTARG
;;
c )
create=true
;;
i )
increment=$OPTARG
;;
\? )
echo "Invalid Option: -$OPTARG" 1>&2
exit 1
Expand Down Expand Up @@ -132,15 +129,20 @@ current_commit_hash=$(git rev-parse HEAD)
# Fetch branch name
branch_name=$(git rev-parse --abbrev-ref HEAD)

# Determine if it's a release branch or not
is_release_branch=$(echo "$branch_name" | grep -q "releases/v$current_version_major" && echo true || echo false)


# Fetch major and minor version numbers from CMakeLists.txt
current_version_major=$(grep "CPACK_PACKAGE_VERSION_MAJOR" CMakeLists.txt | grep -o "[0-9]\+")
current_version_minor=$(grep "CPACK_PACKAGE_VERSION_MINOR" CMakeLists.txt | grep -o "[0-9]\+")

# Initialize tag suffix and next number
# Initialize tag suffix and next number and increment
tag_suffix=""
next_number=0
increment=1

if [[ "$branch_name" == "releases/v$current_version_major" ]]; then
if [[ $is_release_branch == true ]]; then

tag_type="version_minor"
# Find existing tags for the release branch
Expand All @@ -150,44 +152,53 @@ if [[ "$branch_name" == "releases/v$current_version_major" ]]; then
if [[ -z "$existing_release_tags" ]]; then
# No tag exists yet, use current minor version without incrementing
tag_created="true"
new_tag=$(get_new_release_tag $current_version_major $current_version_minor)
increment=0
else
# Some tags already exist, increment the minor version with the defined $increment
tag_created="true"
next_number=$(get_next_minor_version $current_version_minor $increment)
new_tag=$(get_new_release_tag $current_version_major $next_number)
# Some tags already exist
# Get the commit hash of the latest tag
last_tag=$(echo "$existing_release_tags" | sort -V | tail -n1)
last_tag_commit_hash=$(git rev-list -n 1 $last_tag)

if [[ "$current_commit_hash" == "$last_tag_commit_hash" ]]; then
# The commit hash of the HEAD is the same as the last tag, hence no new commits. No need to increment
tag_created="true"
increment=0
else
# There is a new commit, hence increment the minor version by 1
tag_created="true"
increment=1
fi
fi
next_number=$(get_next_minor_version $current_version_minor $increment)
new_tag=$(get_new_release_tag $current_version_major $next_number)

else
# Non-release branches handling
tag_type="version_pre_release"

tag_suffix=$(get_tag_suffix $branch_name $current_version_major)
base_version="V${current_version_major}.${current_version_minor}${tag_suffix}"
existing_tags=$(git tag --list "${base_version}*" | grep -E "${base_version}[0-9]+$" || true)
last_tag_number=0

if [[ -n "$existing_tags" ]]; then
last_tag=$(echo "$existing_tags" | sort -V | tail -n1)
last_tag_number=$(echo "$last_tag" | awk -F"${tag_suffix}" '{print $2}')
last_tag_commit_hash=$(git rev-list -n 2 $last_tag | tail -n 1)
last_tag_commit_hash=$(git rev-list -n 2 $last_tag | tail -n 1) #ignore the commit that updates the version_pre_release

if [[ "$current_commit_hash" == "$last_tag_commit_hash" ]]; then
echo "No new commits since the last tag. No new tag will be created."
tag_created="false"
else
tag_created="true"
next_number=$(get_next_tag_number $last_tag_number $increment)
new_tag=$(get_new_other_tag $base_version $next_number)
fi
else
tag_created="true"
next_number=1
new_tag=$(get_new_other_tag $base_version $next_number)
next_number=1 #replace the default 99
fi
new_tag=$(get_new_other_tag $base_version $next_number)
fi



update_output_file $new_tag $next_number $tag_created $tag_type

# Skip tag creation if no new commits
Expand All @@ -205,15 +216,20 @@ if [[ $create == true ]]; then
# Update variable in CMakeLists.txt
update_cmake_lists "$tag_type" "$next_number"

# Create commit in case of changes. Return "false" if no changes are detected.
commit_made=$(create_commit)

git tag -a "$new_tag" -m "This tag was created with generate_next_git_tag.sh"
git tag -fa "$new_tag" -m "This tag was created with generate_next_git_tag.sh"
git push origin "$new_tag" -f
echo "The tag $new_tag has been created and pushed."

#Only reset local branch if a commit was made
if [[ "$commit_made" == "true" ]]; then
# If it's a release branch, also push the commit to the branch
if [[ $is_release_branch == true ]]; then
git push origin "$branch_name" -f
echo "The commit has been pushed to the $branch_name branch."
fi

# Only reset local branch if a commit was made and it's not a "releases" branch.
if [[ "$commit_made" == "true" && ! $release_branch ]]; then
git reset --hard HEAD~1
echo "The commit used for the tag does not exist on any branch."
fi
Expand Down

0 comments on commit 2e887b4

Please sign in to comment.