Skip to content

Commit 45cafce

Browse files
committed
Skip tag creation when tag already points to the correct commit
A warning is generated when this is detected. Also add a new option `skip_tag_creation` which forces this behaviour, but doesn't generate a warning.
1 parent 54953a8 commit 45cafce

3 files changed

Lines changed: 68 additions & 32 deletions

File tree

README.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,18 @@ It is made in python + docker... cause that's the languages I know
1818
### Input variables this action takes
1919

2020
| Input Name | Description |
21-
|-------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
22-
| token | **Required.** The token this action should use for accessing the GitHub API. Most often you'll want to set this to `${{ secrets.GITHUB_TOKEN }}` |
23-
| tag | **Required.** Name of the tag for the release. |
24-
| name | **Required for new releases.** The title of the release. If not set, the title will not be changed... unless the release does not exist; then it'll fail |
25-
| description | **Required for new releases.** The description of the release. If not set, the description will not be changed... unless the release does not exist; then it'll fail |
26-
| prerelease | **Optional.** Whether the release should be marked as a prerelease. Accepted values: true, false. Defaults to false for new releases, keeps the current setting for existing releases |
27-
| draft | **Optional.** Whether the release should be marked as a draft. Accepted values: true, false. Defaults to false for new releases, keeps the current setting for existing releases |
28-
| target_commit | **Optional.** Commit, branch or tag (or anything `git rev-parse` can resolve) the release tag should be created at or moved to. Defaults to `$GITHUB_SHA`, aka. the "current" commit. |
29-
| files | **Optional.** A newline seperated list of files to attach to the release. Recursive globbing is supported (anything the python `glob.glob()` function can resolve). |
30-
| fail_on_no_files | **Optional.** Should the action fail if no filename globs match? Does nothing if no files were listed. Accepted values: true, false. Default: false. |
31-
| clear_attachments | **Optional.** Should we remove all existing attachments from the release before adding new ones? Accepted values: true, false. Default: false. |
21+
|-------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
22+
| token | **Required.** The token this action should use for accessing the GitHub API. Most often you'll want to set this to `${{ secrets.GITHUB_TOKEN }}` |
23+
| tag | **Required.** Name of the tag for the release. |
24+
| name | **Required for new releases.** The title of the release. If not set, the title will not be changed... unless the release does not exist; then it'll fail |
25+
| description | **Required for new releases.** The description of the release. If not set, the description will not be changed... unless the release does not exist; then it'll fail |
26+
| prerelease | **Optional.** Whether the release should be marked as a prerelease. Accepted values: true, false. Defaults to false for new releases, keeps the current setting for existing releases |
27+
| draft | **Optional.** Whether the release should be marked as a draft. Accepted values: true, false. Defaults to false for new releases, keeps the current setting for existing releases |
28+
| target_commit | **Optional.** Commit, branch or tag (or anything `git rev-parse` can resolve) the release tag should be created at or moved to. Defaults to `$GITHUB_SHA`, aka. the "current" commit, ignored when `skip_tag_creation` is set to true. |
29+
| files | **Optional.** A newline seperated list of files to attach to the release. Recursive globbing is supported (anything the python `glob.glob()` function can resolve). |
30+
| fail_on_no_files | **Optional.** Should the action fail if no filename globs match? Does nothing if no files were listed. Accepted values: true, false. Default: false. |
31+
| clear_attachments | **Optional.** Should we remove all existing attachments from the release before adding new ones? Accepted values: true, false. Default: false. |
32+
| skip_tag_creation | **Optional.** Should the action skip tag creation? Set to true if the tag already points to the correct commit. Accepted values: true, false. Default: false. |
3233

3334
### Environment variables this action uses
3435
Only default environment variables: `$GITHUB_SHA`, `$GITHUB_API_URL` and `$GITHUB_REPOSITORY`
@@ -96,11 +97,11 @@ selfhosted runner's disk space, you can use the prebuilt images instead.
9697
9798
To replace the action with the prebuilt image, simply replace
9899
```yaml
99-
uses: mini-bomba/create-github-release@v1.1.3
100+
uses: mini-bomba/create-github-release@v1.2.0
100101
```
101102
with
102103
```yaml
103-
uses: docker://ghcr.io/mini-bomba/create-github-release:v1.1.3
104+
uses: docker://ghcr.io/mini-bomba/create-github-release:v1.2.0
104105
```
105106
in your workflows.
106107

action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,10 @@ inputs:
3333
description: "Should all existing attachments be deleted before uploading new files?"
3434
required: false
3535
default: "true"
36+
skip_tag_creation:
37+
description: "Should the tag creation/modification be skipped? Set to true if the tag already exists and points to the correct commit"
38+
required: false
39+
default: "false"
3640
runs:
3741
using: "docker"
3842
image: "Dockerfile"

src/main.py

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
import subprocess
99
import secrets
1010

11+
1112
def check_input(key: str) -> bool:
1213
"""
1314
Checks if a given key was passed in as an input variable
1415
"""
1516
return f'INPUT_{key}' in os.environ and os.environ[f'INPUT_{key}'] != ""
1617

18+
1719
def get_boolean(key: str) -> bool:
1820
"""
1921
Parses an environment variable as a boolean
@@ -27,6 +29,7 @@ def get_boolean(key: str) -> bool:
2729
print(f"::error::❌ Invalid '{key.lower()}' input argument: '{os.environ['INPUT_{key}']}'")
2830
exit(1)
2931

32+
3033
def run_command(cmd: list[str], end_group: bool = False):
3134
"""
3235
Runs a given command, surrounding output with ::stop-commands::
@@ -62,22 +65,46 @@ def run_command(cmd: list[str], end_group: bool = False):
6265
print('::debug::😩 Attempting a workaround for the "dubious ownership" git error')
6366
run_command(["git", "config", "--global", "--add", "safe.directory", "/github/workspace"])
6467

65-
if check_input("TARGET_COMMIT"):
66-
target_commit = os.environ['INPUT_TARGET_COMMIT']
67-
proc = subprocess.Popen(['git', 'rev-parse', target_commit], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
68-
out, err = proc.communicate()
69-
if proc.returncode != 0:
70-
print(f"::error::❌ Failed to resolve ref '{target_commit}' (from input 'target_commit')")
71-
err = err.decode()
72-
for line in err.split("\n"):
73-
print(f"::error::{line}")
74-
exit(proc.returncode)
75-
out = out.decode().strip()
76-
if out != target_commit:
77-
print(f"🔬 Resolved reference '{target_commit}' to commit '{out}")
78-
target_commit = out
79-
else:
80-
target_commit = os.environ['GITHUB_SHA']
68+
skip_tag_creation = get_boolean("SKIP_TAG_CREATION") if check_input("SKIP_TAG_CREATION") else False
69+
target_commit = None
70+
71+
if not skip_tag_creation:
72+
if check_input("TARGET_COMMIT"):
73+
target_commit = os.environ['INPUT_TARGET_COMMIT']
74+
if target_commit == tag_name:
75+
print("::warning::⚠️ target_commit and tag inputs are the same - tag creation will be skipped "
76+
"(set skip_tag_creation to true to silence this warning)")
77+
skip_tag_creation = True
78+
target_commit = None
79+
else:
80+
proc = subprocess.Popen(['git', 'rev-parse', target_commit], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
81+
out, err = proc.communicate()
82+
if proc.returncode != 0:
83+
print(f"::error::❌ Failed to resolve ref '{target_commit}' (from input 'target_commit')")
84+
err = err.decode()
85+
for line in err.split("\n"):
86+
print(f"::error::{line}")
87+
exit(proc.returncode)
88+
out = out.decode().strip()
89+
if out != target_commit:
90+
print(f"🔬 Resolved reference '{target_commit}' to commit '{out}'")
91+
target_commit = out
92+
else:
93+
target_commit = os.environ['GITHUB_SHA']
94+
print(f"::debug::🔬 Attempting to resolve tag '{tag_name}' to check if it points to target_commit")
95+
proc = subprocess.Popen(['git', 'rev-parse', tag_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
96+
out, err = proc.communicate()
97+
if proc.returncode != 0:
98+
print(f"::debug::🔬 Tag '{tag_name}' either doesn't exist yet, or wasn't checked out")
99+
else:
100+
out = out.decode().strip()
101+
print(f"::debug::🔬 Resolved tag '{tag_name}' to commit '{out}'")
102+
if out == target_commit:
103+
print(f"::warning::⚠️ Tag '{tag_name}' exists and already points to target commit '{target_commit}' - "
104+
"tag creation will be skipped (set skip_tag_creation to true to silence this warning)")
105+
skip_tag_creation = True
106+
target_commit = None
107+
81108

82109
if not check_input("PRERELEASE"):
83110
prerelease = None
@@ -155,9 +182,13 @@ def run_command(cmd: list[str], end_group: bool = False):
155182
exit(1)
156183

157184
# Create/move tag
158-
print("::group::🏷️ Creating/Moving the tag...")
159-
run_command(["git", "tag", "-f", tag_name, target_commit], end_group=True)
160-
run_command(["git", "push", "--force", "origin", tag_name], end_group=True)
185+
if skip_tag_creation:
186+
print("⏩ Skipping tag creation")
187+
else:
188+
print("::group::🏷️ Creating/Moving the tag...")
189+
run_command(["git", "tag", "-f", tag_name, target_commit], end_group=True)
190+
run_command(["git", "push", "--force", "origin", tag_name], end_group=True)
191+
print("::endgroup::")
161192

162193
print("::group::📦 Creating/Updating the release...")
163194
if release is not None:

0 commit comments

Comments
 (0)