jenkins - 如何在 jenkins 管道中连接两个 env 变量,中间有一个斜线?

标签 jenkins environment-variables concatenation jenkins-pipeline

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

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

我已经设置了环境变量 env.NEXUS_URL但是用两个变量并排调用它,中间有一个斜线,不知何故没有检测到变量,并且构建失败并出现错误。
-Durl="${env.NEXUS_URL}/$REPONAME": bad substitution

最佳答案

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

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}

关于jenkins - 如何在 jenkins 管道中连接两个 env 变量,中间有一个斜线?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47594084/

相关文章:

authentication - Jenkins : 'Trigger Builds Remotely' 带有 ? token 在使用矩阵授权策略时不起作用

c# - 如何引用C :\Users\Public directory programmatically in C#

mysql - 在 MySQL 中选择连接子字符串并替换

mysql - CONCAT 周围的 GROUP_CONCAT 给出错误 - 组函数的使用无效

maven - 全局MAVEN_OPTS似乎不适用于 Jenkins

jenkins - 更改 Jenkins 报告的颜色

jenkins - 设置 Jenkins 来监控外部作业

ubuntu - 如何将环境变量传递给 systemd 启动的服务

windows - 删除环境变量不起作用

javascript - 用数组连接 2 个对象并删除重复项 (js)