How to make a combined action run on either PR or Push event?

I need a workflow, where I want action to run on each PR against master. And I also want to run that same action when a push (direct commit not PR merge commit) happens in master.

Current configurations looks like this:
image

The Problem:
It makes those actions run twice i.e

  1. When PR is being created against master (that makes sense as I’m listening to on PR event)
  2. When that PR gets merged into master (I don’t want to re run the action this time as It’s already in passing state from PR)

Expected Behavior:
I’m expecting action to not run twice, I want to run the action like this:

  1. When a PR gets opened against master, run the action.
  2. When a direct commit happens to master (again not on the PR merge commit as it will make the action to run twice).

@iamfotx,

When a commit is pushed from a merged PR, the automatically generated the head commit message generally follows the format “Merge pull request #<pr_number> from /<source_branch>\n\n<pr_title>”.
For example:

You can use the expression “github.event.head_commit.message” to get the head commit message from the github context.
You can use the following if conditional to skip all the jobs in the workflow when the push is from the PR merged.

if: ${{ github.event_name == 'pull_request' || (github.event_name == 'push' && !contains( github.event.head_commit.message, 'Merge pull request')) }}

A simple demo:

on:
  push:
    branches:
      - master
  pull_request:
    branches:
      - master

jobs:
  job1:
    if: ${{ github.event_name == 'pull_request' || (github.event_name == 'push' && !contains( github.event.head_commit.message, 'Merge pull request')) }}
    . . .

  job2:
    if: ${{ github.event_name == 'pull_request' || (github.event_name == 'push' && !contains( github.event.head_commit.message, 'Merge pull request')) }}
    . . .

  jobN:
    if: ${{ github.event_name == 'pull_request' || (github.event_name == 'push' && !contains( github.event.head_commit.message, 'Merge pull request')) }}
    . . .
1 Like

Thank you so much for the reply! It worked like charm.

But there’s a problem with git checks:

This is how my commit looks in an Active PR…

When I merge this PR into master:
image

I’ve lost this green status check mark. And I think when using circleci/codeship, we didn’t to re-run the workflow on the master branch again rather it picks the status of the PR.

The second screenshot doesn’t have a commit ID, but I assume it’s showing the merge commit? If so, that’s exactly why there is no check mark: The workflow didn’t run for the merge commit. When you look at the commit history the original commit should still have the check mark.

2 Likes

@iamfotx,

The green hook icon (:ballot_box_with_check:) under a commit means all the checks (include jobs in the workflows) run on this commit ref have passed.

If some checks were not successful, a red cross icon (:negative_squared_cross_mark:) is displayed.

As @airtower-luna mentioned, because you have skipped all the checks, there is not checks status displayed under the commit.

@iamfotx,

How are things going?
Are our above explanations helpful to you to understand?
If you have any questions about this topic, feel free to tell us.