Integrate Terraform with Jenkins Pipeline - Organize the Terraform templates

2021年06月13日


More preferable, we could move the pipeline definition script from the Jenkins pipeline inline editor into the repo, making the IaC templates under better source code control.

TerraformJenkins]$ vim Jenkinsfile

pipeline {
  agent any
  tools {
      terraform "Terraform1.0.0"
  }

  stages {
    stage('Git Checkout') {
      steps {
        git credentialsId: '16**-**-**-**-**cb', url: 'https://git-codecommit.us-west-2.amazonaws.com/v1/repos/TerraformJenkins'
      }
    }

    stage('Terraform Init') {
      steps {
        sh label: '', script: 'terraform init'
      }
    }
    
    stage('Terraform apply') {
      steps {
        sh label: '', script: 'terraform apply --auto-approve'
      }
    }
  }
}


TerraformJenkins]$ git add .

TerraformJenkins]$ git commit -m "Add Jenkinsfile"
[master 84c053f] Add Jenkinsfile
 1 file changed, 26 insertions(+)
 create mode 100644 Jenkinsfile

TerraformJenkins]$ git push origin master
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 537 bytes | 0 bytes/s, done.
Total 3 (delta 1), reused 0 (delta 0)
To https://git-codecommit.us-west-2.amazonaws.com/v1/repos/TerraformJenkins
   78ea96c..84c053f  master -> master

TerraformJenkins]$ tree
.
├── Jenkinsfile
├── modules
│   └── ec2
│       ├── main.tf
│       └── variables.tf
├── production.tf
├── provider.tf
└── variables.tf

2 directories, 6 files

Pipeline Script From SCM
Pipeline supports fetching the DSL (Domain Specific Language) script from the SCM. Typically, this file is called Jenkinsfile and located in the root of the project.

Select "Pipeline script from SCM" from the Definition.
Select Git as SCM
Git URL to your repo. Take this URL from Github. It should be the format of git@github.com:{username}/{repo}.git
Credentials: Select the one you created before.
Branches to build: */master




Build Now

Category: Jenkins Tags: public

Upvote


Downvote