Pull requests trigger push workflow

Viewed 868

In a GitHub repository, I have two separate workflows for GitHub Actions:

github/workflows/pr.yml to just build and test

name: Pull request workflow

on: pull_request

and github/workflows/push.yml to build and test and deploy

name: Push workflow

on: push

Creating a pull request triggers both of these workflows.

Is it not possible to separate these or what am I missing here?

3 Answers

If you are only deploying certain branches, limit the Push workflow the following way:

name: Push workflow
on:
  push:
    branches:
    - master

Another option is to exclude branches

on:
  push:
    # Sequence of patterns matched against refs/heads
    branches-ignore:
      # Push events to branches matching refs/heads/mona/octocat
      - 'mona/octocat'
      # Push events to branches matching refs/heads/releases/beta/3-alpha
      - 'releases/**-alpha'

See https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#onpushpull_requestbranchestags for more examples

You can use these lines in your job to avoid internal pull requests (which also triggering push) to trigger your workflow, while allowing external pull requests to trigger your workflow.

    # a push event from the origin repo, or a PR from external repo
    if: ${{ github.event_name == 'push' || github.event.pull_request.head.repo.full_name != 'your/full_repo_name' }}

Note though, due to security design reason, the external pull request will only trigger workflow that's defined in the default branch of the upstream repository.

Reference:

For that, you might need to define the type of trigger in PR as well. enter image description here

example code:

on:
  pull_request:
    types:
      - closed
    branches:
      - master

I hope this helps.

Related