Skip to content

Commit 7c2740d

Browse files
authored
Use GITHUB_REF_NAME as name of push branch; fix error in branch detection [citest skip] (#99)
We need to get the name of the branch to which CHANGELOG.md was pushed. For now, it looks as though `GITHUB_REF_NAME` is that name. But don't trust it - first, check that it is `main` or `master`. If not, then use a couple of other methods to determine what is the push branch. Signed-off-by: Rich Megginson <rmeggins@redhat.com>
1 parent 86eac8f commit 7c2740d

File tree

1 file changed

+32
-18
lines changed

1 file changed

+32
-18
lines changed

.github/workflows/changelog_to_tag.yml

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# yamllint disable rule:line-length
2-
name: Pushing CHANGELOG.md triggers tagging
2+
name: Pushing CHANGELOG.md triggers tag, release, and Galaxy publish
33
on: # yamllint disable-line rule:truthy
44
push:
55
branches:
@@ -10,7 +10,7 @@ on: # yamllint disable-line rule:truthy
1010
env:
1111
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
1212
jobs:
13-
tagging:
13+
tag_release_publish:
1414
runs-on: ubuntu-latest
1515
steps:
1616
- name: checkout PR
@@ -19,30 +19,44 @@ jobs:
1919
id: tag
2020
run: |
2121
set -euxo pipefail
22-
pat='\[[0-9]*\.[0-9]*\.[0-9]*\] - [0-9\-]*'
2322
print=false
24-
cat CHANGELOG.md | while read -r line; do
25-
if [[ "$line" =~ $pat ]] && [[ "$print" == false ]]; then
26-
echo "$line"
27-
print=true
28-
elif [[ "$line" =~ $pat ]] && [[ "$print" == true ]]; then
29-
break
30-
elif [[ "$print" == true ]]; then
23+
while read -r line; do
24+
if [[ "$line" =~ ^\[([0-9]+\.[0-9]+\.[0-9]+)\]\ -\ [0-9-]+ ]]; then
25+
if [ "$print" = false ]; then
26+
_tagname="${BASH_REMATCH[1]}"
27+
echo "$line"
28+
print=true
29+
else
30+
break
31+
fi
32+
elif [ "$print" = true ]; then
3133
echo "$line"
3234
fi
33-
done > ./.tagmsg.txt
34-
_tagname=$( grep -m 1 "[0-9]*\.[0-9]*\.[0-9]*" CHANGELOG.md | \
35-
sed -e "s/^.*\[\([0-9]*\.[0-9]*\.[0-9]*\)\].*/\1/" )
35+
done < CHANGELOG.md > ./.tagmsg.txt
3636
git fetch --all --tags
3737
for t in $( git tag -l ); do
38-
if [[ $t == "$_tagname" ]]; then
39-
echo INFO: tag $t already exists
38+
if [ "$t" = "$_tagname" ]; then
39+
echo INFO: tag "$t" already exists
4040
exit 1
4141
fi
4242
done
43-
# Get the main branch name, "master" or "main".
44-
_branch=$( git branch -r | grep -o 'origin/HEAD -> origin/.*$' | \
45-
awk -F'/' '{print $3}' )
43+
# Get name of the branch that the change was pushed to
44+
_branch="${GITHUB_REF_NAME:-}"
45+
if [ "$_branch" = master ] || [ "$_branch" = main ]; then
46+
echo Using branch name ["$_branch"] as push branch
47+
else
48+
echo WARNING: GITHUB_REF_NAME ["$_branch"] is not main or master
49+
_branch=$( git branch -r | grep -o 'origin/HEAD -> origin/.*$' | \
50+
awk -F'/' '{print $3}' || : )
51+
fi
52+
if [ -z "$_branch" ]; then
53+
_branch=$( git branch --points-at HEAD --no-color --format='%(refname:short)' )
54+
fi
55+
if [ -z "$_branch" ]; then
56+
echo ERROR: unable to determine push branch
57+
git branch -a
58+
exit 1
59+
fi
4660
echo ::set-output name=tagname::"$_tagname"
4761
echo ::set-output name=branch::"$_branch"
4862
- name: Create tag

0 commit comments

Comments
 (0)