This repo consists of :
- Terraform Configuration filesm, please refer to Terraform Configuration Repository to know more about configuration files.
- Jenkinsfile which automates infrastructure provisioning using Terraform.
The script consists of the following:
-
parameters
: Defines input parameters that can be used to customize the pipeline. In this case, a boolean parameter autoApprove is defined with a default value of false. This parameter will be used later to determine if the pipeline should automatically run the apply step or prompt the user for approval. -
environment
: Defines environment variables that will be available to all the steps in the pipeline. This is where the AWS credentials for the Jenkins server are defined.
- Save yourAWS_ACCESS_KEY_ID
&AWS_SECRET_ACCESS_KEY
in your jenkins Server.environment { AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID') AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY') }
-
agent
: Defines the node or agent on which the pipeline will run. In this case, theany
option is used, which allows the pipeline to run on any available node. -
stages
: Represents logical divisions in the pipeline, each stage having one or morecheckout
: This stage checks out the Terraform code from a GitHub repository & removes tf_jenkinsproj dir. everytime the job is run.stage('checkout') { steps { dir('terraform'){ bat 'rmdir /s /q tf_jenkinsProj' bat 'git clone https://github.com/Nikhil-Singh25/tf_jenkinsProj.git' } } }
Plan
: This stage initializes Terraform and generates a plan for infrastructure changes and saving the plan intfplan.txt
filestage('Plan') { steps { bat 'cd terraform' bat 'terraform init' bat 'terraform plan -out tfplan' bat 'terraform show -no-color tfplan > tfplan.txt' } }
Approval
: This stage prompts the user to review the infrastructure changes and approve or reject them. If autoApprove is set to true, the pipeline will automatically run the apply step. If it is set to false, the user will be prompted for approval.stage('Approval') { when { // to conditionally execute `approve` stage if `autoApprove=true` pipeline will run automatically and prompt user if `autoApprove=false` not { equals expected: true, actual: params.autoApprove } } steps { script { def plan = readFile 'terraform/tfplan.txt' input message: "Do you want to apply the plan?", parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)] } } }
Apply
: This stage applies the infrastructure changes by running the terraform apply command with the tfplan file generated in the previous stage.stage('Apply') { steps { bat'cd terraform' bat 'terraform apply -input=false tfplan' } }
-
The entire script is wrapped in a pipeline block, which is the main structure of a Jenkins Pipeline script.
- Create a
pipeline
job - General : Select 'this project is parameterised'-> Boolean Parameter -> Name: "autoApprove -> Description: "Automatically run apply after generating plan?"
- Build Triggers: You can check "GitHub hook trigger for GITScm polling" if you want to use github webhooks
- Pipeline: SCM: Git fill out the "repository URL" and "Branch specifier" accordignly in the end for
Script path :'Jenkinsfile'
save and apply
This repo is created & maintained by-@Nikhil_Singh