【问题标题】:Jenkins Multi Line String Parameter,詹金斯多行字符串参数,
【发布时间】:2020-05-27 08:54:33
【问题描述】:

我对 Jenkins 还很陌生,这是我第一次设置参数。 我有多个帐户,我想为其运行相同的代码,以减少代码行数。

我有 5 个帐户,它们的 URL 使用相同的约定:

export PROFILE=account_one
export PATH="http://account_one/this-is-just-an-example/stack-overflow"

虽然有 1 个帐户不遵循此约定:

export PROFILE=account_two
export PATH="http://second_account/this-is-just-an-example/stack-overflow"

无论如何,我已经创建了一个名为“帐户”的多行字符串参数,并且我已经输入了所有 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

首先,我想确保我做对了,因为我以前从未在 Jenkins 中使用过任何参数化。

其次,我也不熟悉 Bash,所以我不确定这里的语法。

最后,这更像是一个编程问题,我不确定第一个 if 语句应该输入什么。 我尝试将 if 语句中的 aws_account 设置为多行字符串参数中的每个值。如:

if [ "${account}" = "account_one"] || [ "${account}" = "account_three"]

虽然,每个帐户都有不同的名称,并且需要不同的 PATH,即 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

这对吗?

【问题讨论】:

  • 您想在每次构建中循环运行您的所有帐户,还是每次构建选择一个帐户?
  • 我想循环运行我的所有帐户。最终我打印出一份报告,我希望所有帐户都在那里。

标签: jenkins parameters jenkins-pipeline multilinestring


【解决方案1】:

您可以使用单个多行字符串参数。假设您已将值 account_oneaccount_six 分配给名为 aws_accounts 的多行字符串参数。

echo "${aws_accounts}"
account_one
account_two
account_three
account_four
account_five
account_six

然后您可以在for 循环中使用if-else 语句来设置您的环境。

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

【讨论】:

  • 您好!非常感谢你的回复。我仍然有一个问题。我将多行字符串参数中的所有项目作为一个长字符串,即“account_one account_two account_three”。这让我觉得 M-L 字符串参数不像数组那样工作。关于如何单独访问项目的任何想法?
  • 你是对的,与字符串参数相比,MLS 只是便于在新行中输入值,其中所有内容都在一行中。当我进行测试时,它会在我输入的新行中返回值。如果您仍然得到空格分隔的值,只需删除 "${aws_accounts}" 周围的双引号以使其成为 for account in ${aws_accounts}。那应该可以。
  • 是的,这解决了我的问题。谢谢!
  • 我正在尝试用户输入多行参数以在 vSphere 操作的循环中使用。我收到的输入是服务器列表。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-02-22
  • 2018-06-19
  • 1970-01-01
  • 1970-01-01
  • 2020-06-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多