Skip to content

Commit e9f84b9

Browse files
Elias BoulhartsElias Boulharts
authored andcommitted
feature: allow using custom tag message
Signed-off-by: Elias Boulharts <[email protected]>
1 parent 01d77ca commit e9f84b9

File tree

4 files changed

+130
-38
lines changed

4 files changed

+130
-38
lines changed

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: [email protected] # defaults to "41898282+github-actions[bot]@users.noreply.github.com"
8787
commit_author: Author <[email protected]> # defaults to "username <[email protected]>", 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 tag nor tag 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: 106 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="[email protected]"
3434
export INPUT_COMMIT_AUTHOR="Test Suite <[email protected]>"
35+
export INPUT_TAG=""
3536
export INPUT_TAGGING_MESSAGE=""
3637
export INPUT_PUSH_OPTIONS=""
3738
export INPUT_SKIP_DIRTY_CHECK=false
@@ -123,8 +124,9 @@ cat_github_output() {
123124
assert_line "INPUT_FILE_PATTERN: ."
124125
assert_line "INPUT_COMMIT_OPTIONS: "
125126
assert_line "::debug::Apply commit options "
127+
assert_line "INPUT_TAG: "
126128
assert_line "INPUT_TAGGING_MESSAGE: "
127-
assert_line "No tagging message supplied. No tag will be added."
129+
assert_line "Neither tag nor tag message is set. No tag will be added."
128130
assert_line "INPUT_PUSH_OPTIONS: "
129131
assert_line "::debug::Apply push options "
130132
assert_line "::debug::Push commit to remote branch ${FAKE_DEFAULT_BRANCH}"
@@ -146,8 +148,9 @@ cat_github_output() {
146148
assert_line "INPUT_FILE_PATTERN: ."
147149
assert_line "INPUT_COMMIT_OPTIONS: "
148150
assert_line "::debug::Apply commit options "
151+
assert_line "INPUT_TAG: "
149152
assert_line "INPUT_TAGGING_MESSAGE: "
150-
assert_line "No tagging message supplied. No tag will be added."
153+
assert_line "Neither tag nor tag message is set. No tag will be added."
151154
assert_line "INPUT_PUSH_OPTIONS: "
152155
assert_line "::debug::Apply push options "
153156
assert_line "::debug::Push commit to remote branch ${FAKE_DEFAULT_BRANCH}"
@@ -293,21 +296,24 @@ cat_github_output() {
293296
}
294297

295298
@test "It creates a tag with the commit" {
296-
INPUT_TAGGING_MESSAGE="v1.0.0"
299+
INPUT_TAG="v1.0.0"
300+
INPUT_TAGGING_MESSAGE="MyProduct v1.0.0"
297301

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

300304
run git_auto_commit
301305

302306
assert_success
303307

304-
assert_line "INPUT_TAGGING_MESSAGE: v1.0.0"
305-
assert_line "::debug::Create tag v1.0.0"
308+
assert_line "INPUT_TAG: v1.0.0"
309+
assert_line "INPUT_TAGGING_MESSAGE: MyProduct v1.0.0"
310+
311+
assert_line "::debug::Create tag v1.0.0: MyProduct v1.0.0"
306312
assert_line "::debug::Push commit to remote branch ${FAKE_DEFAULT_BRANCH}"
307313

308314
# Assert a tag v1.0.0 has been created
309-
run git tag
310-
assert_output v1.0.0
315+
run git tag -n
316+
assert_output 'v1.0.0 MyProduct v1.0.0'
311317

312318
run git ls-remote --tags --refs
313319
assert_output --partial refs/tags/v1.0.0
@@ -388,18 +394,20 @@ cat_github_output() {
388394
assert_equal $current_sha $remote_sha
389395
}
390396

391-
@test "It uses existing branch when INPUT_BRANCH is empty and INPUT_TAGGING_MESSAGE is set" {
397+
@test "It uses existing branch when INPUT_BRANCH is empty and INPUT_TAG is set" {
392398
INPUT_BRANCH=""
393-
INPUT_TAGGING_MESSAGE="v2.0.0"
399+
INPUT_TAG="v2.0.0"
400+
INPUT_TAGGING_MESSAGE="MyProduct v2.0.0"
401+
394402

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

397405
run git_auto_commit
398406

399407
assert_success
400408

401-
assert_line "INPUT_TAGGING_MESSAGE: v2.0.0"
402-
assert_line "::debug::Create tag v2.0.0"
409+
assert_line "INPUT_TAG: v2.0.0"
410+
assert_line "::debug::Create tag v2.0.0: MyProduct v2.0.0"
403411
assert_line "::debug::git push origin --tags"
404412

405413
# Assert a tag v2.0.0 has been created
@@ -413,16 +421,18 @@ cat_github_output() {
413421

414422
@test "It pushes generated commit and tag to remote and actually updates the commit shas" {
415423
INPUT_BRANCH=""
416-
INPUT_TAGGING_MESSAGE="v2.0.0"
424+
INPUT_TAG="v2.0.0"
425+
INPUT_TAGGING_MESSAGE="MyProduct v2.0.0"
426+
417427

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

420430
run git_auto_commit
421431

422432
assert_success
423433

424-
assert_line "INPUT_TAGGING_MESSAGE: v2.0.0"
425-
assert_line "::debug::Create tag v2.0.0"
434+
assert_line "INPUT_TAG: v2.0.0"
435+
assert_line "::debug::Create tag v2.0.0: MyProduct v2.0.0"
426436
assert_line "::debug::git push origin --tags"
427437

428438
# Assert a tag v2.0.0 has been created
@@ -442,16 +452,18 @@ cat_github_output() {
442452

443453
@test "It pushes generated commit and tag to remote branch and updates commit sha" {
444454
INPUT_BRANCH="a-new-branch"
445-
INPUT_TAGGING_MESSAGE="v2.0.0"
455+
INPUT_TAG="v2.0.0"
456+
INPUT_TAGGING_MESSAGE="MyProduct v2.0.0"
457+
446458

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

449461
run git_auto_commit
450462

451463
assert_success
452464

453-
assert_line "INPUT_TAGGING_MESSAGE: v2.0.0"
454-
assert_line "::debug::Create tag v2.0.0"
465+
assert_line "INPUT_TAG: v2.0.0"
466+
assert_line "::debug::Create tag v2.0.0: MyProduct v2.0.0"
455467
assert_line "::debug::Push commit to remote branch a-new-branch"
456468

457469
# Assert a tag v2.0.0 has been created
@@ -598,8 +610,9 @@ cat_github_output() {
598610
assert_line "INPUT_FILE_PATTERN: ."
599611
assert_line "INPUT_COMMIT_OPTIONS: "
600612
assert_line "::debug::Apply commit options "
613+
assert_line "INPUT_TAG: "
601614
assert_line "INPUT_TAGGING_MESSAGE: "
602-
assert_line "No tagging message supplied. No tag will be added."
615+
assert_line "Neither tag nor tag message is set. No tag will be added."
603616
assert_line "INPUT_PUSH_OPTIONS: "
604617
assert_line "::debug::Apply push options "
605618
assert_line "::debug::Push commit to remote branch not-existend-branch"
@@ -641,8 +654,9 @@ cat_github_output() {
641654
assert_line "INPUT_FILE_PATTERN: ."
642655
assert_line "INPUT_COMMIT_OPTIONS: "
643656
assert_line "::debug::Apply commit options "
657+
assert_line "INPUT_TAG: "
644658
assert_line "INPUT_TAGGING_MESSAGE: "
645-
assert_line "No tagging message supplied. No tag will be added."
659+
assert_line "Neither tag nor tag message is set. No tag will be added."
646660
assert_line "INPUT_PUSH_OPTIONS: "
647661
assert_line "::debug::Apply push options "
648662
assert_line "::debug::Push commit to remote branch not-existend-remote-branch"
@@ -700,8 +714,9 @@ cat_github_output() {
700714
assert_line "INPUT_FILE_PATTERN: ."
701715
assert_line "INPUT_COMMIT_OPTIONS: "
702716
assert_line "::debug::Apply commit options "
717+
assert_line "INPUT_TAG: "
703718
assert_line "INPUT_TAGGING_MESSAGE: "
704-
assert_line "No tagging message supplied. No tag will be added."
719+
assert_line "Neither tag nor tag message is set. No tag will be added."
705720
assert_line "INPUT_PUSH_OPTIONS: "
706721
assert_line "::debug::Apply push options "
707722
assert_line "::debug::Push commit to remote branch existing-remote-branch"
@@ -1031,8 +1046,9 @@ cat_github_output() {
10311046
assert_line "INPUT_FILE_PATTERN: ."
10321047
assert_line "INPUT_COMMIT_OPTIONS: "
10331048
assert_line "::debug::Apply commit options "
1049+
assert_line "INPUT_TAG: "
10341050
assert_line "INPUT_TAGGING_MESSAGE: "
1035-
assert_line "No tagging message supplied. No tag will be added."
1051+
assert_line "Neither tag nor tag message is set. No tag will be added."
10361052
assert_line "INPUT_PUSH_OPTIONS: "
10371053
assert_line "::debug::Apply push options "
10381054
assert_line "::debug::Push commit to remote branch ${FAKE_DEFAULT_BRANCH}"
@@ -1119,16 +1135,17 @@ END
11191135

11201136
@test "it creates a tag if create_git_tag_only is set to true and a message has been supplied" {
11211137
INPUT_CREATE_GIT_TAG_ONLY=true
1122-
INPUT_TAGGING_MESSAGE=v1.0.0
1138+
INPUT_TAG=v1.0.0
1139+
INPUT_TAGGING_MESSAGE="MyProduct v1.0.0"
11231140

11241141
run git_auto_commit
11251142

11261143
assert_success
11271144

11281145
assert_line "::debug::Create git tag only"
11291146

1130-
assert_line "::debug::Create tag v1.0.0"
1131-
refute_line "No tagging message supplied. No tag will be added."
1147+
assert_line "::debug::Create tag v1.0.0: MyProduct v1.0.0"
1148+
refute_line "Neither tag nor tag message is set. No tag will be added."
11321149

11331150
assert_line "::debug::Apply push options "
11341151
assert_line "::debug::Push commit to remote branch ${FAKE_DEFAULT_BRANCH}"
@@ -1139,23 +1156,25 @@ END
11391156
refute_line -e "commit_hash=[0-9a-f]{40}$"
11401157

11411158
# Assert a tag v1.0.0 has been created
1142-
run git tag
1143-
assert_output v1.0.0
1159+
run git tag -n
1160+
assert_output 'v1.0.0 MyProduct v1.0.0'
11441161

11451162
run git ls-remote --tags --refs
11461163
assert_output --partial refs/tags/v1.0.0
11471164
}
11481165

11491166
@test "it output no tagging message supplied if no tagging message is set but create_git_tag_only is set to true" {
11501167
INPUT_CREATE_GIT_TAG_ONLY=true
1168+
INPUT_TAG=""
11511169
INPUT_TAGGING_MESSAGE=""
11521170

11531171
run git_auto_commit
11541172

11551173
assert_success
11561174

1175+
assert_line "INPUT_TAG: "
11571176
assert_line "INPUT_TAGGING_MESSAGE: "
1158-
assert_line "No tagging message supplied. No tag will be added."
1177+
assert_line "Neither tag nor tag message is set. No tag will be added."
11591178
assert_line "::debug::Create git tag only"
11601179

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

0 commit comments

Comments
 (0)