Skip to content

Commit a2c70ae

Browse files
authored
feature: add option to use git tag / git push instead of http soft tags (#210)
1 parent 698f1bb commit a2c70ae

File tree

4 files changed

+55
-23
lines changed

4 files changed

+55
-23
lines changed

.github/workflows/main.yml

+1
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ jobs:
2828
env:
2929
VERBOSE: true
3030
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
31+
GIT_API_TAGGING: false # uses git cli
3132

3233
# auto releases is not working atm and is deleting releases due branch tags
3334
- name: automatic-draft-release

.github/workflows/test.yml

+23-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,16 @@ jobs:
6666
DEFAULT_BUMP: none
6767
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
6868

69+
- name: Test action main5 (with_v true)
70+
id: test_main5
71+
uses: ./
72+
env:
73+
DRY_RUN: true
74+
WITH_V: true
75+
VERBOSE: true
76+
DEFAULT_BUMP: patch
77+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
78+
6979
# Use the action to generate a tag for itself
7080
- name: Test action pre1-release (with_v true)
7181
id: test_pre1
@@ -118,6 +128,10 @@ jobs:
118128
MAIN4_OUTPUT_NEWTAG=${{ steps.test_main4.outputs.new_tag }}
119129
MAIN4_OUTPUT_PART=${{ steps.test_main4.outputs.part }}
120130
131+
MAIN5_OUTPUT_TAG=${{ steps.test_main5.outputs.old_tag }}
132+
MAIN5_OUTPUT_NEWTAG=${{ steps.test_main5.outputs.new_tag }}
133+
MAIN5_OUTPUT_PART=${{ steps.test_main5.outputs.part }}
134+
121135
echo -e "> MAIN tests with_v, default bump:\n" >> $GITHUB_STEP_SUMMARY
122136
123137
echo "MAIN1 with_v Tag: $MAIN1_OUTPUT_TAG" >> $GITHUB_STEP_SUMMARY
@@ -154,6 +168,12 @@ jobs:
154168
echo "MAIN4 with_v New tag: $MAIN4_OUTPUT_NEWTAG" >> $GITHUB_STEP_SUMMARY
155169
echo "MAIN4 with_v Part: $MAIN4_OUTPUT_PART" >> $GITHUB_STEP_SUMMARY
156170
171+
echo -e "> MAIN tests with_v, bump patch:\n" >> $GITHUB_STEP_SUMMARY
172+
173+
echo "MAIN5 with_v Tag: $MAIN5_OUTPUT_TAG" >> $GITHUB_STEP_SUMMARY
174+
echo "MAIN5 with_v New tag: $MAIN5_OUTPUT_NEWTAG" >> $GITHUB_STEP_SUMMARY
175+
echo "MAIN5 with_v Part: $MAIN5_OUTPUT_PART" >> $GITHUB_STEP_SUMMARY
176+
157177
# check that the original tag got bumped either major, minor, patch
158178
verlte() {
159179
[ "$1" = "`echo -e "$1\n$2" | sort -V | head -n1`" ]
@@ -171,8 +191,10 @@ jobs:
171191
# needs to be the latest tag of the repo when bump is none
172192
main3="$([ "$MAIN3_OUTPUT_TAG" = "$MAIN3_OUTPUT_NEWTAG" ] && true || false)"
173193
main4="$([ "$MAIN4_OUTPUT_TAG" = "$MAIN4_OUTPUT_NEWTAG" ] && true || false)"
194+
# needs to be a greater tag in bump patch
195+
main5="$(verlt $MAIN5_OUTPUT_TAG $MAIN5_OUTPUT_NEWTAG && true || false)"
174196
175-
if $main1 && $pre1 && $main2 && $pre2 && $main3 && $main4
197+
if $main1 && $pre1 && $main2 && $pre2 && $main3 && $main4 && $main5
176198
then
177199
echo -e "\n>>>>The tags were created correctly" >> $GITHUB_STEP_SUMMARY
178200
else

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ _NOTE: set the fetch-depth for `actions/checkout@v2` or newer to be sure you ret
8989
- **CUSTOM_TAG** _(optional)_ - Set a custom tag, useful when generating tag based on f.ex FROM image in a docker image. **Setting this tag will invalidate any other settings set!**
9090
- **SOURCE** _(optional)_ - Operate on a relative path under $GITHUB_WORKSPACE.
9191
- **DRY_RUN** _(optional)_ - Determine the next version without tagging the branch. The workflow can use the outputs `new_tag` and `tag` in subsequent steps. Possible values are `true` and `false` (default).
92+
- **GIT_API_TAGGING** _(optional)_ - Set if using git cli or git api calls for tag push operations. Possible values are `false` and `true` (default).
9293
- **INITIAL_VERSION** _(optional)_ - Set initial version before bump. Default `0.0.0`.
9394
- **TAG_CONTEXT** _(optional)_ - Set the context of the previous tag. Possible values are `repo` (default) or `branch`.
9495
- **PRERELEASE** _(optional)_ - Define if workflow runs in prerelease mode, `false` by default. Note this will be overwritten if using complex suffix release branches. Use it with checkout `ref: ${{ github.sha }}` for consistency see [issue 266](https://github.com/anothrNick/github-tag-action/issues/266).

entrypoint.sh

+30-22
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ release_branches=${RELEASE_BRANCHES:-master,main}
1010
custom_tag=${CUSTOM_TAG:-}
1111
source=${SOURCE:-.}
1212
dryrun=${DRY_RUN:-false}
13+
git_api_tagging=${GIT_API_TAGGING:-true}
1314
initial_version=${INITIAL_VERSION:-0.0.0}
1415
tag_context=${TAG_CONTEXT:-repo}
1516
prerelease=${PRERELEASE:-false}
@@ -33,6 +34,7 @@ echo -e "\tRELEASE_BRANCHES: ${release_branches}"
3334
echo -e "\tCUSTOM_TAG: ${custom_tag}"
3435
echo -e "\tSOURCE: ${source}"
3536
echo -e "\tDRY_RUN: ${dryrun}"
37+
echo -e "\tGIT_API_TAGGING: ${git_api_tagging}"
3638
echo -e "\tINITIAL_VERSION: ${initial_version}"
3739
echo -e "\tTAG_CONTEXT: ${tag_context}"
3840
echo -e "\tPRERELEASE: ${prerelease}"
@@ -238,36 +240,42 @@ then
238240
exit 0
239241
fi
240242

243+
echo "EVENT: creating local tag $new"
241244
# create local git tag
242-
git tag "$new"
245+
git tag -f "$new" || exit 1
246+
echo "EVENT: pushing tag $new to origin"
243247

244-
# push new tag ref to github
245-
# this needs permissions in the workflow as contents: write
246-
dt=$(date '+%Y-%m-%dT%H:%M:%SZ')
247-
full_name=$GITHUB_REPOSITORY
248-
git_refs_url=$(jq .repository.git_refs_url "$GITHUB_EVENT_PATH" | tr -d '"' | sed 's/{\/sha}//g')
249-
250-
echo "$dt: **pushing tag $new to repo $full_name"
248+
if $git_api_tagging
249+
then
250+
# use git api to push
251+
dt=$(date '+%Y-%m-%dT%H:%M:%SZ')
252+
full_name=$GITHUB_REPOSITORY
253+
git_refs_url=$(jq .repository.git_refs_url "$GITHUB_EVENT_PATH" | tr -d '"' | sed 's/{\/sha}//g')
251254

252-
git_refs_response=$(
253-
curl -s -X POST "$git_refs_url" \
254-
-H "Authorization: token $GITHUB_TOKEN" \
255-
-d @- << EOF
255+
echo "$dt: **pushing tag $new to repo $full_name"
256256

257+
git_refs_response=$(
258+
curl -s -X POST "$git_refs_url" \
259+
-H "Authorization: token $GITHUB_TOKEN" \
260+
-d @- << EOF
257261
{
258-
"ref": "refs/tags/$new",
259-
"sha": "$commit"
262+
"ref": "refs/tags/$new",
263+
"sha": "$commit"
260264
}
261265
EOF
262266
)
263267

264-
git_ref_posted=$( echo "${git_refs_response}" | jq .ref | tr -d '"' )
268+
git_ref_posted=$( echo "${git_refs_response}" | jq .ref | tr -d '"' )
265269

266-
echo "::debug::${git_refs_response}"
267-
if [ "${git_ref_posted}" = "refs/tags/${new}" ]
268-
then
269-
exit 0
270+
echo "::debug::${git_refs_response}"
271+
if [ "${git_ref_posted}" = "refs/tags/${new}" ]
272+
then
273+
exit 0
274+
else
275+
echo "::error::Tag was not created properly."
276+
exit 1
277+
fi
270278
else
271-
echo "::error::Tag was not created properly."
272-
exit 1
273-
fi
279+
# use git cli to push
280+
git push -f origin "$new" || exit 1
281+
fi

0 commit comments

Comments
 (0)