linux - 从詹金斯执行shell脚本时如何将变量作为参数传递
问题描述
我在 Jenkins 中有一个管道脚本并执行如下所示的 shell 脚本。
steps {
script {
def creds = com.cloudbees.plugins.credentials.CredentialsProvider.lookupCredentials(
com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class,
Jenkins.instance,
null,
null
);
for (c in creds) {
if(c.id == '9d64a593-d40d-4ba2-9ec8-6bxxxxxxxxx') {
println( ( c.properties.password ? "ID: " + c.id + ", UserName: " + c.username + ", Password: " + c.password : ""))
uname = c.username
pwd = c.password
println("uname - " + uname)
sh './runUIContainerLogin.sh "${uname}" "${pwd}" '
}
}
}
}
最后,当我运行 shell 脚本 runUIContainerLogin.sh 时,尽管我在脚本中分别提到了 'uname' 和 'pwd' 的 $1 和 $2,但变量并没有传递给脚本。我在 shell 脚本中得到 uname 和 pwd 的空值(“”)。
请帮助我获取在 runUIContainerLogin.sh 执行时传递的脚本中的变量值。
解决方案
I would try these steps to help resolve your problem.
- Test what is passed into the sh commandline
- If correct call sh with double quotes and remove the single quotes
- Call the sh commandline with " and reomve the single quotes from varaiables.
sh "./runUIContainerLogin.sh ${uname} ${pwd}"
Example:
def uname= 'Jenkins'
echo "${uname}"
Results in:
Jenkins
Here is some documentation including use cases handling credentials based on setting the username and password.
推荐阅读
- flutter - Flutter:如何从其他 State/Page/Class 访问数据
- python - 你是怎么做到的,所以 on_reaction_add 只在机器人的消息被反应时触发
- node.js - 切换到 Typescript 时的 Socket-io 客户端问题
- java - 在动态 viewpager 片段中创建 recyclerviews
- c# - C# Winforms 中面板与用户控件的性能
- java - 你可以在java中制作一个不同元素的列表而不为它创建一个类吗
- typescript - 打字稿自动完成缩小
- sql - 用于 JSON 数据 unix 时间转换的雪花 SQL 查询
- java - AWS DynamoDB SDK 2.x - 我可以重复使用 DynamoDbMapper 的注释吗?
- python - 列表解包中具有布尔值的 index()