Skip to content

Commit 51efd77

Browse files
Elias BoulhartsElias Boulharts
authored andcommitted
feature: allow using custom tag message
Signed-off-by: Elias Boulharts
1 parent 01d77ca commit 51efd77

4 files changed

Lines changed: 122 additions & 38 deletions

File tree

README.md

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,16 @@ The following is an extended example with all available options.
8585
commit_user_name: My GitHub Actions Bot # defaults to "github-actions[bot]"
8686
commit_user_email: my-github-actions-bot@example.org # defaults to "41898282+github-actions[bot]@users.noreply.github.com"
8787
commit_author: Author <actions@github.com> # defaults to "username <numeric_id+username@users.noreply.github.com>", where "numeric_id" and "username" belong to the author of the commit that triggered the run
88+
89+
# Optional. Tag name to be created in the local repository and
90+
# pushed to the remote repository on the defined branch.
91+
# If only one of `tag` or `tagging_message` is provided, the value of the provided field will be used for both tag name and message.
92+
tag: 'v1.0.0'
93+
94+
# Optional. Message to annotate the created tag with.
95+
# If only one of `tag` or `tagging_message` is provided, the value of the provided field will be used for both tag name and message.
96+
tagging_message: 'MyProduct v1.0.0'
8897

89-
# Optional. Tag name being created in the local repository and
90-
# pushed to remote repository and defined branch.
91-
tagging_message: 'v1.0.0'
9298

9399
# Optional. Option used by `git-status` to determine if the repository is
94100
# dirty. See https://git-scm.com/docs/git-status#_options

action.yml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,12 @@ inputs:
4444
description: Value used for the commit author. Defaults to the username of whoever triggered this workflow run.
4545
required: false
4646
default: ${{ github.actor }} <${{ github.actor_id }}+${{ github.actor }}@users.noreply.github.com>
47+
tag:
48+
description: New git tag with the commit. Keep this empty, if no tag should be created.
49+
required: false
50+
default: ''
4751
tagging_message:
48-
description: Message used to create a new git tag with the commit. Keep this empty, if no tag should be created.
52+
description: Message used to create a new git tag with the commit.
4953
required: false
5054
default: ''
5155
push_options:

entrypoint.sh

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,17 @@ _local_commit() {
159159
}
160160
161161
_tag_commit() {
162+
echo "INPUT_TAG: ${INPUT_TAG}"
162163
echo "INPUT_TAGGING_MESSAGE: ${INPUT_TAGGING_MESSAGE}"
163164
164-
if [ -n "$INPUT_TAGGING_MESSAGE" ]
165-
then
166-
_log "debug" "Create tag $INPUT_TAGGING_MESSAGE";
167-
git -c user.name="$INPUT_COMMIT_USER_NAME" -c user.email="$INPUT_COMMIT_USER_EMAIL" tag -a "$INPUT_TAGGING_MESSAGE" -m "$INPUT_TAGGING_MESSAGE";
165+
if [ -n "$INPUT_TAG" ] || [ -n "$INPUT_TAGGING_MESSAGE" ]; then
166+
TAG=${INPUT_TAG:-$INPUT_TAGGING_MESSAGE}
167+
MESSAGE=${INPUT_TAGGING_MESSAGE:-$INPUT_TAG}
168+
169+
_log "debug" "Create tag $TAG: $MESSAGE"
170+
git -c user.name="$INPUT_COMMIT_USER_NAME" -c user.email="$INPUT_COMMIT_USER_EMAIL" tag -a "$TAG" -m "$MESSAGE"
168171
else
169-
echo "No tagging message supplied. No tag will be added.";
172+
echo "Neither INPUT_TAG nor INPUT_TAGGING_MESSAGE is set. No tag will be added."
170173
fi
171174
}
172175
@@ -182,8 +185,8 @@ _push_to_github() {
182185
183186
if [ -z "$INPUT_BRANCH" ]
184187
then
185-
# Only add `--tags` option, if `$INPUT_TAGGING_MESSAGE` is set
186-
if [ -n "$INPUT_TAGGING_MESSAGE" ]
188+
# Only add `--tags` option, if `$INPUT_TAG` or `$INPUT_TAGGING_MESSAGE` is set
189+
if [ -n "$INPUT_TAG" ] || [ -n "$INPUT_TAGGING_MESSAGE" ]
187190
then
188191
_log "debug" "git push origin --tags";
189192
git push origin --follow-tags --atomic ${INPUT_PUSH_OPTIONS:+"${INPUT_PUSH_OPTIONS_ARRAY[@]}"};

tests/git-auto-commit.bats

Lines changed: 98 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ setup() {
3232
export INPUT_COMMIT_USER_NAME="Test Suite"
3333
export INPUT_COMMIT_USER_EMAIL="test@github.com"
3434
export INPUT_COMMIT_AUTHOR="Test Suite <test@users.noreply.github.com>"
35+
export INPUT_TAG=""
3536
export INPUT_TAGGING_MESSAGE=""
3637
export INPUT_PUSH_OPTIONS=""
3738
export INPUT_SKIP_DIRTY_CHECK=false
@@ -123,7 +124,7 @@ cat_github_output() {
123124
assert_line "INPUT_FILE_PATTERN: ."
124125
assert_line "INPUT_COMMIT_OPTIONS: "
125126
assert_line "::debug::Apply commit options "
126-
assert_line "INPUT_TAGGING_MESSAGE: "
127+
assert_line "INPUT_TAG: "
127128
assert_line "No tagging message supplied. No tag will be added."
128129
assert_line "INPUT_PUSH_OPTIONS: "
129130
assert_line "::debug::Apply push options "
@@ -146,7 +147,7 @@ cat_github_output() {
146147
assert_line "INPUT_FILE_PATTERN: ."
147148
assert_line "INPUT_COMMIT_OPTIONS: "
148149
assert_line "::debug::Apply commit options "
149-
assert_line "INPUT_TAGGING_MESSAGE: "
150+
assert_line "INPUT_TAG: "
150151
assert_line "No tagging message supplied. No tag will be added."
151152
assert_line "INPUT_PUSH_OPTIONS: "
152153
assert_line "::debug::Apply push options "
@@ -293,21 +294,24 @@ cat_github_output() {
293294
}
294295

295296
@test "It creates a tag with the commit" {
296-
INPUT_TAGGING_MESSAGE="v1.0.0"
297+
INPUT_TAG="v1.0.0"
298+
INPUT_TAGGING_MESSAGE="MyProduct v1.0.0"
297299

298300
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
299301

300302
run git_auto_commit
301303

302304
assert_success
303305

304-
assert_line "INPUT_TAGGING_MESSAGE: v1.0.0"
305-
assert_line "::debug::Create tag v1.0.0"
306+
assert_line "INPUT_TAG: v1.0.0"
307+
assert_line "INPUT_TAGGING_MESSAGE: MyProduct v1.0.0"
308+
309+
assert_line "::debug::Create tag v1.0.0: MyProduct v1.0.0"
306310
assert_line "::debug::Push commit to remote branch ${FAKE_DEFAULT_BRANCH}"
307311

308312
# Assert a tag v1.0.0 has been created
309-
run git tag
310-
assert_output v1.0.0
313+
run git tag -n
314+
assert_output 'v1.0.0 MyProduct v1.0.0'
311315

312316
run git ls-remote --tags --refs
313317
assert_output --partial refs/tags/v1.0.0
@@ -388,18 +392,20 @@ cat_github_output() {
388392
assert_equal $current_sha $remote_sha
389393
}
390394

391-
@test "It uses existing branch when INPUT_BRANCH is empty and INPUT_TAGGING_MESSAGE is set" {
395+
@test "It uses existing branch when INPUT_BRANCH is empty and INPUT_TAG is set" {
392396
INPUT_BRANCH=""
393-
INPUT_TAGGING_MESSAGE="v2.0.0"
397+
INPUT_TAG="v2.0.0"
398+
INPUT_TAGGING_MESSAGE="MyProduct v2.0.0"
399+
394400

395401
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
396402

397403
run git_auto_commit
398404

399405
assert_success
400406

401-
assert_line "INPUT_TAGGING_MESSAGE: v2.0.0"
402-
assert_line "::debug::Create tag v2.0.0"
407+
assert_line "INPUT_TAG: v2.0.0"
408+
assert_line "::debug::Create tag v2.0.0: MyProduct v2.0.0"
403409
assert_line "::debug::git push origin --tags"
404410

405411
# Assert a tag v2.0.0 has been created
@@ -413,16 +419,18 @@ cat_github_output() {
413419

414420
@test "It pushes generated commit and tag to remote and actually updates the commit shas" {
415421
INPUT_BRANCH=""
416-
INPUT_TAGGING_MESSAGE="v2.0.0"
422+
INPUT_TAG="v2.0.0"
423+
INPUT_TAGGING_MESSAGE="MyProduct v2.0.0"
424+
417425

418426
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
419427

420428
run git_auto_commit
421429

422430
assert_success
423431

424-
assert_line "INPUT_TAGGING_MESSAGE: v2.0.0"
425-
assert_line "::debug::Create tag v2.0.0"
432+
assert_line "INPUT_TAG: v2.0.0"
433+
assert_line "::debug::Create tag v2.0.0: MyProduct v2.0.0"
426434
assert_line "::debug::git push origin --tags"
427435

428436
# Assert a tag v2.0.0 has been created
@@ -442,16 +450,18 @@ cat_github_output() {
442450

443451
@test "It pushes generated commit and tag to remote branch and updates commit sha" {
444452
INPUT_BRANCH="a-new-branch"
445-
INPUT_TAGGING_MESSAGE="v2.0.0"
453+
INPUT_TAG="v2.0.0"
454+
INPUT_TAGGING_MESSAGE="MyProduct v2.0.0"
455+
446456

447457
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
448458

449459
run git_auto_commit
450460

451461
assert_success
452462

453-
assert_line "INPUT_TAGGING_MESSAGE: v2.0.0"
454-
assert_line "::debug::Create tag v2.0.0"
463+
assert_line "INPUT_TAG: v2.0.0"
464+
assert_line "::debug::Create tag v2.0.0: MyProduct v2.0.0"
455465
assert_line "::debug::Push commit to remote branch a-new-branch"
456466

457467
# Assert a tag v2.0.0 has been created
@@ -598,7 +608,7 @@ cat_github_output() {
598608
assert_line "INPUT_FILE_PATTERN: ."
599609
assert_line "INPUT_COMMIT_OPTIONS: "
600610
assert_line "::debug::Apply commit options "
601-
assert_line "INPUT_TAGGING_MESSAGE: "
611+
assert_line "INPUT_TAG: "
602612
assert_line "No tagging message supplied. No tag will be added."
603613
assert_line "INPUT_PUSH_OPTIONS: "
604614
assert_line "::debug::Apply push options "
@@ -641,7 +651,7 @@ cat_github_output() {
641651
assert_line "INPUT_FILE_PATTERN: ."
642652
assert_line "INPUT_COMMIT_OPTIONS: "
643653
assert_line "::debug::Apply commit options "
644-
assert_line "INPUT_TAGGING_MESSAGE: "
654+
assert_line "INPUT_TAG: "
645655
assert_line "No tagging message supplied. No tag will be added."
646656
assert_line "INPUT_PUSH_OPTIONS: "
647657
assert_line "::debug::Apply push options "
@@ -700,7 +710,7 @@ cat_github_output() {
700710
assert_line "INPUT_FILE_PATTERN: ."
701711
assert_line "INPUT_COMMIT_OPTIONS: "
702712
assert_line "::debug::Apply commit options "
703-
assert_line "INPUT_TAGGING_MESSAGE: "
713+
assert_line "INPUT_TAG: "
704714
assert_line "No tagging message supplied. No tag will be added."
705715
assert_line "INPUT_PUSH_OPTIONS: "
706716
assert_line "::debug::Apply push options "
@@ -1031,7 +1041,7 @@ cat_github_output() {
10311041
assert_line "INPUT_FILE_PATTERN: ."
10321042
assert_line "INPUT_COMMIT_OPTIONS: "
10331043
assert_line "::debug::Apply commit options "
1034-
assert_line "INPUT_TAGGING_MESSAGE: "
1044+
assert_line "INPUT_TAG: "
10351045
assert_line "No tagging message supplied. No tag will be added."
10361046
assert_line "INPUT_PUSH_OPTIONS: "
10371047
assert_line "::debug::Apply push options "
@@ -1119,15 +1129,16 @@ END
11191129

11201130
@test "it creates a tag if create_git_tag_only is set to true and a message has been supplied" {
11211131
INPUT_CREATE_GIT_TAG_ONLY=true
1122-
INPUT_TAGGING_MESSAGE=v1.0.0
1132+
INPUT_TAG=v1.0.0
1133+
INPUT_TAGGING_MESSAGE="MyProduct v1.0.0"
11231134

11241135
run git_auto_commit
11251136

11261137
assert_success
11271138

11281139
assert_line "::debug::Create git tag only"
11291140

1130-
assert_line "::debug::Create tag v1.0.0"
1141+
assert_line "::debug::Create tag v1.0.0: MyProduct v1.0.0"
11311142
refute_line "No tagging message supplied. No tag will be added."
11321143

11331144
assert_line "::debug::Apply push options "
@@ -1139,22 +1150,22 @@ END
11391150
refute_line -e "commit_hash=[0-9a-f]{40}$"
11401151

11411152
# Assert a tag v1.0.0 has been created
1142-
run git tag
1143-
assert_output v1.0.0
1153+
run git tag -n
1154+
assert_output 'v1.0.0 MyProduct v1.0.0'
11441155

11451156
run git ls-remote --tags --refs
11461157
assert_output --partial refs/tags/v1.0.0
11471158
}
11481159

11491160
@test "it output no tagging message supplied if no tagging message is set but create_git_tag_only is set to true" {
11501161
INPUT_CREATE_GIT_TAG_ONLY=true
1151-
INPUT_TAGGING_MESSAGE=""
1162+
INPUT_TAG=""
11521163

11531164
run git_auto_commit
11541165

11551166
assert_success
11561167

1157-
assert_line "INPUT_TAGGING_MESSAGE: "
1168+
assert_line "INPUT_TAG: "
11581169
assert_line "No tagging message supplied. No tag will be added."
11591170
assert_line "::debug::Create git tag only"
11601171

@@ -1181,3 +1192,63 @@ END
11811192
assert_line "::warning::git-auto-commit: skip_checkout has been removed in v6. It does not have any effect anymore."
11821193
assert_line "::warning::git-auto-commit: create_branch has been removed in v6. It does not have any effect anymore."
11831194
}
1195+
1196+
@test "Set a tag message only" {
1197+
INPUT_TAGGING_MESSAGE="v1.0.0"
1198+
1199+
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
1200+
1201+
run git_auto_commit
1202+
1203+
assert_success
1204+
1205+
assert_line "INPUT_TAG: "
1206+
assert_line "INPUT_TAGGING_MESSAGE: v1.0.0"
1207+
1208+
assert_line "::debug::Create tag v1.0.0: v1.0.0"
1209+
assert_line "::debug::Push commit to remote branch ${FAKE_DEFAULT_BRANCH}"
1210+
1211+
# Assert a tag v1.0.0 has been created
1212+
run git tag -n
1213+
assert_output 'v1.0.0 v1.0.0'
1214+
1215+
run git ls-remote --tags --refs
1216+
assert_output --partial refs/tags/v1.0.0
1217+
1218+
# Assert that the commit has been pushed with --force and
1219+
# sha values are equal on local and remote
1220+
current_sha="$(git rev-parse --verify --short ${FAKE_DEFAULT_BRANCH})"
1221+
remote_sha="$(git rev-parse --verify --short origin/${FAKE_DEFAULT_BRANCH})"
1222+
1223+
assert_equal $current_sha $remote_sha
1224+
}
1225+
1226+
@test "Set a tag only" {
1227+
INPUT_TAG="v1.0.0"
1228+
1229+
touch "${FAKE_LOCAL_REPOSITORY}"/new-file-{1,2,3}.txt
1230+
1231+
run git_auto_commit
1232+
1233+
assert_success
1234+
1235+
assert_line "INPUT_TAG: v1.0.0"
1236+
assert_line "INPUT_TAGGING_MESSAGE: "
1237+
1238+
assert_line "::debug::Create tag v1.0.0: v1.0.0"
1239+
assert_line "::debug::Push commit to remote branch ${FAKE_DEFAULT_BRANCH}"
1240+
1241+
# Assert a tag v1.0.0 has been created
1242+
run git tag -n
1243+
assert_output 'v1.0.0 v1.0.0'
1244+
1245+
run git ls-remote --tags --refs
1246+
assert_output --partial refs/tags/v1.0.0
1247+
1248+
# Assert that the commit has been pushed with --force and
1249+
# sha values are equal on local and remote
1250+
current_sha="$(git rev-parse --verify --short ${FAKE_DEFAULT_BRANCH})"
1251+
remote_sha="$(git rev-parse --verify --short origin/${FAKE_DEFAULT_BRANCH})"
1252+
1253+
assert_equal $current_sha $remote_sha
1254+
}

0 commit comments

Comments
 (0)