Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I trigger a GitHub Actions workflow on push to another branch?

When I push some code to master, one workflow file runs. This file builds the artifacts and pushes the code to another branch production.

Another workflow file, which looks like the following, is set to run when any push happens to production.

name: Deploy

on:
  push:
    branches:
      - production

jobs:

# Do something

  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@master

But this workflow file is never run. I expect that when the workflow file, listening to push event on master, is done, this file should run as the previous file pushes code to production branch. How do I make sure that this happens?

like image 463
Rohan Singh Avatar asked Nov 30 '25 06:11

Rohan Singh


1 Answers

You need to use a personal access token (PAT) for pushing the code in your workflow instead of the default GITHUB_TOKEN:

Note: You cannot trigger new workflow runs using the GITHUB_TOKEN

For example, if a workflow run pushes code using the repository's GITHUB_TOKEN, a new workflow will not run even when the repository contains a workflow configured to run when push events occur.

If you would like to trigger a workflow from a workflow run, you can trigger the event using a personal access token. You'll need to create a personal access token and store it as a secret.

https://docs.github.com/en/free-pro-team@latest/actions/reference/events-that-trigger-workflows#triggering-new-workflows-using-a-personal-access-token

name: Push to master

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    # the checkout action persists the passed credentials by default
    # subsequent git commands will pick them up automatically
    - uses: actions/checkout@v2
      with:
        token: ${{secrets.PAT}}
    - run: |
        # do something
        git push
like image 152
riQQ Avatar answered Dec 02 '25 00:12

riQQ



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!