Install Jenkins using Docker and Terraform on Ubuntu in Azure Cloud
I’d like to install Jenkins based on docker image in Azure Cloud with all Resources using Terraform, install Docker, Git, Jenkins on Ubuntu Server and Standard DS1 v2 Virtual Machine automatically with bash and groovy scripts.
You should have generated SSH keys, installed git, terraform, az on your local computer.
Connect to Azure
You may configure authentication to Azure in several ways using some parameters like Subscription ID, Tenant ID etc. for automatic tasks. It could be with Active Directory, with certificates or with secrets. I use terraform on a local machine, so I use CLI:
az login
You can see information about your subscription and change it:
az account list
az account seе --subscription="SUBSCRIPTION_ID"
Download scripts
Install scripts from GitHub on your local computer:
mkdir jenkins
cd jenkins
git clone https://github.com/sergeyanover/jenkins-docker-terraform-azure.git
cd jenkins-docker-terraform-azure
SSH keys
Generate SSH public and private keys with puttygen.exe and save them in the folder “keys”. Edit file terraform.tfvars in folder “terraform” and put your public key like “ssh-rsa …” into it as a value of my_public_key. In addition, you should set your current IP address or your network in ip_admin.
Terraform is running
Just run terraform and it install all Resources, make updates and install packets. I use Ubuntu 18.04 and you may find names (sku) of others in the Region:
az vm image list-skus --location eastus --publisher Canonical --offer UbuntuServer --output table
If you need several Jenkins machines, just set the parameter count_vm in terraform.tfvars to the number of machines, by default that’s 1.
cd terraform
terraform init
terraform plan -out main.tfplan
terraform apply main.tfplan
terraform plan
Take some time and open Jenkins in a browser: https//xx.xx.xx.xx:8080 with a temporarily pair “admin — password” which you should change later.
References
https://learn.hashicorp.com/collections/terraform/azure-get-started
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/virtual_machine_extension
https://registry.terraform.io/providers/hashicorp/template/latest/docs/data-sources/file
https://docs.microsoft.com/en-us/azure/developer/terraform/create-linux-virtual-machine-with-infrastructure
https://docs.microsoft.com/en-us/azure/virtual-machines/generation-2
https://docs.microsoft.com/en-us/azure/virtual-machines/dv2-dsv2-series
https://docs.microsoft.com/en-us/azure/virtual-machines/disks-types