Skip to content

Commit

Permalink
ci: add version calc scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jwatson0 committed Feb 9, 2024
1 parent ab151e5 commit d5bc92a
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
65 changes: 65 additions & 0 deletions ci/calc-slm-version-bump.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#!/bin/bash
set -Eeuxo pipefail
PS4='>>> '
TOP="${PWD}"

# This script is designed to be run from a Concourse Task with the following env vars
# outputs the bumped version to stdout

# Required env var inputs
>&2 echo " BRANCH:" "${BRANCH:?Usage: BRANCH=foo $0}"

# Defaulted env var inputs - can override if necessary
>&2 echo " REPODIR:" "${REPODIR:=slm}"
## By default, get the most recent previous tag on this branch from git
### note: if multiple tags exist on one commit, git doesn't always desecribe the most recent one
### ### maybe use: git tag --sort=committerdate --contains HEAD~ | tail -1
### ### also maybe git log --format=%H | git name-rev --stdin --name-only --tags
>&2 echo " PREVTAG:" "${PREVTAG:=$(git -C "${REPODIR}" describe --tags --abbrev=0)}"
[[ "$PREVTAG" == "" ]] && PREVTAG=0.0.0-initial.0

## look for a change in the version number in slm.toml
## and if it changed in the most recent commit, use that as the version
### get the most recent commit, incuding merges
### look for changes in slm.toml
### if the version line was changed, extract the new version number
>&2 echo " TOMLVER:" "${TOMLVER:=$(git -C "${REPODIR}" log -1 -p -m \
| filterdiff -p1 -i slm.toml \
| sed -E -n 's/^version = "(.*)"/\1/p'
)}"

if [[ "${TOMLVER}" != "" ]] ; then
# the version in slm.toml was changed, so use that as the version
NEWVER=${TOMLVER}
else
## By default, bump the prerelease version using the branch name
## note the two dots after branch name are intentional
BRANCH_OR_RC="${BRANCH}"
if [[ "${BRANCH}" == "master" ]] ; then
# if the branch is "master", use the prerelease label "rc"
BRANCH_OR_RC="rc"
fi
>&2 echo " BUMP:" "${BUMP:=prerel ${BRANCH_OR_RC}..}"

## if the previous tag is a final version with no prerel suffix
## then bump the patch version before adding a new suffix
if [[ "${PREVTAG}" == "$(semver get release ${PREVTAG})" ]] ; then
PREVTAG=$(semver bump patch ${PREVTAG})
fi

NEWVER=${PREVTAG}

# keep bumping until the tag doesn't exist
while [[ "${NEWVER}" == "${PREVTAG}" ]] || git -C "${REPODIR}" tag | grep -E -q "^${NEWVER}$" ; do
## Bump the previous tag
BUMPEDTAG=$(semver bump ${BUMP} ${NEWVER})
>&2 echo " BUMPEDTAG:" "${BUMPEDTAG}"

# calculate the new version tag by calling calc-slm-version.sh in the same
# directory as this script
NEWVER=$($(dirname $0)/calc-slm-version.sh "${BUMPEDTAG}" "${BRANCH}")
>&2 echo " NEWVER:" ${NEWVER}
done
fi

echo ${NEWVER}
68 changes: 68 additions & 0 deletions ci/calc-slm-version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash
set -Eeuxo pipefail
PS4='>>> '

# Given a SemVer version string and a branch name, modify the version number to match SLM version numbering practices

# Given MAJ.MIN.PAT-TAG.SUF such as 1.2.34-rc.56

# Number of digits for PAT and SUF should be no more than 2 digits
# For branch "master"
# the TAG part of the semver should be "rc"
# For any other master
# the TAG part of the semver should be the branch name
# and the MAJ digit should be zero


SEMVER=${1:?Usage: $0 1.2.34-foo.56 branchname}
BRANCH=${2:?Usage: $0 1.2.34-foo.56 branchname}

## Verify expected input format
grep -E -q '^([0-9]*)\.([0-9]*)\.([0-9][0-9]?)(-((-|_|[a-z]|[A-Z]|[0-9])*)\.([0-9][0-9]?))?$' <<< $SEMVER || \
{ printf "\n\n*** ERROR: input $SEMVER not in format 1.2.34-foo.56\n\n\n" && exit -2 ; }

# MAJ.MIN.PAT-TAG.SUF
# 0.12.0-foo.0 => 0.12.90000
# 0.12.0-foo.3 => 0.12.90003
# 0.12.3-foo.0 => 0.12.90300
# 0.12.34-foo.56 => 0.12.93456
# 0.12.345-foo.678 => fail
MAJ=$(echo $SEMVER | cut -d. -f1)
MIN=$(echo $SEMVER | cut -d. -f2)
PAT=$(echo $SEMVER | cut -d. -f3 | cut -d- -f1)
TAG=$(echo $SEMVER | cut -d. -f3 | cut -d- -f2-)
SUF=$(echo $SEMVER | cut -d. -f4)

[[ $BRANCH == "rc" ]] && \
printf "\n\n*** ERROR: branch name cannot be \"rc\"\n\n\n" && exit -2

SLM_VERSION=$(printf "%d.%d.%d-%s.%d" "$MAJ" "$MIN" "$PAT" "$TAG" "$SUF")

# If the branch is "master"
if [[ "${BRANCH}" == "master" ]] ; then
# and the semver is a final release version 1.2.34
if [[ "${SEMVER}" == "${MAJ}.${MIN}.${PAT}" ]] ; then
# output the version unchanged
SLM_VERSION="${SEMVER}"
# else force the tag to be "rc"
else
TAG="rc"
SLM_VERSION=$(printf "%d.%d.%d-%s.%d" "$MAJ" "$MIN" "$PAT" "$TAG" "$SUF")
fi

# else the branch is not "develop"
# then make the MAJ part zero
# and the TAG part the branch name
# to avoid colliding with real rc or final versions
else
if [[ "${SEMVER}" == "${MAJ}.${MIN}.${PAT}" ]] ; then
MAJ=0
SLM_VERSION=$(printf "%d.%d.%d" "$MAJ" "$MIN" "$PAT")
else
MAJ=0
TAG=${BRANCH}
SLM_VERSION=$(printf "%d.%d.%d-%s.%d" "$MAJ" "$MIN" "$PAT" "$TAG" "$SUF")
fi
fi

echo ${SLM_VERSION}

0 comments on commit d5bc92a

Please sign in to comment.