How to run a workflow only once, even when triggered mutliple times

I have a CI workflow that I want to run on PRs, but also when I’m just pushing to my repo. That way, I can notice issues sooner during development when I have not yet decided to turn it into a PR. The workflow also needs to run when a PR gets merged into master. But when I simply enter

on: [push, pull_request]

it will run twice every time I update a PR that is on a branch that’s already part of the repo.

Either a config that fits my usecase or a way to tell a workflow to run only once given a commit hash would be nice.

The way you have it setup it will always run on a push and when a PR is opened, yes. That would be expected when you configure it like that. If you only want to run it whenever you push a new branch (which is how I interpret your use case), you should listen just to the create event instead, so you’ll end up with:

on: [create, pull_request]

And it will only run when you first push a new branch and when you eventually open up a PR.

2 Likes

That way it won’t be run when it gets merged into master, though. In other words, I want to run the workflow on every push for every branch within my repo, and also for PRs that come from forked repos. But reading the docs you linked for the pull request event, it seems that there is no such configuration and i just might need to change the way I think about these triggers.

In order to trigger things when they are merged into master, you can use the following workflow structure:

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

jobs:
  deploy:
    runs-on: ubuntu-latest

    steps:
      - name: checkout
        if: github.event.pull_request.merged
        uses: actions/checkout@v1
        with:
          ref: master

Note the “if” condition on the step. You will need to add that to every step to make sure it only run whenever the PR was actually merged (and not closed/discarded without merge). There is currently no way to enforce such a condition on all steps in a job yet, but this setup works just fine for my CD workflow (which I also only want to trigger when something is merged into master).

1 Like

We recently shipped support for if at the job level so this workflow should be easier now. 

https://help.github.com/en/articles/workflow-syntax-for-github-actions#jobsjob_idif

3 Likes