Search code examples
githubgithub-actions

In GitHub Actions, how can I trigger on push but only if a PR is active?


Say I have 2 branches, a feature branch and a develop branch.

There's no SLA on feature branch normally, meaning I can push broken code up to it all day long and no CI build should be triggered.

Then I open a PR into develop. I trigger a CI build action on pull_request: created. Let's say this build fails. By default I can't merge the PR, which is correct.

Now I want to push edits to feature branch to update the PR. I want these pushes to trigger a CI build (because we're now working inside an open PR). I don't want to allow the PR to proceed/be merged until these push-CIs pass.

How can I do that in GitHub Actions? I tried on pull_request: edited but that didn't work for me.

I'm looking for the functional equivalent of:

on:
  push:
    if: inside_open_pr

Solution

  • Use the event pull_request and don't specify activities or specify the activity synchronize:

    on:
      pull_request:
    

    Note: By default, a workflow only runs when a pull_request's activity type is opened, synchronize, or reopened. To trigger workflows for more activity types, use the types keyword.

    source

    if you push a new commit to the HEAD ref of a pull request then this “synchronize” event will be triggered. This is because the system is syncing the pull requests with the latest changes. This will NOT trigger if the base ref is updated.

    source

    If you also need to react to a change of the base branch, then you have to add the activity edited.

    source