Skip to content

Commit 3c5da12

Browse files
Update workflows from templates. (#72)
1 parent 0e6ca9c commit 3c5da12

File tree

5 files changed

+331
-21
lines changed

5 files changed

+331
-21
lines changed

.github/bump-version.bump.sh

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

.github/bump-version.get.sh

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

.github/bump-version.set.sh

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#!/bin/bash
2+
3+
# This script is copied from the depot repo; edit it there, not in the destination repo.
4+
5+
# Set the semver in various files.
6+
7+
# Always performs sanity checking:
8+
# - There must be at least one version file.
9+
# - The version must be a valid semver.
10+
# - The version must not be 0.0.0.
11+
# - After running, altered files must already be under Git control, and they must be only the version files we know how to handle,
12+
# and modifications must be 0 or 1 line changes.
13+
14+
# If setting the version to $a.$b.$c-$pre, substitute "SNAPSHOT" for $pre in any Java-related files.
15+
16+
set -e
17+
18+
# Parse args
19+
if [ $# -ne 1 ] ; then
20+
echo "Usage: $0 version" 1>&2
21+
exit 1
22+
fi
23+
NEWVERS="$1"
24+
25+
# Sanity check: Ignoring any pre-release info, version must not be 0.0.0.
26+
if [ "${NEWVERS/-*/}" = "0.0.0" ] ; then
27+
echo "Illegal zero version '${NEWVERS}'" 1>&2
28+
exit 1
29+
fi
30+
# Sanity check: Must be valid semver.
31+
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
32+
echo "Invalid version '${NEWVERS}'" 1>&2
33+
exit 1
34+
fi
35+
36+
# Find the version files in this directory or its descendants, but don't recurse too deep.
37+
# This line must be kept in sync with "bump-version.get.sh".
38+
VERSFILES=$(find . -maxdepth 3 ! -path ./.git/\* | grep -v /node_modules/ | grep -E '.*/(version|Cargo.toml|package.json|pom.xml|version.sbt)$')
39+
40+
# Edit the version files.
41+
for FILE in ${VERSFILES} ; do
42+
DIR=$(dirname "${FILE}")
43+
case $(basename "${FILE}") in
44+
version)
45+
echo "${NEWVERS}" > "${FILE}"
46+
;;
47+
48+
Cargo.toml)
49+
sed 's/^version = ".*"$/version = "'"${NEWVERS}"'"/' "${FILE}" > "${FILE}.tmp"
50+
mv "${FILE}.tmp" "${FILE}"
51+
52+
# If there's a Cargo.lock, update it also.
53+
if [ -f "${DIR}/Cargo.lock" ] ; then
54+
CARGO_LOCKS="${CARGO_LOCKS} ${DIR}"
55+
fi
56+
;;
57+
58+
package.json)
59+
if [ "$(dirname "${FILE}")" = "." ] ; then
60+
# This is the root package.json, so we want .version.
61+
jq --indent 4 ".version=\"${NEWVERS}\"" "${FILE}" > "${FILE}.new"
62+
else
63+
# We already know the root package name from above, so reuse that here.
64+
jq --indent 4 ".dependencies[\"${ROOTJSNAME}\"]=\"^${NEWVERS}\"" "${FILE}" > "${FILE}.new"
65+
fi
66+
mv "${FILE}.new" "${FILE}"
67+
;;
68+
69+
pom.xml)
70+
# Replace -foo with -SNAPSHOT to be compatible with Java conventions.
71+
JAVAVERS="${NEWVERS/-*/-SNAPSHOT}"
72+
73+
if [ "$(dirname "${FILE}")" = "." ] ; then
74+
# This is the root pom.xml, so we want /m:project/m:version.
75+
xmlstarlet ed -L -P -N m="http://maven.apache.org/POM/4.0.0" -u "/m:project/m:version" -v "${JAVAVERS}" "${FILE}"
76+
else
77+
# We've already computed our XPATH expression above, so reuse that here.
78+
xmlstarlet ed -L -P -N m="http://maven.apache.org/POM/4.0.0" -u "${XPATH}" -v "${JAVAVERS}" "${FILE}"
79+
fi
80+
;;
81+
82+
version.sbt)
83+
# Replace -foo with -SNAPSHOT to be compatible with Java conventions.
84+
# Disabling this logic to work with cmk-s3-proxy. Since we only use bump-version to publish our scala containers, not our
85+
# scala libs, the -SNAPSHOT suffix isn't an important convention.
86+
# JAVAVERS="${NEWVERS/-*/-SNAPSHOT}"
87+
JAVAVERS="${NEWVERS}"
88+
89+
# The file might use the old, deprecated syntax or the newer syntax:
90+
# version in ThisBuild := "1.2.3-SNAPSHOT"
91+
# ThisBuild / version := "1.2.3-SNAPSHOT"
92+
sed 's,^ThisBuild / version := ".*"$,ThisBuild / version := "'"${JAVAVERS}"'",' "${FILE}" > "${FILE}.tmp"
93+
sed 's,^version in ThisBuild := ".*"$,ThisBuild / version := "'"${JAVAVERS}"'",' "${FILE}.tmp" > "${FILE}"
94+
rm "${FILE}.tmp"
95+
;;
96+
97+
*)
98+
echo "Can't edit '${FILE}' with new version" 1>&2
99+
exit 1
100+
esac
101+
102+
# Add it to git.
103+
git add "${FILE}"
104+
# Verify that we've changed zero or one line.
105+
git diff --cached -w --numstat "${FILE}" > /tmp/diffcount
106+
if [ -s /tmp/diffcount ] ; then
107+
# shellcheck disable=SC2034
108+
read -r ADDED REMOVED FILENAME < /tmp/diffcount
109+
if [ "${ADDED}" -ne 1 ] || [ "${REMOVED}" -ne 1 ] ; then
110+
echo "Changes to '${FILE}' must be zero or one line, but observed edits are:" 1>&2
111+
git diff --cached "${FILE}" 1>&2
112+
exit 1
113+
fi
114+
fi
115+
done
116+
117+
# If there are Cargo.lock files, we need to run "cargo fetch" after all the Cargo.toml files have been edited.
118+
for DIR in ${CARGO_LOCKS} ; do
119+
( cd "${DIR}" && cargo fetch )
120+
git add "${DIR}/Cargo.lock"
121+
done
122+
123+
# Look for files that have been changed, but that we haven't told git about.
124+
echo "Checking for modified but untracked files:"
125+
if git status -s | grep -qEv '^M ' ; then
126+
echo "Modified but untracked files:" 1>&2
127+
git status -s | grep -Ev '^M ' 1>&2
128+
echo "This probably means '$0' modified a file but forgot to 'git add' it." 1>&2
129+
exit 1
130+
fi

.github/update-workflows.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#!/bin/bash
22

3+
# This script is copied from the depot repo; edit it there, not in the destination repo.
4+
35
# Copy the requested templates from the templates repo to this one, applying patches as we go.
46
# You must be in the target repo when you run this, and depot must be checked out as a sibling to this repo.
57

@@ -40,6 +42,9 @@ for WF in ${WORKFLOWS} ; do
4042
YAMLPATCH=".github/${BASE}-patch.yaml"
4143
JSONPATCH="/tmp/${BASE}-patch.json"
4244

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

.github/workflows/bump-version.yaml

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,13 @@ name: Bump Version
77
push:
88
branches:
99
- main
10-
workflow_dispatch: null
10+
workflow_dispatch:
11+
inputs:
12+
version:
13+
description: New semver release version.
1114
env:
1215
COMMIT_PREFIX: 'bump-version: '
16+
MODE: release
1317
jobs:
1418
vars:
1519
runs-on: ubuntu-20.04
@@ -34,25 +38,15 @@ jobs:
3438
git config --global user.name "Leeroy Travis"
3539
3640
'
41+
- name: Get version
42+
id: current
43+
run: echo "::set-output name=version::$(.github/bump-version.get.sh)"
3744
- name: Compute versions
3845
id: versions
39-
run: 'set -x
40-
41-
VERSION=$(.github/bump-version.sh)
42-
43-
echo "::set-output name=old::${VERSION}"
44-
45-
VERSION="${VERSION/-*}"
46-
47-
echo "::set-output name=release::${VERSION}"
48-
49-
VERSION="$(echo "${VERSION}" | awk -F. -v OFS=. ''{$NF += 1 ; print}'')-pre"
50-
51-
echo "::set-output name=bumped::${VERSION}"
52-
53-
'
46+
run: .github/bump-version.bump.sh "${{ env.MODE }}" "${{ steps.current.outputs.version
47+
}}" ${{ github.event.inputs.version }}
5448
- name: Set release version
55-
run: '.github/bump-version.sh "${{ steps.versions.outputs.release }}"
49+
run: '.github/bump-version.set.sh "${{ steps.versions.outputs.release }}"
5650
5751
git diff --cached
5852
@@ -78,14 +72,14 @@ jobs:
7872
token: ${{ secrets.WORKFLOW_PAT }}
7973
prerelease: true
8074
tag: ${{ steps.versions.outputs.release }}
81-
- name: Bump version and set pre-release
82-
run: '.github/bump-version.sh "${{ steps.versions.outputs.bumped }}"
75+
- name: Set development version
76+
run: '.github/bump-version.set.sh "${{ steps.versions.outputs.bumped }}"
8377
8478
git diff --cached
8579
8680
'
87-
- name: Commit and push pre-release version
88-
run: 'git commit -m "${COMMIT_PREFIX}Bump to next pre-release version ${{ steps.versions.outputs.bumped
81+
- name: Commit and push development version
82+
run: 'git commit -m "${COMMIT_PREFIX}Bump to next development version ${{ steps.versions.outputs.bumped
8983
}}"
9084
9185
git push origin main

0 commit comments

Comments
 (0)