Hello. I’m working on a config.yml file. I’m trying to make it where one of the multiple jobs is exclusive to a certain branch. I found the listing for that branches parameter, but it said it would only work if I only had 1 job in my config file. So is there a way I can get around that to make just one of the jobs only works with that specific branch?
Hi @JPGR94, welcome to Discuss!
You can get close to what you are looking for with advanced conditional logic – I say ‘close’ since the job will still run, but the actual steps you want isolated to the specific branch will only run on that branch.
As an example (note: you have to have at least one step outside the when
condition, hence my note above about it being ‘close’):
version: 2.1
jobs:
job-1:
docker:
- image: cimg/base:stable
steps:
- run: echo "at least one step"
- when:
condition:
equal: [ default, << pipeline.git.branch >> ]
steps:
- run: echo "I am on default"
- checkout
job-2:
docker:
- image: cimg/base:stable
steps:
- checkout
- run: echo "always runs"
workflows:
run-jobs:
jobs:
- job-1
- job-2
With the above setup both jobs will always run, however the - checkout
and - run: echo "I am on default"
steps for job-1
will only ever trigger when the branch is default
.
I also wrote this support article that details the advanced logic a bit more, you may get some more ideas there.
I hope that helps and please let me know if you need anything else!
I’ve been going over many examples of this, yet I keep getting some form of this build exception:
ERROR IN CONFIG FILE:
[#/jobs/deploy-for-dev] 0 subschemas matched instead of one
1. [#/jobs/deploy-for-dev] only 1 subschema matches out of 2
| 1. [#/jobs/deploy-for-dev/steps/0] 0 subschemas matched instead of one
| | 1. [#/jobs/deploy-for-dev/steps/0] 3 schema violations found
| | | long form commands like `run:`
| | | 1. [#/jobs/deploy-for-dev/steps/0] maximum size: [1], found: [2]
| | | | long form commands like `run:`
| | | | SCHEMA:
| | | | maxProperties: 1
| | | | INPUT:
| | | | when: null
| | | | condition:
| | | | equal:
| | | | - development
| | | | - << pipeline.git.branch >>
| | | | steps:
| | | | - run:
| | | | name: Development Branch Check
| | | | command: |
| | | | echo -n "You shouldn't be seeing this if you aren't development."
| | | 2. [#/jobs/deploy-for-dev/steps/0/steps] no subschema matched out of the total 2 subschemas
| | | | 1. [#/jobs/deploy-for-dev/steps/0/steps] expected type: Mapping, found: Sequence
| | | | | SCHEMA:
| | | | | type:
| | | | | - object
| | | | | - string
| | | | | INPUT:
| | | | | - run:
| | | | | name: Development Branch Check
| | | | | command: |
| | | | | echo -n "You shouldn't be seeing this if you aren't development."
| | | | 2. [#/jobs/deploy-for-dev/steps/0/steps] expected type: String, found: Sequence
| | | | | SCHEMA:
| | | | | type:
| | | | | - object
| | | | | - string
| | | | | INPUT:
| | | | | - run:
| | | | | name: Development Branch Check
| | | | | command: |
| | | | | echo -n "You shouldn't be seeing this if you aren't development."
| | 2. [#/jobs/deploy-for-dev/steps/0] expected type: String, found: Mapping
| | | Shorthand commands, like `checkout`
| | | SCHEMA:
| | | type: string
| | | INPUT:
| | | when: null
| | | condition:
| | | equal:
| | | - development
| | | - << pipeline.git.branch >>
| | | steps:
| | | - run:
| | | name: Development Branch Check
| | | command: |
| | | echo -n "You shouldn't be seeing this if you aren't development."
2. [#/jobs/deploy-for-dev] expected type: String, found: Mapping
| Job may be a string reference to another job
Here is the relevant code in its most current form, basically just testing with the printing of a message:
deploy-for-dev: #Exclusive to development branch for automatic deployment to staging server
working_directory: ~/my-project/My-Project/
docker:
- image: circleci/python:3.6.2-stretch-browsers
steps:
- when:
condition:
equal: [ development , << pipeline.git.branch >> ]
steps:
- run:
name: Development Branch Check
command: |
echo -n "You shouldn't be seeing this if you aren't development."
Hi @JPGR94 thanks for the reply!
I believe the issue may be that the job doesn’t have at least one step outside of the conditional, I mentioned that in my previous reply but realize I maybe didn’t make it clear that its a requirement for this to work.
Can you try updating your config to something like:
working_directory: ~/my-project/My-Project/
docker:
- image: circleci/python:3.6.2-stretch-browsers
steps:
- run: echo "one step outside conditionals"
- when:
condition:
equal: [ development , << pipeline.git.branch >> ]
steps:
- run:
name: Development Branch Check
command: |
echo -n "You shouldn't be seeing this if you aren't development."
If the above still doesn’t work, can you submit a support request with a link to the failing build and mention this discuss post? I can take a closer look that way at your specific example.
Thank you!
Made the change. Now I’m getting this.
#!/bin/sh -eo pipefail
ERROR IN CONFIG FILE:
[#/jobs/deploy-for-dev] 0 subschemas matched instead of one
1. [#/jobs/deploy-for-dev] only 1 subschema matches out of 2
| 1. [#/jobs/deploy-for-dev/steps/1] 0 subschemas matched instead of one
| | 1. [#/jobs/deploy-for-dev/steps/1] 3 schema violations found
| | | long form commands like run:
| | | 1. [#/jobs/deploy-for-dev/steps/1] maximum size: [1], found: [2]
| | | | long form commands like run:
| | | | SCHEMA:
| | | | maxProperties: 1
| | | | INPUT:
| | | | when: null
| | | | condition:
| | | | equal:
| | | | - development
| | | | - << pipeline.git.branch >>
| | | | steps:
| | | | - run:
| | | | name: Development Branch Check
| | | | command: |
| | | | echo -n “You shouldn’t be seeing this if you aren’t development.”
| | | 2. [#/jobs/deploy-for-dev/steps/1/steps] no subschema matched out of the total 2 subschemas
| | | | 1. [#/jobs/deploy-for-dev/steps/1/steps] expected type: Mapping, found: Sequence
| | | | | SCHEMA:
| | | | | type:
| | | | | - object
| | | | | - string
| | | | | INPUT:
| | | | | - run:
| | | | | name: Development Branch Check
| | | | | command: |
| | | | | echo -n “You shouldn’t be seeing this if you aren’t development.”
| | | | 2. [#/jobs/deploy-for-dev/steps/1/steps] expected type: String, found: Sequence
| | | | | SCHEMA:
| | | | | type:
| | | | | - object
| | | | | - string
| | | | | INPUT:
| | | | | - run:
| | | | | name: Development Branch Check
| | | | | command: |
| | | | | echo -n “You shouldn’t be seeing this if you aren’t development.”
| | 2. [#/jobs/deploy-for-dev/steps/1] expected type: String, found: Mapping
| | | Shorthand commands, like checkout
| | | SCHEMA:
| | | type: string
| | | INPUT:
| | | when: null
| | | condition:
| | | equal:
| | | - development
| | | - << pipeline.git.branch >>
| | | steps:
| | | - run:
| | | name: Development Branch Check
| | | command: |
| | | echo -n “You shouldn’t be seeing this if you aren’t development.”
2. [#/jobs/deploy-for-dev] expected type: String, found: Mapping
| Job may be a string reference to another job
-------
Warning: This configuration was auto-generated to show you the message above.
Don’t rerun this job. Rerunning will have no effect.
false
Thanks for attempting that change, sorry it didn’t do the trick!
To continue troubleshooting this please submit a support ticket with a link to this post and a link to your project/build and I can take a closer look at the overall setup and get to the bottom of this.
Thanks!