0
votes

I installed jenkins in local windows machine. Then i installed terraform plug in and did the config changed in global tool configuration in jenkins but when i run the jenkin pipeline i get 'terraform' is not recognized as an internal or external command, operable program or batch file.

Code :

pipeline {
   agent any

   stages {
      stage('Hello') {
         steps {
            bat 'terraform --version'
            echo 'Hello World'
         }
      }
   }
}

can you help me what i am doing wrong in this?

Started by user admin
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in C:\Program Files (x86)\Jenkins\workspace\actimize2
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Hello)
[Pipeline] script
[Pipeline] {
[Pipeline] tool
[Pipeline] }
[Pipeline] // script
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
ERROR: No tool named terraform found
Finished: FAILURE

terraform config : enter image description here

1
You should probably look into using the Docker Terraform image as an agent.Matt Schuchard

1 Answers

0
votes

You'll need to get the Terraform home using a tool command and then add it to the Path environment variable so that the shell interpreterinvoked by bat can find the terraform command:

def tfHome = tool name: 'Terraform', type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool'
env.Path = "${tfHome};${env.Path}"

In your pipeline, this would look like:

pipeline {
   agent any

   stages {
      stage('Hello') {
         steps {
            def tfHome = tool name: 'Terraform', type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool'
            env.Path = "${tfHome};${env.Path}"
            bat 'terraform --version'
            echo 'Hello World'
         }
      }
   }
}

You can also use tool directly in the bat command (this is what I used to do when I was using Jenkins regularly):

pipeline {
   agent any

   stages {
      stage('Hello') {
         steps {
            bat "${tool name: 'Terraform', type: 'com.cloudbees.jenkins.plugins.customtools.CustomTool'}\terraform --version"
            echo 'Hello World'
         }
      }
   }
}

You can see a worked example in this Automating Terraform Projects with Jenkins article.