Jenkinsfile运行Terraform

时间:2019-05-01 00:24:18

标签: jenkins-pipeline terraform jenkins-groovy

遵循本教程 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

2 个答案:

答案 0 :(得分:0)

执行流水线的jenkins从站上未安装terraform二进制文件。必须安装二进制文件才能使插件正常工作

答案 1 :(得分:0)

如果您想使用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可执行文件定义了入口点。