詹金斯管道$ {params.Environment}替代不良

时间:2020-07-16 09:39:14

标签: docker jenkins-pipeline

我试图通过Jenkins管道将命令发送到docker运行的docker容器中。 Jenkins机器位于另一台服务器上,而docker映像位于另一台服务器上。 当我对环境参数进行硬编码时,脚本将按预期执行。但是每当我尝试用params替换它时,它都会出错:

bash:$ {params.Environment}错误替换

这是我的管道脚本

pipeline {
    agent any 
       parameters {
          choice(
              name: 'Environment',
              choices: ['qa','dev'],
              description: 'Passing the Environment'
            )
       }
       stages {
          stage('Environment') {
             steps {
                 echo " The environment is ${params.Environment}"
             }
          }
          stage('run') {
             steps {
                sh 'ssh 10.x.x.x \'sudo docker run --name docker_container_name docker_image_name sh -c "cd mytests ; pip3 install -r requirements.txt ; python3 runTests.py -env  ${params.Environment} "\''
             }
         }
      }
    }

1 个答案:

答案 0 :(得分:1)

df.corr('pearson') # 'kendall', and 'spearman' are the other 2 options 命令的参数需要使用双引号或三重双引号。

sh

在管道脚本使用的Groovy语言中,single-quoted strings根本不进行任何插值,因此sh """ssh 10.x.x.x 'sudo docker run --name docker_container_name docker_image_name sh -c "cd mytests ; pip3 install -r requirements.txt ; python3 runTests.py -env ${params.Environment} "'""" 字符串按原样传递给Shell。 Double-quoted strings确实执行插值,因此Groovy引擎在调用Shell之前会替换${params.Environment}

(您可能会看到对Using Docker with Pipeline的本机支持,尽管它要求Jenkins能够在工作程序节点上运行Docker本身,但可以避免使用${params.Environment}包装)。