20
20
https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event
21
21
22
22
Usage:
23
- python trigger_workflow.py -w workflow_filename -t github_token [-b branch_name]
23
+ python3 trigger_workflow.py -w workflow_filename -t github_token [-b branch_name]
24
24
[-r git_repo_url] [-p <input1> <value1> -p <input2> <value2> ...]'
25
25
[-C curl_command]
26
26
@@ -38,6 +38,7 @@ def main():
38
38
args = parse_cmdline_args ()
39
39
if args .branch is None :
40
40
args .branch = subprocess .check_output (['git' , 'rev-parse' , '--abbrev-ref' , 'HEAD' ]).decode ('utf-8' ).rstrip ('\n ' )
41
+ if args .branch == 'HEAD' : args .branch = 'main'
41
42
print ('autodetected branch: %s' % args .branch )
42
43
if args .repo : # else use default firebase/firebase-unity-sdk repo
43
44
if not firebase_github .set_repo_url (args .repo ):
@@ -52,13 +53,13 @@ def main():
52
53
print (f'request_url: { firebase_github .GITHUB_API_URL } /actions/workflows/{ args .workflow } /dispatches' )
53
54
print (f'request_body: ref: { args .branch } , inputs: { json_params } ' )
54
55
if args .dryrun :
55
- return (0 )
56
+ exit (0 )
56
57
57
58
print ('Sending request to GitHub API...' )
58
59
if not firebase_github .create_workflow_dispatch (args .token , args .workflow , args .branch , json_params ):
59
60
print ('%sFailed to trigger workflow %s' % (
60
61
'::error ::' if args .in_github_action else '' , args .workflow ))
61
- return ( - 1 )
62
+ exit ( 1 )
62
63
63
64
print ('Success!' )
64
65
time .sleep (args .sleep ) # Give a few seconds for the job to become queued.
@@ -67,10 +68,16 @@ def main():
67
68
workflows = firebase_github .list_workflows (args .token , args .workflow , args .branch )
68
69
run_id = 0
69
70
if "workflow_runs" in workflows :
71
+ try :
72
+ branch_sha = subprocess .check_output (['git' , 'rev-parse' , args .branch ]).decode ('utf-8' ).rstrip ('\n ' )
73
+ except subprocess .CalledProcessError as e :
74
+ # Failed to get branch SHA, ignore.
75
+ branch_sha = None
70
76
for workflow in workflows ['workflow_runs' ]:
71
77
# Use a heuristic to get the new workflow's run ID.
72
- # Must match the branch name, and be queued/in progress.
78
+ # Must match the branch name and commit sha , and be queued/in progress.
73
79
if (workflow ['status' ] in ('queued' , 'in_progress' ) and
80
+ (workflow ['head_sha' ] == branch_sha or not branch_sha ) and
74
81
workflow ['head_branch' ] == args .branch ):
75
82
run_id = workflow ['id' ]
76
83
break
@@ -79,8 +86,8 @@ def main():
79
86
workflow_url = 'https://github.com/firebase/firebase-unity-sdk/actions/runs/%s' % (run_id )
80
87
else :
81
88
# Couldn't get a run ID, use a generic URL.
82
- workflow_url = '/ %s/actions/workflows/%s?query=%s+%s' % (
83
- firebase_github . GITHUB_API_URL , args .workflow ,
89
+ workflow_url = '%s/actions/workflows/%s?query=%s+%s' % (
90
+ 'https://github.com/firebase/firebase-unity-sdk' , args .workflow ,
84
91
urllib .parse .quote ('event:workflow_dispatch' , safe = '' ),
85
92
urllib .parse .quote ('branch:' + args .branch , safe = '' ))
86
93
print ('%sStarted workflow %s: %s' % ('::warning ::' if args .in_github_action else '' ,
0 commit comments