Using Github Actions to scaffold repositories
Usually, in many projects, we need to create different repositories with a similar folder structure. In this scenario, you can use GitHub Templates, which I will cover in another blog post. For now, in this post, I’d like to discuss how to use GitHub Actions to scaffold monorepo repositories using CookieCutter in a self-service manner.
CookieCutter
CookieCutter is a tool that help us creating templates using Jinja2 as templating language. Some examples are:
In addition, this tool uses an answer file called cookiecutter.json to set up the default values for each variable in the templates. We can take advantage of this file to configure default settings for our environments, such as:
Later, by running the following command, we can create the template. Please note that we are passing the tenant and service as parameters in the command and using –no-input to avoid prompting for values, relying solely on the responses from the cookiecutter.json file.
We are using -f
to overwrite the content if folder exists, it allow us to creates new files if needed, and -s
to skip render files again if already exists to don’t overwrite if the user has made any change.
You can find more information about it in the official documentation.
Github Actions
Now, let’s use GitHub Actions to establish a self-service process, allowing users to automatically create a pull request with their requirements for new tenants or services.
First, let’s create the user input interface, define the parameters, and configure it as a manually triggered pipeline:
And now, we use these parameters in the workflow execution and automatize the process.
We can create a CODEOWNERS
file set up the PR’s approvers for each tenant. Here more information.
You can find the full code in the repository