简体   繁体   English

在 Jenkinsfile 的 shell 命令中为 Jenkins 管道使用参数

[英]Using parameters within a shell command in Jenkinsfile for Jenkins pipeline

I want to use defined parameters in Jenkinsfile in several shell commands, but I get an exception.我想在多个 shell 命令中使用Jenkinsfile中定义的参数,但出现异常。 In my example I want to execute a simple docker command.在我的示例中,我想执行一个简单的 docker 命令。 The parameter defines the path to docker executable.该参数定义了 docker 可执行文件的路径。

This is my very short Jenkinsfile :这是我很短的Jenkinsfile

pipeline {
  agent any

  parameters {
    string(defaultValue: '/Applications/Docker.app/Contents/Resources/bin/docker', description: '', name: 'docker')
  }

  stages {
    stage('Test') {
      steps {
        sh 'sudo ${params.docker} ps -a'
      }
    }
  }
}

And I get the following exception:我得到以下异常:

[e2e-web-tests_master-U4G4QJHPUACAEACYSISPVBCMQBR2LS5EZRVEKG47I2XHRI54NCCQ] Running shell script
/Users/Shared/Jenkins/Home/workspace/e2e-web-tests_master-U4G4QJHPUACAEACYSISPVBCMQBR2LS5EZRVEKG47I2XHRI54NCCQ@tmp/durable-e394f175/script.sh: line 2: ${params.docker}: bad substitution

When I change the Jenkinsfile without using the paramter inside the shell command it passes successfully:当我在不使用 shell 命令中的参数的情况下更改Jenkinsfile ,它成功通过:

pipeline {
  agent any

  parameters {
    string(defaultValue: '/Applications/Docker.app/Contents/Resources/bin/docker', description: '', name: 'docker')
  }

  stages {
    stage('Test') {
      steps {
        sh 'sudo /Applications/Docker.app/Contents/Resources/bin/docker ps -a'
      }
    }
  }
}

So, how can I use parameters inside a shell command in Jenkinsfile?那么,如何在 Jenkinsfile 的 shell 命令中使用参数? I tried string and text as parameter types.我尝试将stringtext作为参数类型。

The issue you have is that single quotes are a standard java String.您遇到的问题是单引号是标准的 Java 字符串。

Double quotes are a templatable String, which will either return a GString if it is templated, or else a standard Java String.双引号是一个可模板化的字符串,如果它是模板化的,它将返回一个 GString,否则将返回一个标准的 Java 字符串。

So it you use double quotes:所以你使用双引号:

  stages {
    stage('Test') {
      steps {
        sh "sudo ${params.docker} ps -a"
      }
    }
  }

then params.docker will replace the ${params.docker} inside the 'sh' script in the pipeline.然后 params.docker 将替换管道中“sh”脚本内的 ${params.docker}。

If you want to put " inside the "sudo ${params.docker} ps -a" it doesn't work like bash (which is confusing) you use java style escaping, so "sudo \\"${params.docker}\\" ps -a"如果你想把"放在"sudo ${params.docker} ps -a"它不像 bash 那样工作(这令人困惑)你使用 java 风格的转义,所以"sudo \\"${params.docker}\\" ps -a"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Jenkins 管道:使用在 shell 1 中创建的变量,在 shell 2 中 - Jenkins pipeline : using variable created in shell 1, in shell 2 Jenkins 管道中的 Shell 脚本 cd 命令给出错误“没有这样的文件或目录” - Shell script cd command in Jenkins pipeline is giving error "no such file or directory" 如果相同的 shell 命令位于 Jenkins 管道的不同阶段,则其行为会有所不同 - The same shell command behaves differently if it is located in different stages of Jenkins pipeline Jenkins管道 - shell命令杀死了一个大目录 - Jenkins pipeline - shell command killed zipping a large directory 如果使用jenkinsfile(jenkins管道)在控制台日志中找到文本,则使构建版本不稳定 - Make build UNSTABLE if text found in console log using jenkinsfile (jenkins pipeline) 如何在 Jenkins 管道脚本中使用 source 命令 - How to use source command within Jenkins pipeline script 在 jenkins 声明性管道 cloudformation 中使用带有 shell 命令的 If 语句 - Using If statement with shell commands in jenkins Declarative pipeline cloudformation 如何将变量从 Jenkinsfile 传递给 shell 命令 - How to pass variables from Jenkinsfile to shell command 使用管道作为代码在 jenkins master 上找不到返回的 shell 脚本 - shell script returning not found on jenkins master using pipeline as code 在 YML 管道中使用 shell 脚本更改 Jenkins 中的目录 - Change directory in Jenkins using shell script in YML pipeline
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM