Skip to content

Instantly share code, notes, and snippets.

@Voronenko
Created July 29, 2022 11:46
Show Gist options
  • Save Voronenko/48b76e26e94f936fab129d4d8afc94d4 to your computer and use it in GitHub Desktop.
Save Voronenko/48b76e26e94f936fab129d4d8afc94d4 to your computer and use it in GitHub Desktop.
CIRCLECI CONDITIONS

With the recent addition of advanced logic in a config file, the option to conditionally trigger steps in a job or to conditionally trigger a workflow is now available.

Specific logic statements can be used to create multiple nested conditions, that will always at the top level result in true or false -- which in turn determines if the workflow or steps are triggered.

Type Arguments true if Example
YAML literal None is truthy true/42/"a string"
YAML alias None resolves to a truthy value *my-alias
Pipeline Value None resolves to a truthy value << pipeline.git.branch >>
Pipeline Parameter None resolves to a truthy value << pipeline.parameters.my-parameter >>
and N logic statements all arguments are truthy and: [ true, true, false ]
or N logic statements any argument is truthy or: [ false, true, false ]
not 1 logic statements the argument is not truthy not: true
equal N values all arguments evaluate to equal values equal: [ 42, << pipeline.number >>]
matches pattern and value value matches the pattern matches: { pattern: "^feature-.+$", value: << pipeline.git.branch >> }

The following logic values are considered falsy:

false null 0 NaN empty strings (“”) statements with no arguments

All other values are truthy. Also note that using logic with an empty list will cause a validation error.

Logic statements always evaluate to a boolean value at the top level, and coerce as necessary. They can be nested in an arbitrary fashion, according to their argument specifications, and to a maximum depth of 100 levels.

matches uses Java regular expressions for its pattern. A full match pattern must be provided, prefix matching is not an option. Though, it is recommended to enclose a pattern in ^ and $ to avoid accidental partial matches.

Note: When using logic statements at the workflow level, do not include the condition: key (the condition key is only needed for job level logic statements).

version: 2.1
jobs:
build:
docker:
- image: ubuntu:14.04
auth:
username: mydockerhub-user
password: $DOCKERHUB_PASSWORD # context / project UI env-var reference
- image: mongo:2.6.8
auth:
username: mydockerhub-user
password: $DOCKERHUB_PASSWORD # context / project UI env-var reference
command: [mongod, --smallfiles]
- image: postgres:14.2
auth:
username: mydockerhub-user
password: $DOCKERHUB_PASSWORD # context / project UI env-var reference
# some containers require setting environment variables
environment:
POSTGRES_USER: user
- image: redis@sha256:54057dd7e125ca41afe526a877e8bd35ec2cdd33b9217e022ed37bdcf7d09673
auth:
username: mydockerhub-user
password: $DOCKERHUB_PASSWORD # context / project UI env-var reference
- image: rabbitmq:3.5.4
auth:
username: mydockerhub-user
password: $DOCKERHUB_PASSWORD # context / project UI env-var reference
environment:
TEST_REPORTS: /tmp/test-reports
working_directory: ~/my-project
steps:
- checkout
- run:
command: echo 127.0.0.1 devhost | sudo tee -a /etc/hosts
# Create Postgres users and database
# Note the YAML heredoc '|' for nicer formatting
- run: |
sudo -u root createuser -h localhost --superuser ubuntu &&
sudo createdb -h localhost test_db
- restore_cache:
keys:
- v1-my-project-{{ checksum "project.clj" }}
- v1-my-project-
- run:
environment:
SSH_TARGET: "localhost"
TEST_ENV: "linux"
command: |
set -xu
mkdir -p ${TEST_REPORTS}
run-tests.sh
cp out/tests/*.xml ${TEST_REPORTS}
- run: |
set -xu
mkdir -p /tmp/artifacts
create_jars.sh << pipeline.number >>
cp *.jar /tmp/artifacts
- save_cache:
key: v1-my-project-{{ checksum "project.clj" }}
paths:
- ~/.m2
# Save artifacts
- store_artifacts:
path: /tmp/artifacts
destination: build
# Upload test results
- store_test_results:
path: /tmp/test-reports
deploy-stage:
docker:
- image: ubuntu:14.04
auth:
username: mydockerhub-user
password: $DOCKERHUB_PASSWORD # context / project UI env-var reference
working_directory: /tmp/my-project
steps:
- run:
name: Deploy if tests pass and branch is Staging
command: ansible-playbook site.yml -i staging
deploy-prod:
docker:
- image: ubuntu:14.04
auth:
username: mydockerhub-user
password: $DOCKERHUB_PASSWORD # context / project UI env-var reference
working_directory: /tmp/my-project
steps:
- run:
name: Deploy if tests pass and branch is Main
command: ansible-playbook site.yml -i production
workflows:
version: 2
build-deploy:
jobs:
- build:
filters:
branches:
ignore:
- develop
- /feature-.*/
- deploy-stage:
requires:
- build
filters:
branches:
only: staging
- deploy-prod:
requires:
- build
filters:
branches:
only: main
- when:
condition:
or:
- and:
- equal: [ main, << pipeline.git.branch >> ]
- or: [ << pipeline.parameters.param1 >>, << pipeline.parameters.param2 >> ]
- or:
- equal: [ false, << pipeline.parameters.param1 >> ]
steps:
- run: echo "I am on main AND param1 is true OR param2 is true -- OR param1 is false"
version: 2.1
executors:
linux-13:
docker:
- image: cimg/node:13.13
auth:
username: mydockerhub-user
password: $DOCKERHUB_PASSWORD # context / project UI env-var reference
macos: &macos-executor
macos:
xcode: 12.5.1
jobs:
test:
parameters:
os:
type: executor
node-version:
type: string
executor: << parameters.os >>
steps:
- checkout
- when:
condition:
equal: [ *macos-executor, << parameters.os >> ]
steps:
- run: echo << parameters.node-version >>
- run: echo 0
workflows:
all-tests:
jobs:
- test:
os: macos
node-version: "13.13.0"
- when:
condition:
or:
- and:
- or:
- and:
- equal: [ main, << pipeline.git.branch >> ]
- equal: [ false, << pipeline.parameters.param1 >> ]
- or:
- not: << pipeline.parameters.param3 >>
- or:
- equal: [ false, << pipeline.parameters.param3 >> ]
- or: [ << pipeline.parameters.param1 >>, << pipeline.parameters.param2 >> ]
- or:
- equal: [ true, << pipeline.parameters.param4 >> ]
steps:
- run: echo "param 4 is true OR the other nested conditions are true"
workflows:
conditional-workflow:
when:
and: # All must be true to trigger
- equal: [ main, << pipeline.git.branch >> ]
- not: << pipeline.parameters.param1 >>
- or: [ << pipeline.parameters.param1 >>, << pipeline.parameters.param2 >> ]
jobs:
- job-on-condition
workflows:
my-workflow:
when:
or:
- equal: [ main, << pipeline.git.branch >> ]
- equal: [ staging, << pipeline.git.branch >> ]
workflows:
my-workflow:
when:
and:
- not:
matches:
pattern: "^main$"
value: << pipeline.git.branch >>
- or:
- equal: [ canary, << pipeline.git.tag >> ]
- << pipeline.parameters.deploy-canary >>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment