diff --git a/.github/actions/repo-dispatch/action.yml b/.github/actions/repo-dispatch/action.yml new file mode 100644 index 0000000..c879a06 --- /dev/null +++ b/.github/actions/repo-dispatch/action.yml @@ -0,0 +1,46 @@ +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: action in repository_dispatch event (as in github.event.action) + 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=2022-11-28#create-a-repository-dispatch-event + # https://docs.github.com/en/actions/security-for-github-actions/security-guides/automatic-token-authentication + run: | + curl --location \ + --fail-with-body \ + --request POST \ + --header "Accept: application/vnd.github+json" \ + --header "Authorization: Bearer ${{ inputs.secret-token }}" \ + --header "X-GitHub-Api-Version: 2022-11-28" \ + 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..c7e9818 --- /dev/null +++ b/.github/workflows/test-repo-dispatch-listener.yml @@ -0,0 +1,12 @@ +name: test the receiving end of the repo-dispatch action + +on: + repository_dispatch: + +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..c2a3a61 --- /dev/null +++ b/.github/workflows/test-repo-dispatch.yml @@ -0,0 +1,25 @@ +# 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@v4 + - name: test action + 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-event + client-payload: '{"my-key": "my-value"}'