How to trigger a single build on either push or pull_request

I’m already using Azure Pipelines for builds. I’m now trying GitHub Actions for builds side by side, so I can compare them.

I subsequently received a PR for one of my projects from dependabot (https://github.com/gitfool/BoardGameGeek.Dungeon/pull/53). Azure Pipelines only triggered one build, as usual and as expected, but GitHub Actions triggered two builds; one for push and one for pull_request:

Is this a bug or do I need to configure the GitHub Actions differently to match Azure Pipelines behaviour? (After merging the pull request there should again be just a single build, but which should not be due to a pull request.)

https://github.com/gitfool/BoardGameGeek.Dungeon/blob/master/.azure-pipelines.yml

https://github.com/gitfool/BoardGameGeek.Dungeon/blob/master/.github/workflows/build.yml

Hello, your workflow is configured with “on: [push, pull_request]” so it gets triggered twice when a commit is pushed to open a pull request. It looks to me like “on: push” would work well for your use case.

The devil in the details is that pull requests should not publish build artifacts, whereas pushes / merges to master should, so I need to be able to differentiate pull requests from pushes with the GITHUB environment variables:

https://github.com/gitfool/BoardGameGeek.Dungeon/pull/53/checks?check_run_id=228755652#step:6:34

https://github.com/gitfool/BoardGameGeek.Dungeon/pull/53/checks?check_run_id=228755704#step:6:34

Azure Pipelines makes this easy and obvious:

https://dev.azure.com/gitfool/BoardGameGeek.Dungeon/_build/results?buildId=304&view=logs&jobId=1c8ce01e-97de-5be9-a863-648419f09741&taskId=09181c4b-3f3c-50e9-b57f-22616eaa56ea&lineStart=134&lineEnd=142&colStart=1&colEnd=1

You can specify the branches that you want builds to run on.  WIth the on: [push, pull_request] setting, you’ll get any pushes into _any_ branch, and pull requests into _any_ branch.

This means that when dependabot creates its branch - in this case,

dependabot/nuget/Microsoft.NET.Test.Sdk-16.3.0 - that your build will run.  It will then run again when it creates the pull request from that branch.

If you only want to run when something has a pull request opened into master, or when something is merged into master, then you can specify this as:

on:
  push:
    branches:
    - master
  pull_request:
    branches:
    - master
11 Likes

I published https://github.com/marketplace/actions/skip-duplicate-actions to solve this problem in an easy way.
You can give it a try if this is still an issue.