Home > Net >  GitHub Actions triggering workflow form another workflow
GitHub Actions triggering workflow form another workflow

Time:10-03

I have a Github repository (repo A) with a workflow running inside that repo (working with Terraform file and applying them on AWS). I am trying to trigger that workflow from a different Github repository (repo B), so I created a workflow in repo B, made a checkout to repo A and then tried to trigger the workflow with "gh" CLI.

jobs:
  traffic-split:
    runs-on: ubuntu-latest
    permissions:
      id-token: write
      contents: read
    steps:
    - name: Checkout to repo A
      uses: actions/checkout@master
      with:
        repository: <My_Organization>/<My_Called_Repo>
        token: ${{ secrets.GH_TOKEN }}

    - name: Run Workflow
      run: |
        curl -X POST -H "Content-Type: application/json" -H "Accept: application/vnd.github.v3 json" "https://api.github.com/repos/<My_Organization>/<My_Called_Repo>/.github/workflows/<My_Called_Worflow>/dispatches"

However, it fails with 404 NOT FOUND.

{
  "message": "Not Found",
  "documentation_url": "https://docs.github.com/rest/reference/actions#create-a-workflow-dispatch-event"
}

When running other GH CLI commands like "gh workflow list", etc. it works, but triggering a workflow fails. What am I missing when triggering the workflow?

Thank you!

CodePudding user response:

Try using GitHub action: workflow-dispatch

  - name: Invoke Server Workflow 
    uses: benc-uk/workflow-dispatch@v1
    with:
      workflow: Server build
      repo: r10-server
      token: ${{ secrets.PAT_TOKEN }}
      ref: refs/heads/main

CodePudding user response:

Try using GitHub API Docs: GitHub API

- name: Run Workflow
  run: |
    $uri 
    ('https://api.github.com/repos/{0}/{1}/actions/workflows/{2}/dispatches' 
    -f $(ORGANIZATION), $(REPOSITORY), $(WORKFLOW_NUMBER))
    $Body = @{'ref' = 'main'} | ConvertTo-Json
    $params = @{ContentType='application/json'
    Headers     = @{'authorization'="token $(GITHUB_TOKEN)"
    'accept'='application/vnd.github.everest-preview json'}
    Method='Post'
    URI=$Uri
    Body=$Body
    }
    Invoke-RestMethod @params -verbose
  • Related