gpt4 book ai didi

jenkins - 如何将变量传递给 JenkinsFiles 中的凭证参数?

转载 作者:行者123 更新时间:2023-12-05 03:51:27 27 4
gpt4 key购买 nike

我正在尝试编写一个 JenkinsFile,它会自动通过 ssh 访问 git repo 并执行一些操作,但我想让 repo 和 ssh key 使用带有存储在 Jenkins 中的 ssh id 的变量,但我似乎丢失了关于如何将变量传递给 Jenkins 文件的 Jenkins 文档,因为我无法将值传递给凭据 key 。传递给 sh 命令的变量解析得很好......

下面的示例管道:

pipeline {
parameters {
string(name: 'SSH_priv', defaultValue: 'd4f19e34-7828-4215-8304-a2d1f87a2fba', description: 'SSH Credential with the private key added to Jenkins and the public key to the username stored in Git Server, this id can be found in the credential section of Jenkins post its creation.')
string(name: 'REPO', defaultValue: 'git@--------------------')
}
stages {
stage ('Output Variables'){
// checks I can get these variables
steps{
sh("echo ${params.SSH_priv}")
sh("echo ${params.REPO}")
}
}

stage('Do Something') {
steps {
// this below commented line, does not work.
// sshagent (credentials: ['${params.SSH_priv}']){

// this line does work
sshagent (credentials: ['d4f19e34-7828-4215-8304-a2d1f87a2fba']){
sh("git clone --mirror ${params.REPO} temp")
dir("temp"){
// start doing fancy stuff ...
....
....
}
}
}
}

目标是我的开发人员同事可以调用的管道,并将使用他们自己的存储库和我没有使用的自己的 ssh id。当我尝试使用传递值的 SSH_priv 参数运行它时,我在 Jenkins 中遇到以下故障。

Failure Output

JenkinsFile 与硬编码的凭据 ID 完美配合——如下所示: Success Output

最佳答案

所以在测试了不同的东西之后,一位 friend 在 5 分钟内解决了这个问题。 Quotation mark types matter in Groovy Script

改变

sshagent (credentials: ['${params.SSH_lower}']){

sshagent (credentials: ["${params.SSH_lower}"]){

解决了问题。

关于jenkins - 如何将变量传递给 JenkinsFiles 中的凭证参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62869661/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com