Skip to content

Commit 7f0ef4f

Browse files
authored
Merge pull request circleci#4381 from circleci/logic
2 parents c4bf8c4 + 13767b2 commit 7f0ef4f

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

jekyll/_cci2/configuration-cookbook.md

+43
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ Configuration Recipe | Description
6565
[Using Amazon Elastic Container Service for Kubernetes (Amazon EKS)](#using-amazon-elastic-container-service-for-kubernetes-amazon-eks) | This section describes how you can use the Amazon ECS service for Kubernetes for Kubernetes-related tasks and operations.
6666
[Deploying Applications to Heroku](#deploying-applications-to-heroku) | This section describes how you can deploy application to the Heroku platform using the CircleCI Heroku orb.
6767
[Enabling Custom Slack Notifications in CircleCI Jobs](#enabling-custom-slack-notifications-in-circleci-jobs) | This section describes how you can enable customized Slack notifications in CircleCI jobs.
68+
[Selecting a Workflow With a Pipeline Parameter](#selecting-a-workflow-with-a-pipeline-parameter) | This section describes how you can use pipeline parameters to run different workflows.
6869

6970
## Deploying Software Changes to Amazon ECS
7071

@@ -1129,3 +1130,45 @@ version: 2.1
11291130
Notice in the example that the job is run and a Slack status alert is sent to your recipients (USERID1, USERID2) if the job has failed.
11301131

11311132
For more detailed information about this orb and its functionality, refer to the Slack orb in the [CircleCI Orb Registry](https://circleci.com/orbs/registry/orb/circleci/slack).
1133+
1134+
## Selecting a Workflow With a Pipeline Parameter
1135+
1136+
If you want to be able to trigger custom workflows manually via the API, but still run a workflow on every push, you can use pipeline parameters to decide which workflows to run.
1137+
1138+
```yaml
1139+
version: 2.1
1140+
1141+
parameters:
1142+
action:
1143+
type: enum
1144+
enum: [build, report]
1145+
default: build
1146+
1147+
jobs:
1148+
build:
1149+
machine: true
1150+
steps:
1151+
- checkout
1152+
- run: ./run-tests.sh
1153+
1154+
report:
1155+
machine: true
1156+
steps:
1157+
- checkout
1158+
- run: ./create-report.sh
1159+
1160+
workflows:
1161+
build:
1162+
when:
1163+
equal: [ build, << pipeline.parameters.action >> ]
1164+
jobs:
1165+
- build
1166+
1167+
report:
1168+
when:
1169+
equal: [ report, << pipeline.parameters.action >> ]
1170+
jobs:
1171+
- report
1172+
```
1173+
1174+
The `action` parameter will default to `build` on pushes, but you can supply a different value to select a different workflow to run, like `report`.

jekyll/_cci2/configuration-reference.md

+35-2
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ A conditional step consists of a step with the key `when` or `unless`. Under the
805805

806806
Key | Required | Type | Description
807807
----|-----------|------|------------
808-
condition | Y | String | A parameter value
808+
condition | Y | Logic | [A logic statement](https://circleci.com/docs/2.0/configuration-reference/#logic-statements)
809809
steps | Y | Sequence | A list of steps to execute when the condition is true
810810
{: class="table table-striped"}
811811

@@ -1547,7 +1547,7 @@ workflows:
15471547

15481548
##### **Using `when` in Workflows**
15491549

1550-
With CircleCI v2.1 configuration, you may use a `when` clause (the inverse clause `unless` is also supported) under a workflow declaration with a truthy or falsy value to determine whether or not to run that workflow. The most common use of `when` is with [CircleCI API v2 pipeline triggering](https://circleci.com/docs/api/v2/#trigger-a-new-pipeline) with parameters. Truthy/falsy calues can be booleans, numbers, and strings. Falsy would be any of: false, 0, empty string, null, and NaN. Everything else would be truthy.
1550+
With CircleCI v2.1 configuration, you may use a `when` clause (the inverse clause `unless` is also supported) under a workflow declaration with a [logic statement](https://circleci.com/docs/2.0/configuration-reference/#logic-statements) to determine whether or not to run that workflow.
15511551

15521552
The example configuration below uses a pipeline parameter, `run_integration_tests` to drive the `integration_tests` workflow.
15531553

@@ -1582,6 +1582,39 @@ This example prevents the workflow `integration_tests` from running unless the t
15821582

15831583
Refer to the [Orchestrating Workflows]({{ site.baseurl }}/2.0/workflows) document for more examples and conceptual information.
15841584

1585+
## Logic Statements
1586+
1587+
Certain dynamic configuration features accept logic statements as arguments. Logic statements are evaluated to boolean values at configuration compilation time, so before the workflow is run. The group of logic statements includes:
1588+
1589+
Type | Arguments | `true` if | Example
1590+
-------------------|--------------------|----------------------------------------|----------------------------------------
1591+
YAML literal | None | is truthy | `true`/`42`/`"a string"`
1592+
[Pipeline Value](https://circleci.com/docs/2.0/pipeline-variables/#pipeline-values) | None | resolves to a truthy value | `<< pipeline.git.branch >>`
1593+
[Pipeline Parameter](https://circleci.com/docs/2.0/pipeline-variables/#pipeline-parameters-in-configuration) | None | resolves to a truthy value | `<< pipeline.parameter.my-parameter >>`
1594+
and | N logic statements | all arguments are truthy | `and: [ true, true, false ]`
1595+
or | N logic statements | any argument is truthy | `or: [ false, true, false ]`
1596+
not | 1 logic statement | the argument is not truthy | `not: true`
1597+
equal | N values | all arguments evaluate to equal values | `equal: [ 42, << pipeline.number >>]`
1598+
{: class="table table-striped"}
1599+
1600+
Truthiness rules are as follows:
1601+
1602+
`false`, `null`, `0`, the empty string, and `NaN` are falsy. Any other value is truthy.
1603+
1604+
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.
1605+
1606+
### Example
1607+
1608+
```yaml
1609+
when:
1610+
and:
1611+
- not:
1612+
equal: [ master, << pipeline.git.branch >> ]
1613+
- or:
1614+
- equal: [ canary, << pipeline.git.tag >> ]
1615+
- << pipeline.parameter.deploy-canary >>
1616+
```
1617+
15851618
## Full Example
15861619
{:.no_toc}
15871620

0 commit comments

Comments
 (0)