| 1 |
# Configuring Commitizen in a monorepo |
| 2 |
|
| 3 |
This tutorial assumes that your monorepo is structured with multiple components that can be released independently of each other. |
| 4 |
It also assumes that you are using conventional commits with scopes. |
| 5 |
|
| 6 |
Here is a step-by-step example using two libraries, `library-b` and `library-z`: |
| 7 |
|
| 8 |
1. **Organize your monorepo** |
| 9 |
|
| 10 |
For example, you might have one of these layouts: |
| 11 |
|
| 12 |
```shell-session |
| 13 |
. |
| 14 |
├── library-b |
| 15 |
│ └── .cz.toml |
| 16 |
└── library-z |
| 17 |
└── .cz.toml |
| 18 |
``` |
| 19 |
|
| 20 |
```shell-session |
| 21 |
src |
| 22 |
├── library-b |
| 23 |
│ └── .cz.toml |
| 24 |
└── library-z |
| 25 |
└── .cz.toml |
| 26 |
``` |
| 27 |
|
| 28 |
2. **Add a Commitizen configuration for each component** |
| 29 |
|
| 30 |
```toml |
| 31 |
# library-b/.cz.toml |
| 32 |
[tool.commitizen] |
| 33 |
name = "cz_customize" |
| 34 |
version = "0.0.0" |
| 35 |
tag_format = "${version}-library-b" # the component name can be a prefix or suffix with or without a separator |
| 36 |
ignored_tag_formats = ["${version}-library-*"] # Avoid noise from other tags |
| 37 |
update_changelog_on_bump = true |
| 38 |
``` |
| 39 |
|
| 40 |
```toml |
| 41 |
# library-z/.cz.toml |
| 42 |
[tool.commitizen] |
| 43 |
name = "cz_customize" |
| 44 |
version = "0.0.0" |
| 45 |
tag_format = "${version}-library-z" |
| 46 |
ignored_tag_formats = ["${version}-library-*"] # Avoid noise from other tags |
| 47 |
update_changelog_on_bump = true |
| 48 |
``` |
| 49 |
|
| 50 |
3. **Bump each component independently** |
| 51 |
|
| 52 |
```sh |
| 53 |
cz --config library-b/.cz.toml bump --yes |
| 54 |
cz --config library-z/.cz.toml bump --yes |
| 55 |
``` |
| 56 |
|
| 57 |
|
| 58 |
## Changelog per component |
| 59 |
|
| 60 |
To filter the correct commits for each component, you'll need to define a strategy. |
| 61 |
|
| 62 |
For example: |
| 63 |
|
| 64 |
- Trigger the pipeline based on the changed path. This can have some downsides, as you'll rely on the developer not including files from unrelated components. |
| 65 |
- [GitHub Actions](https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#onpushpull_requestpull_request_targetpathspaths-ignore) uses `path` |
| 66 |
- [Jenkins](https://www.jenkins.io/doc/book/pipeline/syntax/#built-in-conditions) uses `changeset` |
| 67 |
- [GitLab](https://docs.gitlab.com/ee/ci/yaml/#ruleschanges) uses `rules:changes` |
| 68 |
- Filter commits by a specific pattern in the commit message (recommended) |
| 69 |
|
| 70 |
|
| 71 |
### Example with scope in conventional commits |
| 72 |
|
| 73 |
In this example, we want `library-b`'s changelog to only include commits that use the `library-b` scope. |
| 74 |
To achieve this, we configure Commitizen to match only commit messages with that scope. |
| 75 |
|
| 76 |
Here is an example configuration for `library-b`: |
| 77 |
|
| 78 |
```toml |
| 79 |
[tool.commitizen.customize] |
| 80 |
changelog_pattern = "^(feat|fix)\\(library-b\\)(!)?:" # the type pattern can be a wildcard or any types you wish to include |
| 81 |
``` |
| 82 |
|
| 83 |
With this configuration, a commit message like the following would be included in `library-b`'s changelog: |
| 84 |
|
| 85 |
```text |
| 86 |
fix(library-b): Some awesome message |
| 87 |
``` |