Skip to content
This repository was archived by the owner on Feb 8, 2024. It is now read-only.

Commit 49d7ca6

Browse files
committed
Add preview docs for path filtering
1 parent d49ee65 commit 49d7ca6

File tree

2 files changed

+97
-0
lines changed

2 files changed

+97
-0
lines changed

docs/path-filtering.md

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# Setup Workflows, Path Filtering, and Monorepos
2+
3+
CircleCI has so far not had any first-class monorepo support. One common problem this entails for monorepo users is that every push runs the entire CI suite.
4+
5+
The work to be run is defined in CircleCI configuration, which is fetched and compiled before any workflow is created or run. As such it cannot be dynamically changed based on the changes in the repository, for example just build the components of a monorepo which have actually changed.
6+
7+
8+
## Setup Workflows
9+
10+
Setup workflows are a new opt-in CircleCI feature which allows the running of a workflow to dynamically generate a CircleCI configuration and pipeline parameters, and then run the resulting work within the same pipeline. This way it sidesteps the limitation outlined above and enables dynamic work discovery.
11+
12+
To this end, the pipeline enters a new `setup` phase (as denoted by the `setup` stanza in the configuration), during which a single workflow can be run. The jobs in this workflow have access to a special `continuation-key`, which can be used to call the public continuation API at [/api/v2/pipeline/continue](https://circleci.com/docs/api/v2/#operation/continuePipeline). This call includes a new configuration file, as well as pipeline parameters. The pipeline is then advanced to the regular `created` phase. A pipeline can only be continued once, and only within 6 hours.
13+
14+
If a pipeline enters the `setup` phase, the configuration used initially is called the `setup config`, and is stored alongside the regular configuration.
15+
16+
The `continuation-key` is injected into the jobs as a secret environment variable, `$CIRCLE_CONTINUATION_KEY`, and excluded from API responses and step output. We strongly recommend against extracting it from the job, as it allows submission of arbitrary configurations, and thus could potentially be used to extract secrets from restricted contexts.
17+
18+
![img](./setup-workflows.png "Flow")
19+
20+
21+
## Using the Path Filtering Orb
22+
23+
To reduce the boilerplate required for a monorepo setup, we have created a set of orbs to implement path filtering and [automatic continuation](https://circleci.com/developer/orbs/orb/sandbox/continuation).
24+
25+
First, you need to enable setup workflows for your project (Project Settings -> Advanced -> At the bottom). After signing up to the preview, navigate to the project settings page on CircleCI and enable the "Run Setup Workflows" setting.
26+
27+
The following steps can happen on a feature branch.
28+
29+
Move your existing config to `.circleci/continue_config.yml`.
30+
31+
Separate out the tests for different components into separate workflows, and gate them on pipeline parameters, like so:
32+
33+
```yaml
34+
version: 2.1
35+
36+
parameters:
37+
build-server:
38+
type: boolean
39+
default: false
40+
build-client:
41+
type: boolean
42+
default: false
43+
44+
jobs:
45+
- [...]
46+
47+
workflows:
48+
build-server:
49+
when: << pipeline.parameters.build-server >>
50+
jobs:
51+
- [...]
52+
build-client:
53+
when: << pipeline.parameters.build-client >>
54+
jobs:
55+
- [...]
56+
```
57+
58+
The `equal` statements ensure that the workflows always run on the default branch, i.e. after merging.
59+
60+
Place this config at `.circleci/config.yml`:
61+
62+
```yaml
63+
version: 2.1
64+
65+
setup: true
66+
67+
orbs:
68+
path-filtering: circleci/[email protected]
69+
70+
workflows:
71+
setup-workflow:
72+
jobs:
73+
- path-filtering/filter:
74+
mapping: |
75+
src/server/.* build-server true
76+
src/client/.* build-client true
77+
# Optional, defaults to main:
78+
base-revision: origin/develop
79+
```
80+
81+
Match the paths and parameters in the mapping to your project.
82+
83+
The mappings consist of three whitespace-separated elements, a regular expression matching a path, a pipeline parameter, and a value for that parameter.
84+
85+
The mappings are evaluated in order. If the regular expression matches any file changed between the base revision and the current `HEAD`, the pipeline parameter will be set to the value specified. Later matches override earlier ones. **Note**: If `base-revision` is a non-default branch, it needs to be prefixed with `origin/`, as it will not be fetched by default.
86+
87+
The regular expressions support the full [Python re syntax](https://docs.python.org/3.8/library/re.html#regular-expression-syntax), and are automatically enclosed by `^` and `$` to prevent partial matches.
88+
89+
90+
## Further Reading
91+
92+
You can find more information about the elements used here:
93+
94+
- [Pipeline Variables](https://circleci.com/docs/2.0/pipeline-variables/)
95+
- [Logic Statements](https://circleci.com/docs/2.0/configuration-reference/#logic-statements)
96+
- [Conditional Steps](https://circleci.com/docs/2.0/reusing-config/#defining-conditional-steps)
97+
- [Introduction to Orbs](https://circleci.com/docs/2.0/orb-intro/)

docs/setup-workflows.png

21.8 KB
Loading

0 commit comments

Comments
 (0)