Jenkinsfile 到 运行 Terraform

Jenkinsfile to run Terraform

学习本教程 https://medium.com/@devopslearning/100-days-of-devops-day-34-terraform-pipeline-using-jenkins-a3d81975730f

我想成为 运行 来自 Jenkins 的 terraform 文件 我已经安装了 Terraform 插件版本 1.0.9 我去创建一个新的管道项目 在管道选项卡上,我选择管道脚本并粘贴以下脚本

node {
env.PATH += ":/opt/terraform_0.7.13/"


 stage ('Terraform Plan') {
 sh 'terraform plan -no-color -out=create.tfplan'
}

// Optional wait for approval
input 'Deploy stack?'

stage ('Terraform Apply') {
sh "terraform --version"
}

这是控制台输出

[Pipeline] {
[Pipeline] stage
[Pipeline] { (Terraform Plan)
[Pipeline] sh
[aws_terraform] Running shell script
+ terraform plan -no-color -out=create.tfplan
/var/lib/jenkins-slave/workspace/ow/ow_eng/aws_terraform@tmp/durable-53622951/script.sh: line 2: terraform: command not found
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: script returned exit code 127
Finished: FAILURE

terraform 二进制文件未安装在执行管道的 jenkins 从站上。必须安装二进制文件才能使插件工作

如果您想为 运行 使用 docker 图片,您可以使用此代码段:

pipeline {
  agent {
    docker {
      image 'hashicorp/terraform:light'
      args '--entrypoint='
    }
  }
  stages {
    stage('Terraform Plan') { 
      steps {
        sh 'terraform plan -no-color -out=create.tfplan' 
      }
    }
    // Optional wait for approval
    input 'Deploy stack?'

    stage ('Terraform Apply') {
      sh "terraform --version"
    }
  }
}

请注意,您需要安装 docker pipeline plugin。这里的技巧是重新定义入口点,因为官方 terraform 图像已经定义了 terraform 可执行文件的入口点。