|
2 | 2 |
|
3 | 3 | [](https://circleci.com/gh/bufferings/orb-split-config) [](https://circleci.com/orbs/registry/orb/bufferings/orb-split-config) [](https://raw.githubusercontent.com/bufferings/orb-split-config/master/LICENSE) [](https://discuss.circleci.com/c/ecosystem/orbs) |
4 | 4 |
|
5 | | -You can merge split files when CircleCI starts. |
6 | | -This Orb uses CUE https://cuelang.org/ to merge the configuration files. |
| 5 | +## Are you having trouble with a large configuration file? |
| 6 | + |
| 7 | +Here is Split Config Orb for you! |
| 8 | + |
| 9 | +You no longer have to tire of managing one huge configuration file. With this Orb, you can split the `config.yml` into multiple files. The split configs are merged when CircleCI starts. |
| 10 | + |
| 11 | +## Getting Started |
| 12 | + |
| 13 | +### `config.yml` file |
| 14 | + |
| 15 | +It's super simple, you just need to prepare the following `config.yml`: |
| 16 | + |
| 17 | +```yaml |
| 18 | +version: 2.1 |
| 19 | + |
| 20 | +setup: true |
| 21 | + |
| 22 | +orbs: |
| 23 | + # Please specify the latest version |
| 24 | + split-config: bufferings/[email protected] |
| 25 | + |
| 26 | +workflows: |
| 27 | + generate-config: |
| 28 | + jobs: |
| 29 | + - split-config/generate-config: |
| 30 | + find-config-regex: .*/\.circleci/config\.yml |
| 31 | +``` |
| 32 | +
|
| 33 | +The Orb finds the split config files according to the regex, merges them, and starts a CircleCI pipeline with the merged config. |
| 34 | +
|
| 35 | +### Enable Dynamic Config |
| 36 | +
|
| 37 | +This Orb uses the [Dynamic Config](https://circleci.com/docs/dynamic-config) feature of CircleCI. By default, the feature is disabled, so please [enable the feature](https://circleci.com/docs/dynamic-config#getting-started-with-dynamic-config-in-circleci) to get started. |
| 38 | +
|
| 39 | +## How to use |
| 40 | +
|
| 41 | +### I want to use regex to find the split configs |
| 42 | +
|
| 43 | +You can use regex to specify config paths. Actually, it uses `find` command with the regex. Please use relative paths to the working directory. |
| 44 | + |
| 45 | +**Regex Example1** |
| 46 | + |
| 47 | +For example, if your repository structure is like this: |
| 48 | + |
| 49 | +<img src="./img/example1.png" alt="example1 files" width="300"/> |
| 50 | + |
| 51 | +The `config.yml` is like this: |
| 52 | + |
| 53 | +```yaml |
| 54 | +version: 2.1 |
| 55 | +
|
| 56 | +setup: true |
| 57 | +
|
| 58 | +orbs: |
| 59 | + # Please specify the latest version |
| 60 | + split-config: bufferings/[email protected] |
| 61 | +
|
| 62 | +workflows: |
| 63 | + generate-config: |
| 64 | + jobs: |
| 65 | + - split-config/generate-config: |
| 66 | + find-config-regex: .*/\.circleci/config\.yml |
| 67 | +``` |
| 68 | + |
| 69 | +Working example: https://github.com/bufferings/orb-split-config-example1 |
| 70 | + |
| 71 | +> **Note** |
| 72 | +> The Orb ignores `./.circleci/config.yml` file. Specifically, it automatically adds `-not -regex "\./\.circleci/config\.yml"` option. |
| 73 | + |
| 74 | +**Example2** |
| 75 | + |
| 76 | +Here is another regex example. If you put configs under `.circleci/configs` directory, it will be like this: |
| 77 | + |
| 78 | +```yaml |
| 79 | +version: 2.1 |
| 80 | +
|
| 81 | +setup: true |
| 82 | +
|
| 83 | +orbs: |
| 84 | + # Please specify the latest version |
| 85 | + split-config: bufferings/[email protected] |
| 86 | +
|
| 87 | +workflows: |
| 88 | + generate-config: |
| 89 | + jobs: |
| 90 | + - split-config/generate-config: |
| 91 | + find-config-regex: \./\.circleci/configs/.*\.yml |
| 92 | +``` |
| 93 | + |
| 94 | +Working example: https://github.com/bufferings/orb-split-config-example2 |
| 95 | + |
| 96 | +### I want to write the split config paths in the `config.yml` |
| 97 | + |
| 98 | +You can set the fixed paths in your `config.yml`. Please use relative paths to the working directory. |
| 99 | + |
| 100 | +```yaml |
| 101 | +version: 2.1 |
| 102 | +
|
| 103 | +setup: true |
| 104 | +
|
| 105 | +orbs: |
| 106 | + # Please specify the latest version |
| 107 | + split-config: bufferings/[email protected] |
| 108 | +
|
| 109 | +workflows: |
| 110 | + generate-config: |
| 111 | + jobs: |
| 112 | + - split-config/generate-config: |
| 113 | + fixed-config-paths: | |
| 114 | + ./common/.circleci/config.yml |
| 115 | + ./service1/.circleci/config.yml |
| 116 | + ./service2/.circleci/config.yml |
| 117 | + ./service3/.circleci/config.yml |
| 118 | +``` |
| 119 | + |
| 120 | +Working example: https://github.com/bufferings/orb-split-config-example3 |
| 121 | + |
| 122 | +### I want to write the split config paths in a file |
| 123 | + |
| 124 | +You can also use a file to specify the fixed list. Please use relative paths to the working directory. |
| 125 | + |
| 126 | +```shell |
| 127 | +❯ cat .circleci/config-list |
| 128 | +./common/.circleci/config.yml |
| 129 | +./service1/.circleci/config.yml |
| 130 | +./service2/.circleci/config.yml |
| 131 | +./service3/.circleci/config.yml |
| 132 | +``` |
| 133 | + |
| 134 | +```yaml |
| 135 | +version: 2.1 |
| 136 | +
|
| 137 | +setup: true |
| 138 | +
|
| 139 | +orbs: |
| 140 | + # Please specify the latest version |
| 141 | + split-config: bufferings/[email protected] |
| 142 | +
|
| 143 | +workflows: |
| 144 | + generate-config: |
| 145 | + jobs: |
| 146 | + - split-config/generate-config: |
| 147 | + config-list-path: .circleci/config-list |
| 148 | +``` |
| 149 | + |
| 150 | +Working example: https://github.com/bufferings/orb-split-config-example4 |
| 151 | + |
| 152 | +### I want to use all the ways to specify config paths |
| 153 | + |
| 154 | +You can! |
| 155 | + |
| 156 | +```yaml |
| 157 | +version: 2.1 |
| 158 | +
|
| 159 | +setup: true |
| 160 | +
|
| 161 | +orbs: |
| 162 | + # Please specify the latest version |
| 163 | + split-config: bufferings/[email protected] |
| 164 | +
|
| 165 | +workflows: |
| 166 | + generate-config: |
| 167 | + jobs: |
| 168 | + - split-config/generate-config: |
| 169 | + config-list-path: .circleci/config-list |
| 170 | + fixed-config-paths: | |
| 171 | + ./service1/.circleci/config.yml |
| 172 | + ./service2/.circleci/config.yml |
| 173 | + find-config-regex: \./service3/\.circleci/configs/.*\.yml |
| 174 | +
|
| 175 | +``` |
| 176 | + |
| 177 | +Working example: https://github.com/bufferings/orb-split-config-example5 |
| 178 | + |
| 179 | + |
| 180 | +### I want to use Split Config Orb with path-filtering Orb |
| 181 | + |
| 182 | +You can pass the generated config to path-filtering using workspace. |
| 183 | + |
| 184 | +```yaml |
| 185 | +version: 2.1 |
| 186 | +
|
| 187 | +setup: true |
| 188 | +
|
| 189 | +orbs: |
| 190 | + split-config: bufferings/[email protected] |
| 191 | + path-filtering: circleci/[email protected] |
| 192 | +
|
| 193 | +workflows: |
| 194 | + generate-config: |
| 195 | + jobs: |
| 196 | + - split-config/generate-config: |
| 197 | + find-config-regex: .*/\.circleci/.*\.yml |
| 198 | + generated-config-path: /tmp/generated_config.yml |
| 199 | + post-steps: |
| 200 | + - persist_to_workspace: |
| 201 | + root: /tmp |
| 202 | + paths: |
| 203 | + - generated_config.yml |
| 204 | + - path-filtering/filter: |
| 205 | + workspace_path: /tmp |
| 206 | + config-path: /tmp/generated_config.yml |
| 207 | + mapping: | |
| 208 | + service1/.* build-service1 true |
| 209 | + service2/.* build-service2 true |
| 210 | + requires: |
| 211 | + - split-config/generate-config |
| 212 | +``` |
| 213 | + |
| 214 | +Working example: https://github.com/bufferings/orb-split-config-example6 |
| 215 | + |
| 216 | +## How it works |
| 217 | + |
| 218 | +The Split Config Orb uses CUE to merge the YAML files. |
| 219 | + |
| 220 | +* CUE https://cuelang.org/ |
| 221 | + |
| 222 | +Therefore, it follows the CUE specification to merge the files. |
| 223 | + |
| 224 | +## Restrictions |
| 225 | + |
| 226 | +There're some restrictions coming from the mechanism behind this Orb. |
| 227 | + |
| 228 | +You need to be careful not to use the same name for Jobs and Workflows. Since it is merged into one YAML file in the end, you can't use the same Job name nor Workflow name. |
| 229 | + |
| 230 | +YAML anchors and aliases need to be in the same YAML file. When CUE import YAML, it parse the anchors and aliases in the file. Therefore, you can't put anchors and aliases into different files separately. |
| 231 | + |
| 232 | +## (Advanced) I want to use CUE to write CircleCI config |
| 233 | + |
| 234 | +```yaml |
| 235 | +``` |
7 | 236 |
|
8 | 237 | --- |
9 | 238 |
|
|
0 commit comments