diff --git a/.github/actions/repo-dispatch/action.yml b/.github/actions/repo-dispatch/action.yml new file mode 100644 index 0000000..9b8536d --- /dev/null +++ b/.github/actions/repo-dispatch/action.yml @@ -0,0 +1,47 @@ +name: repository-dispatch +description: creates repository_dispatch event in target repository +inputs: + secret-token: # beware: mask is applied below + description: fine-grained personal access token with content write permission for the target repo + required: true + type: string + target-repo-owner: + description: target repository owner (as in /) + required: true + type: string + target-repo-name: + description: target repository name (as in /) + required: true + type: string + event-type: + # https://docs.github.com/en/webhooks/webhook-events-and-payloads#repository_dispatch + description: custom event type string (e.g. for filtering in on.repository_dispatch.types) + required: true + type: string + client-payload: + description: client_payload in repository_dispatch event (a JSON object, as in github.event.client_payload) + required: true + type: string + default: '{}' + +runs: + using: composite + steps: + - # https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#masking-a-value-in-a-log + name: mask secret token + run: echo "::add-mask::${{ inputs.secret-token }}" + shell: bash + - name: post to github api dispatches endpoint + # https://docs.github.com/en/rest/repos/repos?apiVersion=2026-03-10#create-a-repository-dispatch-event + # https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication + run: | + curl --location \ + --verbose \ + --fail-with-body \ + --request POST \ + --header "Accept: application/vnd.github+json" \ + --header "Authorization: Bearer ${{ inputs.secret-token }}" \ + --header "X-GitHub-Api-Version: 2026-03-10" \ + https://api.github.com/repos/${{ inputs.target-repo-owner }}/${{ inputs.target-repo-name }}/dispatches \ + --data '{"event_type":"${{ inputs.event-type }}","client_payload":${{ inputs.client-payload }}}' + shell: bash diff --git a/.github/workflows/test-repo-dispatch-listener.yml b/.github/workflows/test-repo-dispatch-listener.yml new file mode 100644 index 0000000..6e9b13f --- /dev/null +++ b/.github/workflows/test-repo-dispatch-listener.yml @@ -0,0 +1,13 @@ +name: test the receiving end of the repo-dispatch action + +on: + repository_dispatch: + types: [my-custom-event-type] + +jobs: + test: + runs-on: ubuntu-latest + steps: + - run: | + echo "action: ${{ github.event.action }}" + echo "payload: ${{ github.event.client_payload.my-key }}" diff --git a/.github/workflows/test-repo-dispatch.yml b/.github/workflows/test-repo-dispatch.yml new file mode 100644 index 0000000..4045286 --- /dev/null +++ b/.github/workflows/test-repo-dispatch.yml @@ -0,0 +1,28 @@ +# This workflow tests the reusable repo-dispatch action + +name: test repo-dispatch action + +on: + push: + branches: + - main + pull_request: + workflow_dispatch: + +jobs: + dispatch: + runs-on: ubuntu-latest + steps: + - name: checkout in order to use local action + uses: actions/checkout@v7 + - name: test the repo-dispatch action (and usage example) + uses: ./.github/actions/repo-dispatch + with: + secret-token: ${{ secrets.personal_access_token }} + target-repo-name: ${{ github.event.repository.name }} + target-repo-owner: ${{ github.repository_owner }} + event-type: my-custom-event-type + client-payload: | + { + "my-key": "my-value" + }