Jas*_*000 1 parameters multilinestring jenkins jenkins-pipeline
我对 Jenkins 相当陌生,这是我第一次设置参数。我有多个帐户,我想为其运行相同的代码,以减少代码行。
我有 5 个帐户,它们在其 URL 中使用相同的约定:
export PROFILE=account_one
Run Code Online (Sandbox Code Playgroud)
export PATH="http://account_one/this-is-just-an-example/stack-overflow"
Run Code Online (Sandbox Code Playgroud)
而 1 个帐户不遵循此约定:
export PROFILE=account_two
Run Code Online (Sandbox Code Playgroud)
export PATH="http://second_account/this-is-just-an-example/stack-overflow"
Run Code Online (Sandbox Code Playgroud)
无论如何,我已经创建了一个名为“account”的多行字符串参数,并且我已经输入了所有 5 个帐户名称的值。这就是我现在想要做的:
if [ "${account}" = ???]
then
export PROFILE=${account}
export PATH="http://${account}/this-is-just-an-example/stack-overflow"
python3 run_code.py
elif [ "${account}" = "account_two" ]
export PROFILE="account_two"
export PATH="http://second_account/this-is-just-an-example/stack-overflow"
python3 run_code.py
fi
Run Code Online (Sandbox Code Playgroud)
首先,我想确保我这样做是正确的,因为我以前从未在 Jenkins 中使用过任何参数化。
其次,我也不熟悉 Bash,所以我不确定这里的语法。
最后,这更像是一个编程问题,我不确定在第一个if语句中该输入什么内容。我所尝试的是将 if 语句中的 aws_account 设置为多行字符串参数中的每个值。如:
if [ "${account}" = "account_one"] || [ "${account}" = "account_three"]
Run Code Online (Sandbox Code Playgroud)
虽然,每个帐户都有不同的名称,并且需要不同的路径,即 account_one 的路径是“ http://account_one/this-is-just-an-example/stack-overflow ”,所以我不希望 account_three 有路径“ http://account_one/this-is-just-an-example/stack-overflow ”。
我知道我的理解参差不齐,但 Jenkins 没有最好的例子,大多数是互联网上未回答的问题。请帮忙。
编辑:我创建了两个多行字符串参数,一个用于遵循文件格式的帐户,另一个用于不遵循文件格式的帐户。我决定为每个帐户使用 for 循环:
for i in "${account[@]}"; do
export PROFILE=${i}
export PATH="http://${i}/this-is-just-an-example/stack-overflow"
python3 run_code.py
done
for i in "${account_other[@]}"; do
export PROFILE=${i}
export PATH="http://${i}/this-is-just-an-example/stack-overflow"
python3 run_code.py
done
Run Code Online (Sandbox Code Playgroud)
这样对吗?
您可以使用单个多行字符串参数。让我们假设你已经分配的值account_one通account_six到名为多行字符串参数aws_accounts。
echo "${aws_accounts}"
account_one
account_two
account_three
account_four
account_five
account_six
Run Code Online (Sandbox Code Playgroud)
然后,您可以if-else在for循环中使用语句来设置环境。
for account in "${aws_accounts}"; do
export PROFILE=${account}
if [ "${account}" = 'account_two' ]; then
export PATH="http://second_account/this-is-just-an-example/stack-overflow"
else
export PATH="http://${account}/this-is-just-an-example/stack-overflow"
fi
python3 run_code.py
done
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10219 次 |
| 最近记录: |