GithubHelp home page GithubHelp logo

codex- / return-dispatch Goto Github PK

View Code? Open in Web Editor NEW
36.0 3.0 10.0 4.01 MB

⚙️ Dispatch an action to a foreign repository and output the newly created run ID.

License: MIT License

TypeScript 96.51% JavaScript 3.49%
actions dispatch github-actions workflow

return-dispatch's Introduction

GitHub Action: return-dispatch

GitHub Workflow Status code style: prettier codecov GitHub Marketplace

Dispatch an action to a foreign repository and output the newly created run ID.

This Action exists as a workaround for the issue where dispatching an action to foreign repository does not return any kind of identifier.

Usage

Ensure you have configured your remote action correctly, see below for an example.

Dispatching Repository Action

steps:
  - name: Dispatch an action and get the run ID and URL
    uses: codex-/return-dispatch@v1
    id: return_dispatch
    with:
      token: ${{ secrets.TOKEN }} # Note this is NOT GITHUB_TOKEN but a PAT
      ref: target_branch # or refs/heads/target_branch
      repo: repository-name
      owner: repository-owner
      workflow: automation-test.yml
      workflow_inputs: '{ "some_input": "value" }' # Optional
      workflow_timeout_seconds: 120 # Default: 300

  - name: Use the output run ID and URL
    run: |
      echo ${{steps.return_dispatch.outputs.run_id}}
      echo ${{steps.return_dispatch.outputs.run_url}}

Receiving Repository Action

In the earliest possible stage for the Action, add the input into the name.

As every step needs a uses or run, simply echo the ID or similar to satisfy this requirement.

name: action-test
on:
  workflow_dispatch:
    inputs:
      distinct_id:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: echo distinct ID ${{ github.event.inputs.distinct_id }}
        run: echo ${{ github.event.inputs.distinct_id }}

Token

To be able to use dispatch we need to use a token which has repo permissions. GITHUB_TOKEN currently does not allow adding permissions for repo level permissions currently so a Personal Access Token (PAT) must be used.

Permissions Required

The permissions required for this action to function correctly are:

  • repo scope
    • You may get away with simply having repo:public_repo
    • repo is definitely needed if the repository is private.
  • actions:read
  • actions:write

APIs Used

For the sake of transparency please note that this action uses the following API calls:

For more information please see api.ts.

Where does this help?

If you have an action in a repository that dispatches an action on a foreign repository currently with Github API there is no way to know what the foreign run you've just dispatched is. Identifying this can be cumbersome and tricky.

The consequence of not being provided with something to identify the run is that you cannot easily wait for this run or poll the run for it's completion status (success, failure, etc).

Flow

┌─────────────────┐
│                 │
│ Dispatch Action │
│                 │
│ with unique ID  │
│                 │
└───────┬─────────┘
        │
        │
        ▼                          ┌───────────────┐
┌────────────────┐                 │               │
│                │                 │ Request steps │
│ Request top 10 ├────────────────►│               │
│                │                 │ for each run  │
│ workflow runs  │                 │               │
│                │◄────────────────┤ and search    │
└───────┬────────┘     Retry       │               │
        │                          └───────┬───────┘
        │                                  │
Timeout │                                  │
        │                                  │
        ▼                                  ▼
     ┌──────┐                      ┌───────────────┐
     │ Fail │                      │ Output run ID │
     └──────┘                      └───────────────┘

return-dispatch's People

Contributors

codex- avatar dependabot[bot] avatar nickdub avatar pmrotule avatar renovate[bot] avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

return-dispatch's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • chore(deps): update all non-major dependencies (@types/node, @typescript-eslint/eslint-plugin, @typescript-eslint/parser, changelogithub, esbuild, prettier, typescript)
  • chore(deps): update vitest monorepo to v2 (major) (@vitest/coverage-v8, vitest)
  • fix(deps): update dependency uuid to v10 (uuid, @types/uuid)
  • chore(deps): lock file maintenance

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/action.yml
  • actions/checkout v4
.github/workflows/codeql-analysis.yml
  • actions/checkout v4
  • github/codeql-action v3
  • github/codeql-action v3
.github/workflows/dispatch.yml
.github/workflows/release.yml
  • actions/checkout v4
  • actions/setup-node v4
.github/workflows/test.yml
  • actions/checkout v4
  • actions/setup-node v4
  • actions/checkout v4
  • actions/setup-node v4
  • codecov/codecov-action v4
npm
package.json
  • @actions/core ^1.10.1
  • @actions/github ^6.0.0
  • uuid ^9.0.1
  • @types/node ^20.14.9
  • @types/uuid ^9.0.8
  • @typescript-eslint/eslint-plugin ^7.14.1
  • @typescript-eslint/parser ^7.14.1
  • @vitest/coverage-v8 ^1.6.0
  • chalk ^5.3.0
  • changelogithub ^0.13.7
  • esbuild ^0.21.5
  • eslint ^8.57.0
  • eslint-config-airbnb-typescript ^18.0.0
  • eslint-config-prettier ^9.1.0
  • eslint-plugin-github ^5.0.1
  • eslint-plugin-import ^2.29.1
  • prettier 3.3.2
  • typescript ^5.5.2
  • vitest ^1.6.0

  • Check this box to trigger a request for Renovate to run again on this repository

Only looks at the first 30 workflows found in the repository

By default, calling octokit.rest.actions.listRepoWorkflows without per_page will return the first 30 workflows found in the repository. It is an issue in our case since we have a long list of workflows to trigger all kind of deployments and the one I wanted to use was not found by the return-dispatch action because it was only in the second page of results.

Error: getWorkflowId: An unexpected error has occurred: Unable to find ID for Workflow

Fetching Workflow ID for regression-test.yaml.yaml...
Error: getWorkflowId: An unexpected error has occurred: Unable to find ID for Workflow: regression-test.yaml.yaml
Error: Failed to complete: Unable to find ID for Workflow: regression-test.yaml.yaml
Warning: Does the token have the correct permissions?
Error: Unable to find ID for Workflow: regression-test.yaml.yaml

Please I need help. I am using this action in 2 jobs in one workflow. the first job in the workflow works but this error in the second one.
I wonder what could be wrong because we are using the same git token

Here is what the workflow looks like

  ## QA 1 TEST
  QA-1-Test:
    runs-on: ubuntu-latest
    needs: onejobabove
    steps:
      - name: Get Token
        id: get_token
        uses: tibdex/github-app-token@v1
        with:
          app_id: ${{ secrets.APP}}
          private_key: ${{ secrets.APP}}
      - name: Wait for deployment to complete
        run : sleep 180
      - name: QA 1 Test
        uses: codex-/[email protected]
        id: qa_1_test
        with:
          token: ${{ steps.get_token.outputs.token }}
          ref: refs/heads/test-ghaction
          repo: repo
          owner: owner
          workflow: bat-1-test.yaml
          workflow_inputs: '{"test_environment":"qa"}'
          workflow_timeout_seconds: 300 # Default: 300
      - name: Await Run ID ${{ steps.qa_bat_test.outputs.run_id }}
        uses: Codex-/[email protected]
        with:
          token: ${{ steps.get_token.outputs.token }}
          repo: repo
          owner: owner
          run_id: ${{ steps.qa_1_test.outputs.run_id }}
          run_timeout_seconds: 900 # Optional
          poll_interval_ms: 5000 # Optional

  ## QA TEST 2
  QA-2-Test:
    continue-on-error: true
    runs-on: ubuntu-latest
    needs: onejobabove
    steps:
      - name: Get Token
        id: get_token
        uses: tibdex/github-app-token@v1
        with:
          app_id: ${{ secrets.APP}}
          private_key: ${{ secrets.APP}}
      - name: QA 2 Test
        uses: codex-/[email protected]
        id: qa_2_test
        with:
          token: ${{ steps.get_token.outputs.token }}
          ref: refs/heads/test-ghaction
          repo: repo
          owner: owner
          workflow: regression-test.yaml
          workflow_inputs: '{"test_environment":"qa"}'
          workflow_timeout_seconds: 300 # Default: 300
      - name: Await Run ID ${{ steps.qa_regression_test.outputs.run_id }}
        uses: Codex-/[email protected]
        with:
          token: ${{ steps.get_token.outputs.token }}
          repo: repo
          owner: owner
          run_id: ${{ steps.qa_2_test.outputs.run_id }}
          run_timeout_seconds: 1800 # Optional
          poll_interval_ms: 5000 # Optional

Hide warnings like "failed to get branch for ref: main"

These warnings show up on the summary page of a build. Can they be hidden from the summary page? People get scared when they see them.

Fetched Workflow ID: 88382506
Successfully dispatched workflow:
  Repository: bay1inc/devops
  Branch: main
  Workflow ID: prbox_update_routing.yaml
  Workflow Inputs: {"services":"non-pci","tenants":"live"}
  Distinct ID: e2d6a12c-5b91-453c-8c86-9337dce03389
Attempt to extract run ID from steps...
Warning: failed to get branch for ref: main, please raise an issue with this git ref.
Exhausted searching IDs in known runs, attempt 1...
Warning: failed to get branch for ref: main, please raise an issue with this git ref.
Exhausted searching IDs in known runs, attempt 2...
Warning: failed to get branch for ref: main, please raise an issue with this git ref.
Successfully identified remote Run:
  Run ID: 8354122386

Optimised Run ID Extraction Process

It is possible to dynamically name a GitHub Workflow Run using the run-name attribute. In the workflow that is to be triggered by the return-dispatch action, it is possible to include the dispatch_id within the run name [Reference]

name: example-workflow
run-name: Example Workflow [${{ inputs.distinct_id }}]

on:
  workflow_dispatch:
    inputs:
      distinct_id:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - name: echo
        run: echo "Hello World"

It is then possible to extract the run_id by doing a String.contains(distinct_id) search on the name attribute of the response returned by /repos/{owner}/{repo}/actions/workflows/{workflow_id}/runs [Reference]. The Run ID is available from the corresponding id attribute.

Benefits

  • Removes the need for a separate query to the /repos/{owner}/{repo}/actions/runs/{run_id}/jobs endpoint to try extract the distinct_id from the Jobs.
  • I believe with these changes, it is possible to extract the Run ID while the workflow is in queue to be picked up by a GitHub Runner. This is from my assumption, that the name of the workflow is available before the initial Job is picked up by a GitHub Runner. This would reduce the time taken for this action to successfully fetch a Run ID

Challenges

  • A hard change to the current process of retrieving the Run ID would break backwards compatibility with existing workflows that depend on this action assuming that the distinct_id is somewhere within the Jobs. Perhaps a backwards compatibility layer can be introduced to check the distinct_id from the Jobs if it can't be found in the Workflow Run name.
    • It is computationally negligible to do a String.contains() search on 10 strings, so I don't see an issue with this approach

Make distinct_id configurable

I love this action and it's great to have 2 actions to separate 2 use cases.
I just want to make a suggestion.
The name distinct_id can have a meaning in a context but not in another one. I wanted to know if it could be possible to make the name of the input send to the other workflow configurable with distinct_id as default value. In that way we could name it for example workflow-correlation-id for example and in our repo we would be able to create an action that uses this one to make abstraction of what's inside without to have the kind of corruption of fixed distinct_id input in all the other workflows.
I can make a PR for this if you want.
Thank you

Timeout getting the workflow id

Hi,

I'm trying to dispatch a workflow from one repo and wait for getting the status that workflow. When I tried to use return-dispatch, I'm getting the below error even though the PAT has right access, let me know if you need any more details.

image

Exhausted searching IDs in known runs

Hi,

From time to time, we would see this error "Exhausted searching IDs in known runs". See the logs below. I bet this is caused by github API not finding the relevant run. That said, is there anything we can do to mitigate this problem? Thanks!

Mon, 07 Aug 2023 16:14:00 GMT Fetching Workflow ID for smokes-cron.yml...
Mon, 07 Aug 2023 16:14:00 GMT Fetched Workflow ID: 7198060
Mon, 07 Aug 2023 16:14:01 GMT Successfully dispatched workflow:
Mon, 07 Aug 2023 16:14:01 GMT   Repository: bay1inc/api-platform
Mon, 07 Aug 2023 16:14:01 GMT   Branch: refs/heads/main
Mon, 07 Aug 2023 16:14:01 GMT   Workflow ID: smokes-cron.yml
Mon, 07 Aug 2023 16:14:01 GMT   Workflow Inputs: {"envs":"staging-test,staging-live"}
Mon, 07 Aug 2023 16:14:01 GMT   Distinct ID: 29fc4ecb-c9d5-44d8-9216-aa22fc11ff89
Mon, 07 Aug 2023 16:14:01 GMT Attempt to extract run ID from steps...
Mon, 07 Aug 2023 16:14:03 GMT Exhausted searching IDs in known runs, attempt 1...
Mon, 07 Aug 2023 16:14:11 GMT Exhausted searching IDs in known runs, attempt 2...
Mon, 07 Aug 2023 16:14:18 GMT Exhausted searching IDs in known runs, attempt 3...
Mon, 07 Aug 2023 16:14:26 GMT Exhausted searching IDs in known runs, attempt 4...
Mon, 07 Aug 2023 16:14:33 GMT Exhausted searching IDs in known runs, attempt 5...
Mon, 07 Aug 2023 16:14:41 GMT Exhausted searching IDs in known runs, attempt 6...
Mon, 07 Aug 2023 16:14:48 GMT Exhausted searching IDs in known runs, attempt 7...
Mon, 07 Aug 2023 16:14:56 GMT Exhausted searching IDs in known runs, attempt 8...
Mon, 07 Aug 2023 16:15:03 GMT Exhausted searching IDs in known runs, attempt 9...
Mon, 07 Aug 2023 16:15:11 GMT Exhausted searching IDs in known runs, attempt 10...
Mon, 07 Aug 2023 16:15:18 GMT Exhausted searching IDs in known runs, attempt 11...
Mon, 07 Aug 2023 16:15:26 GMT Exhausted searching IDs in known runs, attempt 12...
Mon, 07 Aug 2023 16:15:33 GMT Exhausted searching IDs in known runs, attempt 13...
Mon, 07 Aug 2023 16:15:41 GMT Exhausted searching IDs in known runs, attempt 14...
Mon, 07 Aug 2023 16:15:48 GMT Exhausted searching IDs in known runs, attempt 15...
Mon, 07 Aug 2023 16:15:55 GMT Exhausted searching IDs in known runs, attempt 16...
Mon, 07 Aug 2023 16:16:04 GMT Exhausted searching IDs in known runs, attempt 17...
Mon, 07 Aug 2023 16:16:12 GMT Exhausted searching IDs in known runs, attempt 18...
Mon, 07 Aug 2023 16:16:20 GMT Exhausted searching IDs in known runs, attempt 19...
Mon, 07 Aug 2023 16:16:27 GMT Exhausted searching IDs in known runs, attempt 20...
Mon, 07 Aug 2023 16:16:35 GMT Exhausted searching IDs in known runs, attempt 21...
Mon, 07 Aug 2023 16:16:42 GMT Exhausted searching IDs in known runs, attempt 22...
Mon, 07 Aug 2023 16:16:55 GMT Exhausted searching IDs in known runs, attempt 23...
Mon, 07 Aug 2023 16:16:57 GMT Exhausted searching IDs in known runs, attempt 24...
Mon, 07 Aug 2023 16:17:05 GMT Exhausted searching IDs in known runs, attempt 25...
Mon, 07 Aug 2023 16:17:12 GMT Exhausted searching IDs in known runs, attempt 26...
Mon, 07 Aug 2023 16:17:20 GMT Exhausted searching IDs in known runs, attempt 27...
Mon, 07 Aug 2023 16:17:27 GMT Exhausted searching IDs in known runs, attempt 28...
Mon, 07 Aug 2023 16:17:35 GMT Exhausted searching IDs in known runs, attempt 29...
Mon, 07 Aug 2023 16:17:42 GMT Exhausted searching IDs in known runs, attempt 30...
Mon, 07 Aug 2023 16:17:50 GMT Exhausted searching IDs in known runs, attempt 31...
Mon, 07 Aug 2023 16:17:57 GMT Exhausted searching IDs in known runs, attempt 32...
Mon, 07 Aug 2023 16:18:05 GMT Exhausted searching IDs in known runs, attempt 33...
Mon, 07 Aug 2023 16:18:13 GMT Exhausted searching IDs in known runs, attempt 34...
Mon, 07 Aug 2023 16:18:20 GMT Exhausted searching IDs in known runs, attempt 35...
Mon, 07 Aug 2023 16:18:28 GMT Exhausted searching IDs in known runs, attempt 36...
Mon, 07 Aug 2023 16:18:35 GMT Exhausted searching IDs in known runs, attempt 37...
Mon, 07 Aug 2023 16:18:43 GMT Exhausted searching IDs in known runs, attempt 38...
Mon, 07 Aug 2023 16:18:50 GMT Exhausted searching IDs in known runs, attempt 39...
Mon, 07 Aug 2023 16:19:05 GMT Exhausted searching IDs in known runs, attempt 40...
Mon, 07 Aug 2023 16:19:07 GMT Exhausted searching IDs in known runs, attempt 41...
Mon, 07 Aug 2023 16:19:12 GMT Error: Failed to complete: Timeout exceeded while attempting to get Run ID
Mon, 07 Aug 2023 16:19:12 GMT Warning: Does the token have the correct permissions?
Mon, 07 Aug 2023 16:19:12 GMT Error: Timeout exceeded while attempting to get Run ID

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.