-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgh_create_release.py
70 lines (57 loc) · 2.11 KB
/
gh_create_release.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import subprocess
import sys
import re
def run_command(command):
"""Run the specified command and handle errors."""
try:
result = subprocess.run(command, shell=True, check=True, capture_output=True, text=True)
print(result.stdout)
return result.returncode
except subprocess.CalledProcessError as e:
print(f"Error occurred: {e.stderr}", file=sys.stderr)
return e.returncode
def trigger_workflow(tag_name):
"""Trigger the GitHub Actions workflow using GitHub CLI."""
# Set repository and branch
repo = "GameAnalytics/GA-SDK-CPP-NEW"
branch = "main"
# Construct the GitHub CLI command
command = f'gh workflow run "Create Release" --repo {repo} --ref {branch} -f tag_name={tag_name}'
print(f"Running command: {command}")
# Execute the command
return_code = run_command(command)
if return_code == 0:
print("Workflow triggered successfully.")
else:
print(f"Failed to trigger workflow. Return code: {return_code}")
def check_gh_cli_installed():
"""Check if GitHub CLI (gh) is installed and available in the system path."""
try:
subprocess.run(['gh', '--version'], capture_output=True, check=True)
return True
except FileNotFoundError:
return False
def validate_tag_name(tag_name):
"""Validate that the tag name matches the format 'v1.0.0'."""
pattern = r"^v\d+\.\d+\.\d+$"
if re.match(pattern, tag_name):
return True
else:
return False
def main():
# Ensure GitHub CLI is installed
if not check_gh_cli_installed():
print("GitHub CLI (gh) is not installed. Please install it and try again.")
sys.exit(1)
# Validate tag name passed as an argument
if len(sys.argv) != 2:
print("Usage: python trigger_workflow.py <tag_name>")
sys.exit(1)
tag_name = sys.argv[1]
if not validate_tag_name(tag_name):
print("Invalid tag name format. Please use the format 'v1.0.0'.")
sys.exit(1)
# Trigger the workflow with validated tag name
trigger_workflow(tag_name)
if __name__ == "__main__":
main()