I have build forked PRs feature turned on in my project. I have two workflows:
pre-merge: this triggers when a PR is pushed to my project
merge: this triggers when there’s an update to the master branch. like a merge to master
Here’s my workflow:
workflows:
pre-merge:
jobs:
- build
- lint-and-unit-tests:
requires:
- build
merge:
jobs:
- build:
filters:
branches:
only: master
- lint-and-unit-tests:
requires:
- build
filters:
branches:
only: master
- integration-tests:
context: sceptre-core
requires:
- build
filters:
branches:
only: master
The change I want to make is to have the merge workflow to trigger on any changes to any branch but also don’t run the pre-merge workflow when that happens. I’ve read the circleci trigger docs and I still cannot figure out a way to do it. Can someone please help?
Hi @zaro0508 ,
Thank you for the question!
I believe that when pull requests are opened, the text pull/
is contained in the branch name.
We can use this to filter the branches for which jobs are executed upon.
First, we can set the job filtering to the following conditions:
- For the
pre-merge
workflow, filter the jobs to only run if pull/
is included in the branchname
- For the
merge
workflow, filter the jobs to only run if pull/
is not included in the branchname
Since this syntax can get kind of long when specifying it for multiple jobs, I will use yaml aliases to simplify it (I am using negative lookaheads here, but if there are issues with this, then using the ignore
key could replace them):
pre-merge: &pre-merge
filters:
branches:
only: /^(pull\/).*$/
merge: &merge
filters:
branches:
only: /^(?!pull\/).*$/
If we use this in the workflows, it would look something like this:
workflows:
pre-merge:
jobs:
- build:
<<: *pre-merge
- lint:
<<: *pre-merge
merge:
jobs:
- build:
<<: *merge
By having the filter condition for every job, if no jobs are to be run for a workflow, the workflow will not execute.
Another method would be to check for the value of environment variables such as CIRCLE_PULL_REQUEST
. Then you could cancel a workflow based on if it is defined or not (as this variable is only present in jobs triggered by a PR ).
One caveat with this approach is that a workflow will actually be triggered on CircleCI, but it will be immediately canceled. The filter approach I mentioned earlier will not trigger a workflow on CircleCI.
Let me know if this points you in the right direction, and if I seem to have misunderstood your question, please let me know and I am happy to take another look!