【问题标题】:Terraform is not recognized as internal or external command in jenkinsTerraform 在詹金斯中未被识别为内部或外部命令
【发布时间】:2020-05-30 21:58:53
【问题描述】:

我在本地 Windows 机器上安装了 jenkins。然后我安装了 terraform 插件,并在 jenkins 的全局工具配置中更改了配置,但是当我运行 jenkin 管道时,我得到“terraform”不被识别为内部或外部命令, 可运行的程序或批处理文件。

代码:

pipeline {
   agent any

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

你能帮我看看我做错了什么吗?

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

地形配置:

【问题讨论】:

  • 您可能应该考虑使用 Docker Terraform 图像作为agent

标签: windows jenkins continuous-integration jenkins-pipeline terraform


【解决方案1】:

您需要使用工具命令获取 Terraform 主页,然后将其添加到 Path 环境变量中,以便 bat 调用的 shell 解释器可以找到 terraform 命令:

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

在您的管道中,这看起来像:

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

你也可以直接在bat命令中使用tool(这是我以前经常使用Jenkins时的做法):

pipeline {
   agent any

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

您可以在这篇 Automating Terraform Projects with Jenkins 文章中看到一个工作示例。

【讨论】:

  • 我收到这个“错误:没有找到名为 Terraform 的 com.cloudbees.jenkins.plugins.customtools.CustomTool”,知道吗?
  • 您需要先在 Jenkins 服务器上安装和配置 Terraform 工具:“将 Terraform 添加到 Jenkins 服务器就像添加自定义工具一样简单。首先,转到管理 Jenkins | 全局工具Jenkins 服务器上的配置屏幕(适用于 v2.x)。在自定义工具下,选择自定义工具安装...按钮,然后添加自定义工具。”来自链接的文章。
猜你喜欢
  • 2018-04-28
  • 2021-12-17
  • 2014-04-29
  • 1970-01-01
  • 1970-01-01
  • 2013-10-06
  • 2013-10-11
  • 2013-01-25
  • 2016-10-01
相关资源
最近更新 更多