admin管理员组

文章数量:1395367

I have a GitHub Actions workflow named update_branches that lives on a github-base branch (the default branch for the repo in GitHub) and periodically updates several other branches including one named github/akpm-mm/mm-stable (clarification: github is in the name of the branch itself, that's not a remote name).

On github/akpm-mm/mm-stable I have a workflow that's defined to run when the branch gets pushed:

❯❯  git checkout origin/github/akpm-mm/mm-stable
❯❯  head .github/workflows/test.yaml
on:
  push:
    branches:
      - github/linus/master
      - github/akpm-mm/mm-stable
      - github/akpm-mm/mm-unstable

The test workflow works when I push to github/akpm-mm/mm-stable myself, but it doesn't run when the branch is pushed by the update_branches workflow.

This discussion on GitHub says that having actions that push trigger other actions is deliberately disabled when the push happens via GITHUB_TOKEN, so I have set up a Personal Access Token (PAT) and configured the update_branches token to use that when pushing:

      - name: Configure git
        run: |
          set -eux # Note - GitHub redacts secrets in logs so -x is not _that_ sketchy

          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github"
          git remote set-url origin https://x-access-token:${{ secrets.PAT_TOKEN }}@github/bjackman/linux.git

      - name: Update branches
        # This will push the brances to `origin`
        run: .github/scripts/update_branches.sh

I confirmed in the logs that update_branches.sh is using the PAT I configured (at least, I see a *** where GitHub redacts the PAT secret). I confirmed that the push happened by fetching the github/akpm-mm/mm-stable locally, I can see a new commit. But, the push workflows didn't trigger.

What am I missing here?

I have a GitHub Actions workflow named update_branches that lives on a github-base branch (the default branch for the repo in GitHub) and periodically updates several other branches including one named github/akpm-mm/mm-stable (clarification: github is in the name of the branch itself, that's not a remote name).

On github/akpm-mm/mm-stable I have a workflow that's defined to run when the branch gets pushed:

❯❯  git checkout origin/github/akpm-mm/mm-stable
❯❯  head .github/workflows/test.yaml
on:
  push:
    branches:
      - github/linus/master
      - github/akpm-mm/mm-stable
      - github/akpm-mm/mm-unstable

The test workflow works when I push to github/akpm-mm/mm-stable myself, but it doesn't run when the branch is pushed by the update_branches workflow.

This discussion on GitHub says that having actions that push trigger other actions is deliberately disabled when the push happens via GITHUB_TOKEN, so I have set up a Personal Access Token (PAT) and configured the update_branches token to use that when pushing:

      - name: Configure git
        run: |
          set -eux # Note - GitHub redacts secrets in logs so -x is not _that_ sketchy

          git config --global user.name "github-actions[bot]"
          git config --global user.email "github-actions[bot]@users.noreply.github"
          git remote set-url origin https://x-access-token:${{ secrets.PAT_TOKEN }}@github/bjackman/linux.git

      - name: Update branches
        # This will push the brances to `origin`
        run: .github/scripts/update_branches.sh

I confirmed in the logs that update_branches.sh is using the PAT I configured (at least, I see a *** where GitHub redacts the PAT secret). I confirmed that the push happened by fetching the github/akpm-mm/mm-stable locally, I can see a new commit. But, the push workflows didn't trigger.

What am I missing here?

Share Improve this question asked Mar 16 at 14:00 BrendanBrendan 2,4211 gold badge23 silver badges36 bronze badges 1
  • I don't know exactly what's wrong with the config in the question, but Szymon's theory is correct. It doesn't seem to be using the PAT correctly for some reason or another. If I use it throughout, including in the clone, it works. (And this makes the config simpler anyway, there was no particular reason to use the basic token for the clone). – Brendan Commented Mar 18 at 14:39
Add a comment  | 

2 Answers 2

Reset to default 2

GitHub not allowing the push event trigger when the push is made by another workflow is unfortunate.

One way I can think of is run the workflow "from inside" the first workflow.

Running a GitHub Workflow, from another workflow

There are two ways that I can think of:

  • Reusable workflows (creating) & (... and calling one) - next section.
  • Workflow Dispatching

I am not going over the Reusable Workflows for simplicity, but it is a nice way to do it ;).

Workflow Dispatching

So! One way to jump this ups could be to "manually" run the workflow you want to run - the test.yaml - right after you finish all the steps/jobs you want to do in the update_branches.yaml.

For us to be able to do this, we need:

  • Add a step in the update_branch workflow that runs the test workflow;
  • Add a workflow_dispatch trigger event to the test workflow - Because running workflows manually triggers this event type and not a push event.
Add step

So what I am suggesting is an added final step in your update_branches workflow, which tells GitHub to run the test workflow. You can do so using the GitHub CLI - gh:

gh workflow run <workflow-name> --ref <branch-name>

In your case, the workflow is test.yaml, and your branch name will depend on wich version/revision of the workflow you want.

If your most up-to-date/the version you want is in the default branch, then:

gh workflow run test.yaml --ref github-base

So, in your workflow, you do something like:

      # other previous steps...

      # your workflow executes the script
      - name: Update branches
        run: .github/scripts/update_branches.sh
+ 
+     - name: run test workflow "manually"
+       run: gh workflow run test.yaml --ref github-base
+       env:
+         GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

Now, you may or may not need to give this new step some additional permissions (a higher permission-ed token). See this official docs page about using github-cli inside a workflow.

Add workflow_dispatch trigger

Now, you need to tell GitHub what do in the event of running the workflow "manually"/through a workflow_dispatch.

In the test workflow, you must specify the trigger type workflow_dispatch, and the inputs (which I am assuming is the branch). SEE: Triggering Workflows - Defining Inputs:

on:
  push:
    # what you already have...
+ workflow_disptach:
+   inputs:
+       branch:
+         description: 'target branch for "testing"'
+         required: true
+         type: string

You may need to change a few things throughout the test workflow. Any context that you are currently using in that workflow, base on the push trigger, will need to be modified to also work with the workflow_dispatch event type.

For example, the "Checkout code" step, when ran by a workflow_dispatch, I am not sure what happens when ran by a workflow_disptach event trigger. According to their Readme it should work since >it only needs the $GITHUB_WORKSPACE Env. variable.

  # ...

     - name: Checkout code
       uses: actions/checkout@v4

  # ---

From what I can see, you don't seem to be using any contextual information inside the workflow when triggered. I may be wrong. What I recommend is testing it, and if there does arise the need to use contextual information about the run, or if what you are currently using is not valid/doesn't exist when triggered by the workflow_dispatch compare what both provide as their payload (another name for context):

  • On Push - events and their payloads
  • On Workflow Dispatch - events and their payloads

Please do hit back if it doesn't work.

You can find detailed examples and explanation of gh workflow run... command in the official docs: https://cli.github/manual/gh_workflow_run.

As a sidenote, as of the time of writing, a Github workflow only runs from a different > branch if you run it manually (for example with the gh CLI tool: gh workflow run workflow-name --ref branch-name) And, for the workflow to be valid, it must exist first in the default branch (even if just a skeleton).

Current solution

at least, I see a *** where GitHub redacts the PAT secret

It will also redact the default GitHub Token given to the action, not only the PAT, hence you might be using the provided one. In a similar problem, this answer tells you to specify your PAT token via checkout action (and that's what is recommended by actions/checkout here):

- name: Checkout Repository
  uses: actions/checkout@v4
  with:
    token: ${{ secrets.GH_CLASSIC_TOKEN }}
  # Other steps if any
- name: Configure git
  run: |
    git config --global user.name "github-actions[bot]"
    git config --global user.email "github-actions[bot]@users.noreply.github"
- name: Update branches
  # This will push the brances to `origin`
  run: .github/scripts/update_branches.sh

本文标签: githubCan39t trigger push workflow when push happens in actionStack Overflow