Rohan Singh
Rohan Singh

Reputation: 663

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?

Upvotes: 16

Views: 53072

Answers (2)

user22171882
user22171882

Reputation: 3

If anyone else stumbles upon this, it's actually git commit that is throwing an error. The '||' catches the error and then just prints to the log so the action doesn't even get to git push.

  - name: Scrape webpage and download file
    run: | 
      python ./python/scraper.py
      git config user.name github-actions
      git config user.email [email protected]
      git commit -am "Auto Generated" || echo "There is nothing to commit"
      git push

Upvotes: -4

riQQ
riQQ

Reputation: 12901

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

Upvotes: 19

Related Questions