【问题标题】:how to concatenate two env variables in the jenkins pipeline with a slash in between?如何在詹金斯管道中用斜线连接两个环境变量?
【发布时间】:2018-05-15 14:23:56
【问题描述】:

我有一个将生成的 RPM 发送到 Nexus OSS 管理器的脚本。 Jenkins 的阶段是:

stage ('nexus deploy') {
  env.REPONAME = "snapshots"
  sh '''
    mvn deploy:deploy-file -Durl="${env.NEXUS_URL}/$env.REPONAME"
    '''
  }

我已经设置了 env 变量 env.NEXUS_URL,但是用两个变量并排调用它,中间有一个斜线,不知何故无法检测到变量,并且构建失败并出现错误。

-Durl="${env.NEXUS_URL}/$REPONAME": bad substitution

【问题讨论】:

  • 你的mvn deploy:deploy-file -Durl="${env.NEXUS_URL}/$env.REPONAME"正在shell上执行,env.NEXUS_URL和env.REPONAME都是无效的shell替换。

标签: jenkins environment-variables concatenation jenkins-pipeline


【解决方案1】:

您混淆了groovy 语法和shell 中的语法。 您可以在groovy 中使用env.VAR,也可以在sh '..' 之间使用${VAR}:

pipeline {
    agent any

    options {
        buildDiscarder(logRotator(numToKeepStr: '3'))
    }

    environment {
        NEXUS_URL = 'https://mynexus.com'
        REPONAME    = 'myrepo'
    }

    stages {
        stage('test') {
            steps {
                echo "print env vars in groovy"
                echo "my nexus is " + env.NEXUS_URL + " any my repo name is " + env.REPONAME
                sh 'echo "env vars in sh"'
                sh 'echo "nexus is ${NEXUS_URL} and my repo name is ${REPONAME}"'
            }
        }
    }
}

输出:

[Pipeline] echo
print env vars in groovy
[Pipeline] echo
my nexus is https://mynexus.com any my repo name is myrepo
[Pipeline] sh
[test] Running shell script
+ echo 'env vars in sh'
env vars in sh
[Pipeline] sh
[test] Running shell script
+ echo 'nexus is https://mynexus.com and my repo name is myrepo'
nexus is https://mynexus.com and my repo name is myrepo

根据您的情况,您需要:

mvn deploy:deploy-file -Durl=${NEXUS_URL}/${REPONAME}

【讨论】:

    猜你喜欢
    • 2016-11-09
    • 2017-09-06
    • 2018-07-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 2011-08-14
    • 1970-01-01
    相关资源
    最近更新 更多