Based on tjtharrison’s solution.
One challenge we’ll face sooner or later when managing multiple Terraform modules is how to effectively organize them and how to handle Terraform module versioning. Should we opt for individual repositories or consolidate them into a monorepo?
Each approach has its advantages and drawbacks. Placing each module in its own repository ensures modularity and encapsulation, which simplifies the management of releases. However, this approach might lead to a complex web of dependencies when changes in one module impact several others.
In contrast, using a single repository could serve as a centralized source of truth for all infrastructure configurations, making the testing and review processes more efficient. The difficulty, however, will be in creating and managing versioned releases to allow deploying different versions of our modules on development and production environments. This is where careful Terraform module versioning becomes crucial.
In this post, our focus will be on utilizing automation and the semantic-release tool as a solution for managing releases and Terraform module versioning in a monorepo setup.
In this post, our focus will be on utilizing automation and the semantic-release tool as a solution for managing releases and Terraform module versioning in a monorepo setup.
Using the semantic-release tool
Semantic-release is a NodeJS tool that can be used on any code base to automate the whole package release workflow including determining the next version number, generating the release notes, and publishing the package.
It follows the standard Semantic Versioning format of MAJOR.MINOR.PATCH, for example 1.2.3. Using semantic-release in a project can help ensure that all versions created will strictly adhere to semantic versioning, prohibiting jumping versions or moving backwards.
How does semantic-release work?
Semantic-release uses commit messages to determine the impact of the pushed changes in the codebase. By following formalized conventions for commit messages, it automatically determines the next semantic version number, generates a changelog and publishes the release.
The table below shows which commit message gets you which release type when semantic-release runs (using the default configuration).
Commit message |
Release type |
`fix(pencil): stop graphite breaking when too much pressure applied` |
Patch Fix Release |
`feat(pencil): add ‘graphiteWidth’ option` |
Minor Feature Release |
`perf(pencil): remove graphiteWidth option` `BREAKING CHANGE: The graphiteWidth option has been removed.` `The default graphite width of 10mm is always used for performance reasons.` |
Major Breaking Release (Note that the BREAKING CHANGE: token must be in the footer of the commit) |
Semantic-release is meant to be executed on the continuous integration environment after every successful build on the release branch. This way no human is directly involved in the release process.
Implementation
The Qubika’s sre-terraform-modules repository will be used as an example on how release automation could be implemented. The repository currently has the following structure: