dark

Jenkins to manage a libvirt infrastructure with Terraform

blank
blank

Jenkins is an open source automation server which provides hundreds of plugins to build, deploy and automate projects.

Terraform codifies cloud, virtualization and many other APIs into declarative configuration files, allowing to define the infrastructure as code.

The combination of both is an excellent platform to manage resources in the Microsoft Azure cloud in an automated or semi-automated way.

Installation

Let’s start by installing Jenkins and Terraform. The official documentation at https://www.jenkins.io/doc/book/installing/linux/#debianubuntu describes how to install the Jenkins binaries on a Debian/ Ubuntu system, so we’ll not go further into details on that.

Once the required packages are installed as described above, go to http://localhost:8080/ to finalize the Jenkins setup. After installing the essential plugins required to run Jenkins, you should be directed to the following screen:

blank
Home screen

Next, install the required plugins.

Go the main dashboard and then click on “Manage Jenkins” -> “Manage Plugins“. In the “Available” tab, search for:

  • Azure Credentials
  • Terraform
  • AnsiColor
  • Git plugin

Check the checkbox of the required plugins and install them. Jenkins will restart at the end of the installation.

Pipeline Setup

After all plugins have been installed, the next step will be to add a “New Item” (see the menu on the right side).

blank
Add a new item

Start by entering a valid name for the job and choose the type “Pipeline“. Hit “OK” to continue to the next screen.

blank

Enter a description for the pipeline and scroll down to the “Pipeline” section and choose in the “Definition” section the option “Pipeline script from SCM“. Choose the SCM you want to use and fill in the repository URL of the code repository containing the Jenkinsfile pipeline file.

blank

A bit further down it is possible to configure the SCM branch name to work on and the actual filename of the Jenkinsfile, which in most cases will be called just “Jenkinsfile“.

blank

Once all options have been set, press “Save” to save the configuration.

Depending on whether the pipeline was created with or without parameters, either click on “Build” or “Build with Parameters” to start the pipeline.

blank

Each build will appear in the “Build History” and contains links to for instance the “Console Output“, which contains the text output of all stages, steps and plugins executed by the pipeline.

blank

Each build/ run contains information such as:

  • Who started the pipeline
  • The current SCM repository versions (git commit hash)
  • An SCM log of the changes since the last build (git commit messages)
  • Who approved the pipeline (if applicable)
  • The console output of the plugins/ commands executed
blank

The pipeline dashboard page, contains a stage view of pipeline runs, divided on the stages and their execution status.

blank

Pipeline definitions

Finally, let’s have a look on how this pipeline file is actually built.

blank
Jenkinsfile

The above Jenkinsfile is divided in 4 sections:

  • the agent section
  • the tools section
  • the environment section
  • the stages section

The first 3 sections basically set some build options like:

  • on which agent to run the pipeline (in this case, on “any” available node)
  • which tools are required to be present on the build node
  • which environment (shell) parameters should be set

The stages section contains the different stages and steps the pipeline is supposed to run.

Each stage, contains several steps separated per line:

blank

In the above example, all steps are defined between the ‘steps { }’ block. Steps can be wrapped (enclosed between curly brackets), like in the above example line 15 calls the ansiColor() plugin to display a colorized output of the steps contained by it.

Next, the plugin dir() wraps the rest of the steps, which means that all enclosed steps will be executed in a specific directory.

In that specific directory, 3 plugins will be executed:

  • git: line 17 in the above example will git clone the main branch of the supplied Github URL
  • echo: line 19 will output some text
  • sh: line 20 – 23 defines the shell commands to be executed

A complete example can be found at: https://github.com/insani4c/jenkins-terraform-datacenter-example/blob/main/JenkinsFile

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Previous Post
blank

Using multipath together with mdadm on Debian

Next Post
blank

A monitoring solution with Docker

Related Posts