Skip to content

Commit

Permalink
Add repository dispatch reusable workflow (#46)
Browse files Browse the repository at this point in the history
Co-authored-by: Nguyen Nhu Viet <[email protected]>
  • Loading branch information
MSevey and Bidon15 authored Apr 26, 2023
1 parent fa412a8 commit d51dd57
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions .github/workflows/reusable_repository_dispatch.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
name: Repository Dispatch

# This workflow is used to call workflows in external repositories and bring the
# result back into the calling repository.
#
# This is ideal for E2E testing when a repository can trigger a dependent
# repository's integration tests in order to check for breaking changes.
#
# The called repository should have a workflow that triggers on `workflow_dispatch`
#
# Example:
#
# name: Repository Dispatch
# on:
# workflow_dispatch:
# inputs:
# distinct_id:
# key:
#
# jobs:
# test:
# runs-on: ubuntu-latest
# steps:
# - name: echo distinct ID ${{ github.event.inputs.distinct_id }}
# run: |
# echo ${{ github.event.inputs.distinct_id }}
# echo 'my input key ${{ inputs.key }}'
#
#
# At a minimum a `distinct_id` input is required in the called workflow so that
# this workflow can find the workflow run in the API since the `distinct_id` is
# then printed in the run name. This is just needed in one step, so as a
# template, the echo statement can be used for debugging purposes.
#
# This example also shows how you can access addition inputs via the
# `workflow_inputs` variable. These `workflow_inputs` should correspond with
# `workflow_dispatch` inputs. In this example, the `workflow_inputs` would have
# been '{"key": "my_value"}'

on:
workflow_call:
inputs:
owner:
description: "Repository owner for the target repository"
type: string
required: true
repo:
description: "Repository being targeted"
type: string
required: true
ref:
description: "The branch of the target repository that should be targeted, i.e. main or refs/heads/main"
type: string
required: false
default: main
workflow:
description: "The workflow in the target repository that should be triggered"
type: string
required: true
workflow_inputs:
description: "A key value map of custom inputs, i.e. `{'my_key':'my_value'}`"
type: string
required: false
default: ""
workflow_timeout_seconds:
description: "Timeout for called workflow"
type: number
required: false
default: 300

jobs:
triggerMyEvent:
runs-on: ubuntu-latest
steps:
- name: Dispatch an action and get the run ID
uses: codex-/return-dispatch@v1
id: return_dispatch
with:
token: ${{ secrets.PAT_REPO_DISPATCH }} # this is an org level secret
ref: ${{inputs.repo}}
repo: ${{inputs.repo}}
owner: ${{inputs.owner}}
workflow: ${{inputs.workflow}}
workflow_inputs: ${{ inputs.workflow_inputs }} # Optional
workflow_timeout_seconds: ${{inputs.workflow_timeout_seconds}} # Default: 300

# I added this as I observed the API response sometimes being empty as the
# API route used to get the conclusion is different than the API route
# used in the previous action
- name: Delay for api to update
run: sleep 2

- name: Get Conclusion
uses: octokit/[email protected]
id: get_run_conclusion
with:
route: GET /repos/{owner}/{repo}/actions/runs/{run_id}
owner: ${{inputs.owner}}
repo: ${{inputs.repo}}
run_id: ${{steps.return_dispatch.outputs.run_id}}
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Fail if not successful
if: ${{ fromJSON(steps.get_run_conclusion.outputs.data).conclusion != 'success' }}
run: exit 1
# Alternative if descriptive exit code is helpful
# uses: actions/github-script@v3
# with:
# script: |
# core.setFailed('My detailed error response')

0 comments on commit d51dd57

Please sign in to comment.