Pre-requisites:
- Install terraform.
- Configure terraform to use Azure (Refer Azure Docs to create the required parameters using Azure Cloud Shell).
- Create a simple shell script that will act as a source e.g. azure_params.sh.
Instructions on usage:
- create a directory named jenkins_vm_terraform.
- copy the jenkins_vm.tf and customdata.txt into the above directory.
- run cmd: terraform init --> this will ensure terraform has the required plugins as per vendor installed.
- run cmd: terraform plan --> this will give us the info of the changes terraform will make on the azure
- run cmd: terraform apply -auto-approve --> this will start the actual execution of the .tf script to spawn different resources in azure required for running a virtual machine.
- will create resource group, virtual network, subnets, security groups, port configuration etc.
- the most important is the custom installation of packages post re-boot. the optional "custom_data" under os_profile will execute the file customdata.txt.
- the customdata.txt is a configuration file passed to cloud-init in azure this file acts as an instructions to install third party packages post instance boot.
- The above .tf script configures only three ports viz 80(jenkins),8080(npm) and 22 as an inbound to azure virtual machine.
Jenkins is hosted on Azure Ubuntu 16.04 as a container with docker and npm tools installed along with basic plugins of jenkins.
The Jenkinsfile will perform the below steps:
- scm checkout
- npm install --> build using a docker file to create images with tag $JOB_NAME:$JOB_NAME_v_$BUILD_NUMBER
- static code analysis
- push the image to the docker registry --> for versioning
- stop the container, remove the container and create a container with new image --> shell script
- the Jenkinsfile should be checked in the git repo as a template with variables source from a file specific to the git project
- static anaylsis on the npm project
- break the build in case the analysis threshold is breached
- upload new images to the docker registry
- use of kubernetes to deploy the latest build image in a rolling update fashion for HA