Skip to content

Commit 2edc1f5

Browse files
committed
Added ability to trigger target workflow via API or workflow run
1 parent cb2f9c2 commit 2edc1f5

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

.github/workflows/trigger-package-and-publish.yml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ on:
55
branches:
66
- "att/**"
77
workflow_dispatch:
8+
inputs:
9+
trigger-type:
10+
description: "How to trigger the target workflow"
11+
required: true
12+
type: choice
13+
options:
14+
- api
15+
- workflow-run
816

917
jobs:
1018
trigger:
@@ -43,7 +51,9 @@ jobs:
4351
echo "${{ secrets.TARGET_REPOSITORY_ACCESS_TOKEN }}" | gh auth login --with-token
4452
gh auth status
4553
54+
# Triggering workflows via the API is meant to be used by external systems.
4655
- name: Trigger Workflow in Target Repository (api)
56+
if: "${{ github.event.inputs.trigger-type == 'api'}}"
4757
run: |
4858
event_data=$(cat <<EOM | yq -o json -I 0 '.'
4959
event_type: "${{ env.event_type }}"
@@ -71,3 +81,14 @@ jobs:
7181
--input - \
7282
/repos/${{ env.target_owner }}/${{ env.target_name }}/dispatches \
7383
;
84+
85+
# Triggering workflows via workflow execution is preferred when within the GitHub ecosystem.
86+
- name: Trigger Workflow in Target Repository (workflow run)
87+
if: "${{ github.event.inputs.trigger-type == 'workflow-run' }}"
88+
run: |
89+
gh workflow run \
90+
${{ env.target_workflow }} \
91+
--repo ${{ env.target_owner }}/${{ env.target_name }} \
92+
--ref main \
93+
--field branch-name=${{ github.ref_name }} \
94+
;

0 commit comments

Comments
 (0)