Home > other >  Configuration of GitHub Actions: Avoid running CI twice when merging PR into main
Configuration of GitHub Actions: Avoid running CI twice when merging PR into main

Time:02-11

I am using GitHub a ctions to manage my CI and the following events trigger my workflow:

on:
  pull_request:
    branches: main
  push:
    branches: main

I observed the following "problem": When I create a PR, the CI is run. If the test passes and I merge it into main, the tests are run again (which I don' t want in specific cases). How can I setup my workflow such that the CI is not triggered when merging a PR, where the CI already passed for the PR?

Thanks in advance!

CodePudding user response:

You might consider an if conditional in your case:

jobs.<job_id>.if

You can use the jobs.<job_id>.if conditional to prevent a job from running unless a condition is met. You can use any supported context and expression to create a conditional.

When you use expressions in an if conditional, you may omit the expression syntax (${{ }}) because GitHub automatically evaluates the if conditional as an expression. For more information, see "Expressions".

jobs:
  build:
    if: github.event.pull_request.merged == 'false'
    . . .

That would still trigger the workflow, but it can skip the jobs in the workflow.

Alternatives exist in this thread, which deals with the opposite requirements ("Trigger workflow only on pull request merge"): it can be adapted to do what you need (not trigger a workflow, or at least do the same job twice, on PR merge)

  • Related