0

I have an action configured to run an automated test suite on all pull requests to master. However, I have a separate release branch called release/master that should not trigger the CI, as there's no point in testing it as it only bumps the package version.

The docs don't seem to cover this topic at all though. Is there anyway to filter by target AND source branch at the same time?

rschristian
  • 1,730
  • 1
  • 6
  • 15
  • 1
    are you merging _from_ `release/master` into `master`, and want to not trigger the workflow in that case? Would predicating the job on an `if` be sufficient, or you're only wondering about workflow initialisation? – Skenvy Jul 01 '23 at 12:36
  • The branch is the trigger, the filtering about the other branch you do in your workflow. What @Skenvy wrote. This is why you don't find it documented on the trigger docs. Perhaps a feature request, but I doubt it'll get traction. – hakre Jul 01 '23 at 16:28
  • Yes, that's what I'd like. I tried a few forms of `if`, but couldn't make it work, if it's meant to be. I don't see why better trigger logic is "something unlikely to get traction", seems like a pretty basic desire. Splitting up your trigger throughout your script to bail on certain conditions is a pretty poor way to do things. – rschristian Jul 01 '23 at 22:57
  • 1
    Have you checked these answers, they seem very relevant https://stackoverflow.com/questions/58139406/only-run-job-on-specific-branch-with-github-actions – Dimo Markov Jul 03 '23 at 15:38
  • It's a hacky workaround, I suppose that could work for others but it's not suitable for me. Not worth splitting up the filtering conditions and making the entire workflow less clear. – rschristian Jul 03 '23 at 23:17

2 Answers2

0

There's currently no way to specify a condition on the head branch name at the workflow initialisation (on) level. Both pull_request and pull_request_target use the base branch (the one being merged to) as the branch name checked against in the on.<pull_request|pull_request_target>.<branches|branches-ignore>, even though the ref for the pull_request_target is the head branch and not the base branch.

The docs for the pull_request event suggest that the only available way to limit on the head branch is to if the job.

To run a job based on the pull request's head branch name (as opposed to the pull request's base branch name), use the github.head_ref context in a conditional. For example, this workflow will run whenever a pull request is opened, but the run_if job will only execute if the head of the pull request is a branch whose name starts with releases/:

on:
  pull_request:
    types:
      - opened
jobs:
  run_if:
    if:  startsWith(github.head_ref, 'releases/')
    runs-on: ubuntu-latest
    steps:
      - run: echo "The head of this PR starts with 'releases/'"

In your case you would have to if every job in the workflow to run on;

if: github.head_ref != 'release/master'

Or add it to existing if's you already have;

if: >-
  ${{ github.head_ref != 'release/master' && (
  ...) }}
Skenvy
  • 724
  • 1
  • 4
  • 15
-1

You can use "paths" filter for workaround, like

on:
  pull_request:
    branches:
      - 'master'
    paths:
      - '**.js'

Based on https://docs.github.com/en/actions/using-workflows/events-that-trigger-workflows#pull_request. So workflow trigger will be 'target branch is 'master' AND .js files were modified.

If 'it only bumps the package version' modifies only packages.json, workflow is not trigged.

  • 1
    How is this supposed to help? – rschristian Jul 03 '23 at 23:15
  • @rschristian - updated my answer. – Vitaly Karasik DevOps Jul 04 '23 at 04:03
  • Yeah that's not relevant. This has nothing to do with `.js` files, I'm not sure where in the world you're getting that from? – rschristian Jul 04 '23 at 04:16
  • 1
    A similar answer to this could be tangentially relevant, if your set up included a specific file that only specified the version (for example, as is common in ruby `version.rb` or python `__version__.py`); rather than `if` on not being the `release/master` branch, `paths-ignore`-ing a version file would mean it wouldn't trigger if only that file was changed. – Skenvy Jul 05 '23 at 01:43