Skip to content

Commit e7c9ca3

Browse files
committed
Duplicate the C++ trigger workflow script.
1 parent 56d2b70 commit e7c9ca3

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

scripts/gha/trigger_workflow.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
https://docs.github.com/en/rest/reference/actions#create-a-workflow-dispatch-event
2121
2222
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]
2424
[-r git_repo_url] [-p <input1> <value1> -p <input2> <value2> ...]'
2525
[-C curl_command]
2626
@@ -38,6 +38,7 @@ def main():
3838
args = parse_cmdline_args()
3939
if args.branch is None:
4040
args.branch=subprocess.check_output(['git', 'rev-parse', '--abbrev-ref', 'HEAD']).decode('utf-8').rstrip('\n')
41+
if args.branch == 'HEAD': args.branch = 'main'
4142
print('autodetected branch: %s' % args.branch)
4243
if args.repo: # else use default firebase/firebase-unity-sdk repo
4344
if not firebase_github.set_repo_url(args.repo):
@@ -52,13 +53,13 @@ def main():
5253
print(f'request_url: {firebase_github.GITHUB_API_URL}/actions/workflows/{args.workflow}/dispatches')
5354
print(f'request_body: ref: {args.branch}, inputs: {json_params}')
5455
if args.dryrun:
55-
return(0)
56+
exit(0)
5657

5758
print('Sending request to GitHub API...')
5859
if not firebase_github.create_workflow_dispatch(args.token, args.workflow, args.branch, json_params):
5960
print('%sFailed to trigger workflow %s' % (
6061
'::error ::' if args.in_github_action else '', args.workflow))
61-
return(-1)
62+
exit(1)
6263

6364
print('Success!')
6465
time.sleep(args.sleep) # Give a few seconds for the job to become queued.
@@ -67,10 +68,16 @@ def main():
6768
workflows = firebase_github.list_workflows(args.token, args.workflow, args.branch)
6869
run_id = 0
6970
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
7076
for workflow in workflows['workflow_runs']:
7177
# 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.
7379
if (workflow['status'] in ('queued', 'in_progress') and
80+
(workflow['head_sha'] == branch_sha or not branch_sha) and
7481
workflow['head_branch'] == args.branch):
7582
run_id = workflow['id']
7683
break
@@ -79,8 +86,8 @@ def main():
7986
workflow_url = 'https://github.com/firebase/firebase-unity-sdk/actions/runs/%s' % (run_id)
8087
else:
8188
# 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,
8491
urllib.parse.quote('event:workflow_dispatch', safe=''),
8592
urllib.parse.quote('branch:'+args.branch, safe=''))
8693
print('%sStarted workflow %s: %s' % ('::warning ::' if args.in_github_action else '',

0 commit comments

Comments
 (0)