Part 03 - Publishing Terraform Modules using GitLab Pipelines

Vighnesh Prakash
4 min readJan 29, 2023

--

Having learned how to manually publish modules, let’s delve into using GitLab’s CI/CD to automate the process. If you are unfamiliar with GitLab CI/CD, we recommend reading the “Just Enough Gitlab” article to gain a basic understanding of how it works before proceeding. It provides a comprehensive overview of the necessary concepts.

We will employ the “release flow” strategy to publish our Terraform modules. This strategy involves creating a separate release branch from the main branch, dedicated to preparing and publishing a specific version of the module. This release branch follows semantic versioning, which will be used to version the Terraform module. This approach helps to ensure stability and control over the codebase and makes it easier to implement continuous integration and delivery.

Below are the steps.

1- Adding the .gitlab-ci.yml

If you haven’t already done so, add a “.gitlab-ci.yml” file to the root of your project, and then include the provided script in the file.

.gitlab-ci.yml publish script

Here:

  • The pipeline has a single Stage called publish and a single job called publish_terraform_module
  • Line 6, we are using a docker image that contains the curl command to “upload” the terraform image.
  • Lines 7–10 The rules specified in lines 7–10 determine when the job will be executed. The regex is used to ensure that the job is run only on branches that follow the format “release/x.x.x”, and line 10 specifically denies any other branches from running the job.
  • Lines 11–26 of the script, labeled “before_script,” define and set variables that will be used by the package. These include the location of the package within the runner instance, the name of the Terraform module (configurable but defaulting to the GitLab project name for this demo), the target platform (hardcoded as “aws ”but can be configured), and the version of the Terraform module (obtained from the branch name).
  • Lines 27–34 of the script handle the compression and uploading of the Terraform module. This is identical to what is covered in the previous article.

For more information on publishing and consuming the package refer to https://medium.com/@vighnesh_prakash/publishing-terraform-modules-to-gitlab-infra-registry-a52755ebc712

2- Create the Release Branch

Once this .gitlab-ci.yml file is committed to the main branch, we then create a release branch as shown below

Creating the release branch

where the release number (0.0.1) must follow the semantic versioning

The pipeline started upon release branch creation

As soon as the branch is created, the pipeline related to it is automatically initiated and the jobs within it start to execute.

publish_terraform_module Job Log
Inside the Pipeline
Pipelines Page in Gitlab

Once the package is successfully uploaded to the GitLab Infra registry, the “publish_terraform_module” job is marked as a success, thus completing the pipeline.

Package Uploaded to the Gitlab Infrastructure Registry

Conclusion

By using GitLab CI/CD, the task of packaging and publishing the module is automated. The steps followed by the script are the same as those covered in the manual publishing process but are written in a format that GitLab CI can understand.

Now that we have a solid understanding of GitLab pipelines, we can proceed to use them to generate documentation for our Terraform module directly.

--

--