【问题标题】:How to use jenkins variable as execute command如何使用 jenkins 变量作为执行命令
【发布时间】:2021-02-04 02:30:18
【问题描述】:

        stage('Preparation') {
            steps {
                script{
                    if(Deployment == 'UAT'){
                        goals = 'mvn clean install -Denvironment=uat '
                        script_arg='''shutdown.sh uat | sleep 20 | startup.sh uat'''
                    }
                    else{
                        goals = 'mvn clean install -Denvironment=dev'
                        script_arg='''shutdown.sh dev | sleep 20 | startup.sh dev'''

                    }
                }
            }
        }
        
        stage('Build') {
            steps{
                sh goals
            }
        }
        
        stage('Deploy') {
            steps([$class: 'BapSshPromotionPublisherPlugin']) {
                sshPublisher(
                    continueOnError: false, failOnError: true,
                    publishers: [
                        sshPublisherDesc(
                            configName: "ABC",
                            verbose: true,
                            transfers: [
                                sshTransfer(sourceFiles: "target/abcd.war",
                                removePrefix: "/target", 
                                remoteDirectory: "/remote", 
                                execCommand: '''shutdown.sh uat | sleep 20 | startup.sh uat''')

                            ]
                        )
                    ]
                )
            }
        }

我是詹金斯的新手。我正在尝试设置管道。 我上面的代码工作正常,但我想根据环境 dev/uat 传递执行命令 我在准备阶段创建了变量 script_arg 并希望在部署阶段用作 execCommand

部署 --> 步骤 --> sshPublisher --> 发布者 --> sshPublisherDesc --> 传输

但是当我如下替换它时

execCommand: "${script_arg}" 

这不起作用。

【问题讨论】:

  • 你在哪里添加了这个 execCommand?
  • 部署 --> 步骤 --> sshPublisher --> 发布者 --> sshPublisherDesc --> 转移
  • 我正在用代码'${script_arg}'替换上面的execCommand

标签: jenkins jenkins-pipeline jenkins-plugins jenkins-groovy


【解决方案1】:

尝试在管道外部定义变量,看看,如下所示。

def script_arg

pipeline {
   stages {
        stage() {}
        stage() {}
        ...
   }
}

【讨论】:

  • 我试过但没有运气。我用单引号弄错了吗
  • 我误认为单引号代码需要用作 execCommand:"${script_arg}"
猜你喜欢
  • 2014-08-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-22
  • 2014-04-24
  • 1970-01-01
  • 2021-07-12
  • 1970-01-01
相关资源
最近更新 更多