user17970
user17970

Reputation: 503

Jenkinsfile to run Terraform

following this tutorial https://medium.com/@devopslearning/100-days-of-devops-day-34-terraform-pipeline-using-jenkins-a3d81975730f

I want to be run a terraform file from Jenkins I have installed Terraform plugin version 1.0.9 I go create a new pipeline project on the pipeline tab I choose pipeline script and paste the below script

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"
}

This is the console output

[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

Upvotes: 4

Views: 6955

Answers (2)

S.Spieker
S.Spieker

Reputation: 7355

If you would like to use a docker image for running this you can use this snippet:

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"
    }
  }
}

Be aware that you will need to install the docker pipeline plugin. The trick is here to redefine the entrypoint because the official terraform image already defines an entrypoint to the terraform executable.

Upvotes: 1

StephenG
StephenG

Reputation: 2881

The terraform binary is not installed on the jenkins slave that is executing the pipeline. The binary must be installed to have the plugin work

Upvotes: 2

Related Questions