Skip to content

Commit 825f97c

Browse files
committed
THX-1138: mono-repo
1 parent 257e191 commit 825f97c

File tree

6 files changed

+198
-39
lines changed

6 files changed

+198
-39
lines changed

.github/workflows/build-and-release.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
echo "FUNCTION=$FUNCTION" >> $GITHUB_ENV
2828
- name: Run GitOps Automatic Versioning Action
2929
id: gitops-autover
30-
uses: AlexAtkinson/github-action-gitops-autover@0.1.7
30+
uses: AlexAtkinson/github-action-gitops-autover@0.3.1
3131
- name: Set Version Dependent Envars
3232
run: |
3333
VERSION=${{ steps.gitops-autover.outputs.new-version }}
@@ -47,4 +47,4 @@ jobs:
4747
automatic_release_tag: "${{ env.VERSION }}"
4848
title: "${{ env.VERSION }}"
4949
files: |
50-
${{ env.ZIPFILE }}
50+
${{ env.ZIPFILE }}

README.md

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,27 @@
88
<!-- https://github.com/marketplace/actions/count-action-users -->
99
<!-- ![HitCount](https://hits.dwyl.com/AlexAtkinson/github-action-gitops-autover.svg?style=flat-square) -->
1010

11-
Language/content agnostic method of automatically determining the [semantic version](https://semver.org/) for a product based on _branch_ merge history with MINIMAL discipline dependencies.
11+
Language/content agnostic method of automatically determining the [semantic version](https://semver.org/) for a product based on _branch_ merge history with MINIMAL [discipline dependencies](https://github.com/marketplace/actions/gitops-automatic-versioning?#discipline-dependency).
1212

1313
This is accomplished by counting the merges of branches matching the [naming scheme](#branch-naming-scheme) into the [main|master] branch. Folks familiar with Scrum/SAFe or GitFlow/fooFlow strategies will recognize this scheme.
1414

15-
**Yes**, this can be implemented in repos that previously used different version increment methods.
15+
**Burning Questions**
16+
17+
- **Yes** - This can indeed be implemented in repos that previously used different version increment methods.
18+
- **Yes** - Jira will recognize the issue tag anywhere in the branch name -- it does not have to be a prefix for the integration to function.
19+
- **Yes** - This aligns with and extends on [guidance](https://confluence.atlassian.com/bitbucketserver050/using-branches-in-bitbucket-server-913474726.html) from Atlassian on branch naming schemes.
1620

1721
> [Convenience link to this action on the marketplace](https://github.com/marketplace/actions/gitops-automatic-versioning)
1822
23+
## Recent Changes
24+
25+
- 1.0.0: (non-breaking) Addition of support for mono-repos. IE: Discretely version specific directories.
26+
- NOTE: Github, Jira, etc., were designed to host one product per repo/project. DO NOT create new mono-repo projects unless you're specifically tooling out to support them well.
27+
- 0.3.1: Update the checkout action version to v4.
28+
- 0.3.0: Bring back the unshallowing, which ensures the full git log is available.
29+
- TODO: Adjust scripts to use `git log --remotes` to avoid unshallowing large repos.
30+
- 0.2.9: Fix 'ops' increments; add user friendly error outputs.
31+
1932
## Usage
2033

2134
### Setup
@@ -45,6 +58,33 @@ git push --set-upstream origin fix/not-a-feature
4558
# THEN: Click the link to create a PR & merge it</pre></dd>
4659
</dl>
4760

61+
### Inputs
62+
63+
Note: Only required for setting up mono-repo versioning.
64+
65+
<dl>
66+
<dt>mono-repo-mode: [bool]</dt>
67+
<dd>Enable semver matching against any tag structure that includes a valid semver string.<br>
68+
Eg: '&lt;product-name&gt;_&lt;semver&gt;'<br>
69+
<i>Required:</i> false<br>
70+
<i>Default:</i> false</dd>
71+
<dt>mono-repo-product-name: [string]</dt>
72+
<dd>The product name to match against.<br>
73+
Eg: 'bob', would match the tags like 'bob_1.2.3'.<br>
74+
<i>Required:</i> if mono-repo-mode: true<br>
75+
<i>Default:</i> ''</dd>
76+
<dt>mono-repo-product-path: [string]</dt>
77+
<dd>The path to the product.<br>
78+
Eg: path/to/bob<br>
79+
<i>Required:</i> if mono-repo-mode: true<br>
80+
<i>Default:</i> ''</dd>
81+
<dt>force-patch-increment: [bool]</dt>
82+
<dd>Forces a PATCH increment if no other increment detected.<br>
83+
(Intended for development purposes only.)<br>
84+
<i>Required:</i> false<br>
85+
<i>Default:</i> false</dd>
86+
</dl>
87+
4888
### Outputs
4989

5090
<dl>
@@ -73,14 +113,23 @@ Below is a valid workflow utilizing this action. If you wanted to extend it to d
73113
- uses: actions/checkout@v3
74114
- name: Run GitOps Automatic Versioning Action
75115
id: gitops-autover
76-
uses: AlexAtkinson/github-action-gitops-autover@0.1.7
116+
uses: AlexAtkinson/github-action-gitops-autover@0.3.0
77117
- name: Verify Outputs
78118
run: |
79119
NEW_VERSION=${{ steps.gitops-autover.outputs.new-version }}
80120
echo "new-version: $NEW_VERSION"
81121
PREVIOUS_VERSION=${{ steps.gitops-autover.outputs.previous-version }}
82122
echo "previous-version: $PREVIOUS_VERSION"
83123

124+
To make use of the mono-repo support, simply add a block for the director you wish to version.
125+
126+
- name: Run GitOps Automatic Versioning Action
127+
id: gitops-autover
128+
uses: AlexAtkinson/[email protected]
129+
with:
130+
mono-repo-mode: true
131+
mono-repo-product-name: bob
132+
84133
This results in outputs like:
85134

86135
_major:_
@@ -140,7 +189,7 @@ Additionally, this repo uses its own action for versioning, so feel free to inve
140189
echo "PRODUCT_NAME_LOWER=$PRODUCT_NAME_LOWER" >> $GITHUB_OUTPUT
141190
- name: GitOps Automatic Versioning
142191
id: gitops-autover
143-
uses: AlexAtkinson/github-action-gitops-autover@0.1.7
192+
uses: AlexAtkinson/github-action-gitops-autover@0.3.0
144193
build:
145194
name: "Build"
146195
runs-on: ubuntu-latest
@@ -254,5 +303,5 @@ For those interested, here's some pseudo code:
254303
PRs are welcome.
255304

256305
- input(s): iteration-branches (map) - inform MINOR and PATCH incrementing branch name patterns.
257-
- input(s): mono-mode (bool) - version subdirs discretely
258-
- ~~CAN'T DO~~: DONE: unshallow from last version tag to latest commit to... Seems a limitation of (git at first glance). See the [Checkout From Tag](https://github.com/marketplace/actions/checkout-from-tag) action.
306+
- DONE: input(s): mono-mode (bool) - version subdirs discretely
307+
- UNDONE-CAN'T DO: ~~CAN'T DO~~: ~~DONE:~~ unshallow from last version tag to latest commit to... Seems a limitation of (git at first glance). See the [Checkout From Tag](https://github.com/marketplace/actions/checkout-from-tag) action.

action.yml

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@ inputs:
1313
description: "Forces a PATCH increment if no other increment detected. NOTE: This is intended for development purposes only."
1414
required: false
1515
default: 'false'
16+
mono-repo-mode:
17+
description: "Enable semver matching against any tag structure that includes a valid semver string. Eg: <product-name>_<semver>"
18+
required: false
19+
default: 'false'
20+
mono-repo-product-name:
21+
description: "The product name to match against. For example, 'bob', would match the tags like 'bob_1.2.3'. Required if 'mono-repo-mode' is enabled."
22+
required: false
23+
default: ''
24+
mono-repo-product-path:
25+
description: "The path to the product. IE: 'path/to/bob'. Required if 'mono-repo-mode' is enabled."
26+
required: false
27+
default: ''
1628
outputs:
1729
new-version:
1830
description: "New Version"
@@ -23,8 +35,13 @@ outputs:
2335
runs:
2436
using: "composite"
2537
steps:
26-
- name: Checkout to the depth of the last DETECTED version.
27-
uses: AlexAtkinson/github-action-checkout-from-tag@latest
38+
- name: Checkout
39+
uses: actions/checkout@v4
40+
- name: Ensure full commit history
41+
run: |
42+
cd $GITHUB_WORKSPACE
43+
git pull --unshallow
44+
shell: bash
2845
- name: Detect Previous Version
2946
id: previous-version
3047
run: |
@@ -36,27 +53,37 @@ runs:
3653
run: |
3754
cd $GITHUB_WORKSPACE
3855
opt=''
39-
[[ "${{ inputs.force-re-evaluate }}" == 'true' ]] && opt='-f'
40-
[[ "${{ inputs.force-re-evaluate }}" == 'true' ]] && opt='-p'
41-
new_version="$(${{ github.action_path }}/scripts/detectNewVersion.sh $opt)"
56+
[[ "${{ inputs.force-re-evaluate }}" == 'true' ]] && opt='$opt -f'
57+
[[ "${{ inputs.force-patch-increment }}" == 'true' ]] && opt='$opt -p'
58+
[[ "${{ inputs.mono-repo-mode }}" == 'true' ]] && opt='$opt -m'
59+
if [[ "${{ inputs.mono-repo-mode }}" == 'true' && -z ${{ inputs.mono-repo-product-name }} ]]; then
60+
echo -e "ERROR: 571 - mono-repo-product-name must be set and NOT null!"
61+
exit 1 || true
62+
fi
63+
[[ -n "${{ inputs.mono-repo-product-name }}" ]] && opt='$opt -n ${{ inputs.mono-repo-product-name }}'
64+
new_version="$(${{ github.action_path }}/scripts/detectNewVersion.sh $opt)" || true
4265
echo "new-version=$new_version" | tee $GITHUB_OUTPUT
66+
if [[ "$new_version" =~ "520" ]]; then
67+
echo -e "ERROR: 520 - You must source this script when specifying an environment variable! Eg: '. ./foo.sh -e bar_ver'"
68+
exit 1 || true
69+
fi
4370
if [[ "$new_version" =~ "570" ]]; then
44-
echo -e "\e[01;31mERROR\e[00m: 570 - Invalid argument!"
71+
echo -e "ERROR: 570 - Invalid argument!"
4572
exit 1 || true
4673
fi
4774
if [[ "$new_version" =~ "590" ]]; then
48-
echo -e "\e[01;31mFATAL\e[00m: 501 - This is not a git repository!"
75+
echo -e "FATAL: 501 - This is not a git repository!"
4976
exit 1 || true
5077
fi
5178
if [[ "$new_version" =~ "591" ]]; then
52-
echo -e "\e[01;31mERROR\e[0m: 591 - Unsupported origin host."
79+
echo -e "ERROR: 591 - Unsupported origin host."
5380
exit 1 || true
5481
fi
5582
if [[ "$new_version" =~ "599" ]]; then
56-
echo -e "\e[01;31mERROR\e[00m: 599 - No feature, enhancement, fix, bugfix, hotfix, or ops branches detected!"
83+
echo -e "ERROR: 599 - No feature, enhancement, fix, bugfix, hotfix, or ops branches detected!"
5784
exit 1 || true
5885
fi
5986
shell: bash
6087

6188
# See github contexts for more like github.action_path.
62-
# REF: https://docs.github.com/en/actions/learn-github-actions/contexts#github-context
89+
# REF: https://docs.github.com/en/actions/learn-github-actions/contexts#github-context

scripts/detectNewVersion.sh

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ DESCRIPTION
4747
-p Increments PATCH version on _every_ run.
4848
WARN: This is intended development use only.
4949
50+
-m Enables mono-repo mode, allowing matching against semvers prefixed with
51+
a product name. Eg: 'cool-app_1.2.3'
52+
53+
-n The product name to match against. EG: 'bob' would match tags like 'bob_1.2.3'.
54+
55+
-d The directory of the product to version. EG: 'path/to/bob'.
56+
5057
EXAMPLES
5158
The following detects the new version for the repo.
5259
@@ -75,7 +82,7 @@ fi
7582
# --------------------------------------------------------------------------------------------------
7683

7784
OPTIND=1
78-
while getopts "he:vfp" opt; do
85+
while getopts "he:vfpmn:d:" opt; do
7986
case $opt in
8087
h)
8188
printHelp
@@ -95,9 +102,23 @@ while getopts "he:vfp" opt; do
95102
p)
96103
arg_p='set'
97104
;;
105+
m)
106+
arg_m='set'
107+
arg_opts="$arg_opts -m"
108+
;;
109+
n)
110+
arg_n='set'
111+
arg_n_val="$OPTARG"
112+
arg_opts="$arg_opts -n $OPTARG"
113+
;;
114+
d)
115+
arg_d='set'
116+
arg_d_val="$OPTARG"
117+
arg_d_opt="--full-history"
118+
arg_opts="$arg_opts -d $OPTARG"
119+
;;
98120
*)
99121
echo -e "\e[01;31mERROR\e[00m: 570 - Invalid argument!"
100-
echo "::error title=Argument Error::ERROR 570 - Invalid argument!"
101122
printHelp
102123
if [[ "$sourced" == 0 ]]; then
103124
exit 0
@@ -119,13 +140,13 @@ tsCmd='date --utc +%FT%T.%3NZ'
119140
relative_path="$(dirname "${BASH_SOURCE[0]}")"
120141
dir="$(realpath "${relative_path}")"
121142

122-
lastVersion=$(/usr/bin/env bash -c "${dir}/detectPreviousVersion.sh")
123-
lastVersionMajor=$(/usr/bin/env bash -c "${dir}/validateSemver.sh -p major $lastVersion")
124-
lastVersionMinor=$(/usr/bin/env bash -c "${dir}/validateSemver.sh -p minor $lastVersion")
125-
lastVersionPatch=$(/usr/bin/env bash -c "${dir}/validateSemver.sh -p patch $lastVersion")
126-
lastVersionCommitHash=$(/usr/bin/env bash -c "${dir}/detectPreviousVersion.sh -c")
143+
lastVersion=$(/usr/bin/env bash -c "${dir}/detectPreviousVersion.sh -9 $arg_opts")
144+
lastVersionMajor=$(/usr/bin/env bash -c "${dir}/validateSemver.sh -p major $lastVersion $arg_opts")
145+
lastVersionMinor=$(/usr/bin/env bash -c "${dir}/validateSemver.sh -p minor $lastVersion $arg_opts")
146+
lastVersionPatch=$(/usr/bin/env bash -c "${dir}/validateSemver.sh -p patch $lastVersion $arg_opts")
147+
lastVersionCommitHash=$(/usr/bin/env bash -c "${dir}/detectPreviousVersion.sh -9 -c $arg_opts")
127148
lastCommitHash=$(git rev-parse HEAD)
128-
firstCommitHash=$(git rev-list --max-parents=0 HEAD)
149+
firstCommitHash=$(git rev-list --max-parents=0 HEAD | tail -n 1)
129150

130151
ci_name=$("${dir}/detect-ci.sh")
131152
origin=$(git config --get remote.origin.url)
@@ -155,7 +176,6 @@ case "$origin_host" in
155176
;;
156177
*)
157178
echo -e "\e[01;31mERROR\e[0m: 591 - Unsupported origin host."
158-
echo "::error title=Origin Host Error::ERROR 591 - Unsupported origin host!"
159179
exit 1
160180
;;
161181
esac
@@ -167,25 +187,24 @@ esac
167187
if [[ -n $arg_e ]]; then
168188
if [[ "$sourced" == 0 ]]; then
169189
echo -e "[$(${tsCmd})] \e[01;31mERROR\e[00m: 520 - You must source this script when specifying an environment variable! Eg: '. ./${0##*/} -e foo_ver'\n"
170-
echo "::error title=Usage Error::ERROR 520 - You must source this script when specifying an environment variable! Eg: '. ./foo.sh -e bar_ver'"
171190
exit 1
172191
fi
173192
fi
174193

175-
git log --pretty=oneline "$lastVersionCommitHash".."$lastCommitHash" | grep '+semver' | grep -q 'major\|breaking' && incrementMajor='true'
194+
git log $arg_d_opt --pretty=oneline "$lastVersionCommitHash".."$lastCommitHash" $arg_d_val | grep '+semver' | grep -q 'major\|breaking' && incrementMajor='true'
176195

177196
if [[ $incrementMajor != 'true' ]]; then
178197
IFS=$'\r\n'
179198
if [[ -n $arg_f ]]; then
180-
for i in $(git log --pretty=oneline "${firstCommitHash}".."${lastCommitHash}" | awk -v s="$merge_string" -v c="$column" '$0 ~ s {print $c}' | awk -v f="$field" -F'/' '{print $f}' | tr -d "'" | grep -i '^enhancement$\|^feature$\|^fix$\|^hotfix$\|^bugfix$\|^ops$' | awk -F '\r' '{print $1}' | sort | uniq -c | sort -nr) ; do
199+
for i in $(git log $arg_d_opt --pretty=oneline "${firstCommitHash}".."${lastCommitHash}" $arg_d_val | awk -v s="$merge_string" -v c="$column" '$0 ~ s {print $c}' | awk -v f="$field" -F'/' '{print $f}' | tr -d "'" | grep -i '^enhancement$\|^feature$\|^fix$\|^hotfix$\|^bugfix$\|^ops$' | awk -F '\r' '{print $1}' | sort | uniq -c | sort -nr) ; do
181200
varname=$(echo "$i" | awk '{print $2}')
182201
varname=${varname,,}
183202
value=$(echo "$i" | awk '{print $1}')
184203
value=${value,,}
185204
declare count_"$varname"="$value"
186205
done
187206
else
188-
for i in $(git log --pretty=oneline "${lastVersionCommitHash}".."${lastCommitHash}" | awk -v s="$merge_string" -v c="$column" '$0 ~ s {print $c}' | awk -v f="$field" -F'/' '{print $f}' | tr -d "'" | grep -i '^enhancement$\|^feature$\|^fix$\|^hotfix$\|^bugfix$\|^ops$' | awk -F '\r' '{print $1}' | sort | uniq -c | sort -nr) ; do
207+
for i in $(git log $arg_d_opt --pretty=oneline "${lastVersionCommitHash}".."${lastCommitHash}" $arg_d_val | awk -v s="$merge_string" -v c="$column" '$0 ~ s {print $c}' | awk -v f="$field" -F'/' '{print $f}' | tr -d "'" | grep -i '^enhancement$\|^feature$\|^fix$\|^hotfix$\|^bugfix$\|^ops$' | awk -F '\r' '{print $1}' | sort | uniq -c | sort -nr) ; do
189208
varname=$(echo "$i" | awk '{print $2}')
190209
varname=${varname,,}
191210
value=$(echo "$i" | awk '{print $1}')
@@ -208,7 +227,6 @@ if [[ -n $arg_f ]]; then
208227
else
209228
if [[ -z $incrementMajor && -z $count_feature && -z $count_enhancement && -z $count_fix && -z $count_bugfix && -z $count_hotfix && -z $count_ops ]]; then
210229
echo -e "\e[01;31mERROR\e[00m: 599 - No feature, enhancement, fix, bugfix, hotfix, or ops branches detected!"
211-
echo "::error title=No Valid Merge Detected::ERROR 599 - No feature, enhancement, fix, bugfix, hotfix, or ops branches detected!"
212230
exit 1
213231
fi
214232
fi
@@ -240,7 +258,8 @@ elif [[ -n $arg_p ]]; then
240258
newVersionPatch=$((lastVersionPatch + 1))
241259
fi
242260

243-
newVersion=$(/usr/bin/env bash -c "${dir}/validateSemver.sh -9p full $newVersionMajor.$newVersionMinor.$newVersionPatch")
261+
newVersion=$(/usr/bin/env bash -c "${dir}/validateSemver.sh -9p full $newVersionMajor.$newVersionMinor.$newVersionPatch $arg_opts")
262+
[[ -n $arg_n ]] && newVersion="${arg_n_val}_${newVersion}"
244263

245264
if [[ -n $arg_e ]]; then
246265
export_var="$arg_e_val"

0 commit comments

Comments
 (0)