Skip to content

Commit 5977b84

Browse files
authored
Use the bump-version workflow's new prerelease mode. (#82)
1 parent f032785 commit 5977b84

File tree

6 files changed

+208
-172
lines changed

6 files changed

+208
-172
lines changed

.github/bump-version-patch.yaml

Lines changed: 3 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,4 @@
1-
# If we're running on merge to main, then we're just incrementing the "-pre.N" and starting the docker workflow.
2-
# If we're running from workflow_dispatch, then we're making a new release.
3-
# The next several entries in the patch cover the two cases just described.
1+
# Normal releases are internal prereleases.
42
- op: replace
5-
path: /on/workflow_dispatch
6-
value:
7-
inputs:
8-
version:
9-
description: New semver release version.
10-
required: true
11-
- op: replace
12-
path: /jobs/bump/steps/2
13-
value:
14-
name: Compute version
15-
id: versions
16-
run: |
17-
set -x
18-
# Read the current prerelease version.
19-
VERSION=$(.github/bump-version.sh)
20-
echo "::set-output name=old::${VERSION}"
21-
BUILDNUM=$(echo "${VERSION}" | sed 's/[^0-9][^0-9]*/ /g' | awk '{print $NF}')
22-
if [ "${BUILDNUM}" = "" ] ; then
23-
BUILDNUM=0
24-
fi
25-
26-
# Set the release version. If this is a workflow_dispatch, use the specified version. Otherwise increment the "-pre.N" part.
27-
if [ -n "${{ github.event.inputs.version }}" ] ; then
28-
VERSION="${{ github.event.inputs.version }}"
29-
else
30-
BUILDNUM=$(expr "${BUILDNUM}" + 1)
31-
VERSION="$(echo "${VERSION}" | sed 's/-.*//')"
32-
VERSION="${VERSION}-pre.${BUILDNUM}"
33-
fi
34-
echo "::set-output name=release::${VERSION}"
35-
36-
# Is it a release version? If so, it won't have a '-' character in it. "1.2.3-pre.4" is not a release version.
37-
if [[ ${VERSION} =~ .*-.* ]] ; then
38-
echo "::set-output name=is_release::false"
39-
else
40-
echo "::set-output name=is_release::true"
41-
VERSION="$(echo "${VERSION}" | awk -F. -v OFS=. '{$NF += 1 ; print}')-pre.1"
42-
echo "::set-output name=bumped::${VERSION}"
43-
fi
44-
- op: add
45-
path: /jobs/bump/steps/8/if
46-
value: ${{ steps.versions.outputs.bumped != '' }}
47-
- op: add
48-
path: /jobs/bump/steps/9/if
49-
value: ${{ steps.versions.outputs.bumped != '' }}
3+
path: /env/MODE
4+
value: prerelease

.github/bump-version.bump.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
3+
# Decide what the next versions are going to be. Inputs are the mode, current version, and optional release version override.
4+
# Outputs are the new release version and the new development version.
5+
6+
# Mode can be "release" or "prerelease". See bump-version.yaml for details.
7+
8+
set -e
9+
10+
case "$#" in
11+
"2")
12+
MODE="$1"
13+
CURRENTVERS="$2"
14+
;;
15+
"3")
16+
MODE="$1"
17+
CURRENTVERS="$2"
18+
RELEASEVERS="$3"
19+
;;
20+
*)
21+
echo "Usage: $0 mode current_vers [new_vers]" 1>&2
22+
exit 1
23+
esac
24+
if [ "${MODE}" != "release" ] && [ "${MODE}" != "prerelease" ] ; then
25+
echo "Invalid mode '${MODE}'" 1>&2
26+
exit 1
27+
fi
28+
29+
for V in ${CURRENTVERS} ${RELEASEVERS} ; do
30+
# Sanity check: Ignoring any pre-release info, version must not be 0.0.0.
31+
if [ "${V/-*/}" = "0.0.0" ] ; then
32+
echo "Illegal zero version '${V}'" 1>&2
33+
exit 1
34+
fi
35+
# Sanity check: Must be valid semver.
36+
if ! [[ ${V} =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$ ]] ; then
37+
echo "Invalid version '${V}'" 1>&2
38+
exit 1
39+
fi
40+
done
41+
42+
# Derive a new release version.
43+
if [ -z "${RELEASEVERS}" ] ; then
44+
case "${MODE}" in
45+
"release")
46+
RELEASEVERS="${CURRENTVERS/-*}"
47+
;;
48+
"prerelease")
49+
# Split "1.2.3-pre.4" into "1.2.3" and "pre.4".
50+
read -r PRE POST < <(echo "${CURRENTVERS/-/ }")
51+
# Remove non-numeric parts of "pre.4" like "pre".
52+
POST="$(echo "${POST}" | sed -e 's/[^0-9.]//g' -e 's/\.*$//')"
53+
# Remove leading, trailing, or repeated "." characters.
54+
POST="$(echo "${POST}" | sed -e 's/^\.*//' -e 's/\.*$//' -e 's/\.\.*/./g')"
55+
if [ "${POST}" = "" ] ; then
56+
POST="0"
57+
fi
58+
# Set "1.2.3-rc.4".
59+
RELEASEVERS="${PRE}-rc.${POST}"
60+
;;
61+
esac
62+
fi
63+
echo "::set-output name=release::${RELEASEVERS}"
64+
65+
# Derive a new bumped version from the release version.
66+
# Change "1.2.3-rc.4" to "1.2.3.4".
67+
VERSION="$(echo "${RELEASEVERS}" | sed -e 's/-/./g' -e 's/rc//' -e 's/\.\.*/./g')"
68+
# Increment "1.2.3.4" to "1.2.3.5".
69+
VERSION="$(echo "${VERSION}" | awk -F. -v OFS=. '{$NF += 1 ; print}')"
70+
# Convert back to valid semver syntax "1.2.3-pre.5".
71+
VERSION="$(echo "${VERSION}" | sed -e 's/\([^.]*\)\.\([^.]*\)\.\([^.]*\)\(\.\(.*\)\)\{0,1\}/\1.\2.\3-pre.\5/' -e 's/\.$//')"
72+
echo "::set-output name=bumped::${VERSION}"

.github/bump-version.get.sh

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
#!/bin/bash
2+
3+
# Get the semver from various files in the repo.
4+
5+
# Always performs sanity checking:
6+
# - There must be at least one version file.
7+
# - All version files must agree. (Ignoring the contents but not existence of pre-release version.)
8+
# - The version must be a valid semver.
9+
# - The version must not be 0.0.0.
10+
11+
set -e
12+
13+
# Parse args
14+
if [ $# -gt 0 ] ; then
15+
echo "Usage: $0" 1>&2
16+
exit 1
17+
fi
18+
19+
# Find the version files in this directory or its descendants, but don't recurse too deep.
20+
# This line must be kept in sync with "bump-version.set.sh".
21+
VERSFILES=$(find . -maxdepth 3 ! -path ./.git/\* | grep -v /node_modules/ | grep -E '.*/(version|Cargo.toml|package.json|pom.xml|version.sbt)$')
22+
23+
# Do we have at least one?
24+
if [ -z "${VERSFILES}" ] ; then
25+
echo "No version files found; aborting" 1>&2
26+
exit 1
27+
fi
28+
29+
# Read the versions.
30+
CURRENTVERS=""
31+
for FILE in ${VERSFILES} ; do
32+
# Parse each version file according to its type.
33+
case $(basename "${FILE}") in
34+
version)
35+
# It's a file to capture version info for generic things that don't have their own format.
36+
VERS=$(cat "${FILE}")
37+
;;
38+
Cargo.toml)
39+
VERS=$(cargo metadata --manifest-path "${FILE}" --no-deps --offline --format-version 1 | jq -r '.packages[0].version')
40+
;;
41+
package.json)
42+
if [ "$(dirname "${FILE}")" = "." ] ; then
43+
# This is the root package.json, so we want .version.
44+
VERS=$(jq -r '.version' < "${FILE}")
45+
else
46+
# This isn't the root package.json, so we assume it depends on the package declared in the root package.json. We need to
47+
# get the root package's name.
48+
ROOTJSNAME=$(jq -r '.name' < package.json)
49+
VERS=$(jq -r ".dependencies[\"${ROOTJSNAME}\"]" < "${FILE}")
50+
# Strip off any leading "^".
51+
VERS=${VERS/^/}
52+
fi
53+
;;
54+
pom.xml)
55+
if [ "$(dirname "${FILE}")" = "." ] ; then
56+
# This is the root pom.xml, so we want /m:project/m:version.
57+
VERS=$(xmlstarlet sel -N m="http://maven.apache.org/POM/4.0.0" -t -v "/m:project/m:version" < "${FILE}")
58+
else
59+
# This isn't the root pom.xml, so we assume it depends on the package declared in the root pom.xml. We need to get the
60+
# root pom's artifactId.
61+
ROOTID=$(xmlstarlet sel -N m="http://maven.apache.org/POM/4.0.0" -t -v "/m:project/m:artifactId" < pom.xml)
62+
# Select /m:project/m:dependencies/m:dependency/m:version where it has a sibling m:artifactId with the correct value.
63+
XPATH="/m:project/m:dependencies/m:dependency[m:artifactId=\"${ROOTID}\"]/m:version"
64+
VERS=$(xmlstarlet sel -N m="http://maven.apache.org/POM/4.0.0" -t -v "${XPATH}" < "${FILE}")
65+
fi
66+
;;
67+
version.sbt)
68+
VERS=$(sed -e 's/^[^"]*"//' -e 's/"$//' < "${FILE}")
69+
;;
70+
*)
71+
echo "Can't parse '${FILE}' for version" 1>&2
72+
exit 1
73+
;;
74+
esac
75+
76+
if [ -z "${VERS}" ] ; then
77+
echo "Empty version from '${FILE}'" 1>&2
78+
exit 1
79+
fi
80+
81+
# If this is the first parsed version file, then set current version.
82+
if [ -z "${CURRENTVERS}" ] ; then
83+
CURRENTVERS="${VERS}"
84+
fi
85+
86+
# Compare this file's version to other files' version. Ignore anything after the "-" in a pre-release version, but keep the "-"
87+
# so a release version is unequal to a pre-release.
88+
if ! [ "${CURRENTVERS/-*/-}" = "${VERS/-*/-}" ] ; then
89+
echo "Version '${VERS}' in '${FILE}' doesn't match '${CURRENTVERS}' from others in '${VERSFILES}'" 1>&2
90+
exit 1
91+
fi
92+
done
93+
94+
# Sanity check: Ignoring any pre-release info, version must not be 0.0.0.
95+
if [ "${CURRENTVERS/-*/}" = "0.0.0" ] ; then
96+
echo "Illegal zero version '${CURRENTVERS}'" 1>&2
97+
exit 1
98+
fi
99+
# Sanity check: Must be valid semver.
100+
if ! [[ ${CURRENTVERS} =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$ ]] ; then
101+
echo "Invalid version '${CURRENTVERS}'" 1>&2
102+
exit 1
103+
fi
104+
105+
echo "${CURRENTVERS}"

.github/bump-version.sh renamed to .github/bump-version.set.sh

Lines changed: 13 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -1,126 +1,39 @@
11
#!/bin/bash
22

3-
# Get or set the semver in various files.
4-
5-
# If called with no args, return the current version.
6-
# If called with one arg, set the version to the new value.
3+
# Set the semver in various files.
74

85
# Always performs sanity checking:
96
# - There must be at least one version file.
10-
# - All version files must agree. (Ignoring the contents but not existence of pre-release version.)
117
# - The version must be a valid semver.
128
# - The version must not be 0.0.0.
9+
# - After running, altered files must already be under Git control, and they must be only the version files we know how to handle,
10+
# and modifications must be 0 or 1 line changes.
1311

1412
# If setting the version to $a.$b.$c-$pre, substitute "SNAPSHOT" for $pre in any Java-related files.
1513

1614
set -e
1715

1816
# Parse args
19-
case "$#" in
20-
"0")
21-
NEWVERS=""
22-
;;
23-
"1")
24-
NEWVERS="$1"
25-
;;
26-
"*")
27-
echo "Usage: $0 [version]" 1>&2
28-
exit 1
29-
;;
30-
esac
31-
32-
# Find the version files in this directory or its descendants, but don't recurse too deep.
33-
VERSFILES=$(find . -maxdepth 3 ! -path ./.git/\* | grep -v /node_modules/ | grep -E '.*/(version|Cargo.toml|package.json|pom.xml|version.sbt)$')
34-
35-
# Do we have at least one?
36-
if [ -z "${VERSFILES}" ] ; then
37-
echo "No version files found; aborting" 1>&2
17+
if [ $# -ne 1 ] ; then
18+
echo "Usage: $0 version" 1>&2
3819
exit 1
3920
fi
40-
41-
# Read the versions.
42-
CURRENTVERS=""
43-
for FILE in ${VERSFILES} ; do
44-
# Parse each version file according to its type.
45-
case $(basename "${FILE}") in
46-
version)
47-
# It's a file to capture version info for generic things that don't have their own format.
48-
VERS=$(cat "${FILE}")
49-
;;
50-
Cargo.toml)
51-
VERS=$(cargo metadata --manifest-path "${FILE}" --no-deps --offline --format-version 1 | jq -r '.packages[0].version')
52-
;;
53-
package.json)
54-
if [ "$(dirname "${FILE}")" = "." ] ; then
55-
# This is the root package.json, so we want .version.
56-
VERS=$(jq -r '.version' < "${FILE}")
57-
else
58-
# This isn't the root package.json, so we assume it depends on the package declared in the root package.json. We need to
59-
# get the root package's name.
60-
ROOTJSNAME=$(jq -r '.name' < package.json)
61-
VERS=$(jq -r ".dependencies[\"${ROOTJSNAME}\"]" < "${FILE}")
62-
# Strip off any leading "^".
63-
VERS=${VERS/^/}
64-
fi
65-
;;
66-
./pom.xml)
67-
if [ "$(dirname "${FILE}")" = "." ] ; then
68-
# This is the root pom.xml, so we want /m:project/m:version.
69-
VERS=$(xmlstarlet sel -N m="http://maven.apache.org/POM/4.0.0" -t -v "/m:project/m:version" < "${FILE}")
70-
else
71-
# This isn't the root pom.xml, so we assume it depends on the package declared in the root pom.xml. We need to get the
72-
# root pom's artifactId.
73-
ROOTID=$(xmlstarlet sel -N m="http://maven.apache.org/POM/4.0.0" -t -v "/m:project/m:artifactId" < pom.xml)
74-
# Select /m:project/m:dependencies/m:dependency/m:version where it has a sibling m:artifactId with the correct value.
75-
XPATH="/m:project/m:dependencies/m:dependency[m:artifactId=\"${ROOTID}\"]/m:version"
76-
VERS=$(xmlstarlet sel -N m="http://maven.apache.org/POM/4.0.0" -t -v "${XPATH}" < "${FILE}")
77-
fi
78-
;;
79-
version.sbt)
80-
VERS=$(sed -e 's/^[^"]*"//' -e 's/"$//' < "${FILE}")
81-
;;
82-
*)
83-
echo "Can't parse '${FILE}' for version" 1>&2
84-
exit 1
85-
;;
86-
esac
87-
88-
if [ -z "${VERS}" ] ; then
89-
echo "Empty version from '${FILE}'" 1>&2
90-
exit 1
91-
fi
92-
93-
# If this is the first parsed version file, then set current version.
94-
if [ -z "${CURRENTVERS}" ] ; then
95-
CURRENTVERS="${VERS}"
96-
fi
97-
98-
# Compare this file's version to other files' version. Ignore anything after the "-" in a pre-release version, but keep the "-"
99-
# so a release version is unequal to a pre-release.
100-
if ! [ "${CURRENTVERS/-*/-}" = "${VERS/-*/-}" ] ; then
101-
echo "Version '${VERS}' in '${FILE}' doesn't match '${CURRENTVERS}' from others in '${VERSFILES}'" 1>&2
102-
exit 1
103-
fi
104-
done
21+
NEWVERS="$1"
10522

10623
# Sanity check: Ignoring any pre-release info, version must not be 0.0.0.
107-
if [ "${CURRENTVERS/-*/}" = "0.0.0" ] ; then
108-
echo "Illegal zero version '${CURRENTVERS}'" 1>&2
24+
if [ "${NEWVERS/-*/}" = "0.0.0" ] ; then
25+
echo "Illegal zero version '${NEWVERS}'" 1>&2
10926
exit 1
11027
fi
11128
# Sanity check: Must be valid semver.
112-
if ! [[ ${CURRENTVERS} =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$ ]] ; then
113-
echo "Invalid version '${CURRENTVERS}'" 1>&2
29+
if ! [[ ${NEWVERS} =~ ^(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)\.(0|[1-9][0-9]*)(-((0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*)(\.(0|[1-9][0-9]*|[0-9]*[a-zA-Z-][0-9a-zA-Z-]*))*))?(\+([0-9a-zA-Z-]+(\.[0-9a-zA-Z-]+)*))?$ ]] ; then
30+
echo "Invalid version '${NEWVERS}'" 1>&2
11431
exit 1
11532
fi
11633

117-
# If we're just getting the current version, print it and exit successfully.
118-
if [ -z "${NEWVERS}" ] ; then
119-
echo "${CURRENTVERS}"
120-
exit 0
121-
fi
122-
123-
# If we reach this point, it means we're setting the version.
34+
# Find the version files in this directory or its descendants, but don't recurse too deep.
35+
# This line must be kept in sync with "bump-version.get.sh".
36+
VERSFILES=$(find . -maxdepth 3 ! -path ./.git/\* | grep -v /node_modules/ | grep -E '.*/(version|Cargo.toml|package.json|pom.xml|version.sbt)$')
12437

12538
# Edit the version files.
12639
for FILE in ${VERSFILES} ; do

.github/update-workflows.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ for WF in ${WORKFLOWS} ; do
4040
YAMLPATCH=".github/${BASE}-patch.yaml"
4141
JSONPATCH="/tmp/${BASE}-patch.json"
4242

43+
# Remove the target files before creating them from scratch. This lets us handle file renames and deletes.
44+
rm -f "${THISREPO}"/.github/"${BASE}".*
45+
4346
# Copy the workflow file, using jsonpatch.
4447
(
4548
echo "# DO NOT EDIT THIS FILE."

0 commit comments

Comments
 (0)