github-actionsgithub-cli

gh workflow run not working for particular branch


I've two branch in my repo i.e main and kaju1,

On main branch (default), there are two workflow defined push.yml and test.yml.

On kaju1 branch, there are three workflow defined push.yml, test.yml, and pr.yml.

I want to trigger pr.yml i.e on kaju1 branch using github cli.

I tried using this code:

gh workflow run --repo username/repo-name --ref kaju1 pr.yml

But this is returning me the following error:

HTTP 404: Not Found (https://api.github.com/repos/username/repo-name/actions/workflows/pr.yml)

Solution

  • I did some research and found out that workflow only work with two conditions

    1. It is present in main (default) branch.
    2. It is present in some other branch but has already run before.

    So what I came up with, I'll keep it in other branch (that I want), then I'll add on:push trigger on it for that it get triggered on every push to that branch then I'll use if/else to distinguish between on:push and on:workflow_dispach

    on:
      push:
        branches:
          - "other-branch"
    
      workflow_dispatch:
        inputs:
          greeting:
            description: 'Greetings'
            required: true
            default: 'Hello, World'
            type: string
    
    jobs:
      job1:
        runs-on: ubuntu-latest
    
        if: ${{ github.event_name == 'workflow_dispatch' }}
    
        steps:
          - uses: actions/checkout@v3
    

    now on every push It'll at least get registered in github/actionss, then

     gh workflow run --repo username/repo-name --ref other-branch pr.yml