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