Jenkins Multi Line String Parameter,

Jason000 picture Jason000 · Feb 11, 2020 · Viewed 9.6k times · Source

I'm fairly new to Jenkins, and it's my first time setting up parameters. I have multiple accounts that I want to run the same code for, in an effort to cut down on lines of code.

I have 5 accounts, which use the same convention in their URL:

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

while 1 account does not follow this convention:

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

Anyway, I've created a multi-line string parameter named 'account' and for the value i've entered all 5 account names. This is what i'm trying to do now:

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

First, I want to make sure i'm doing this right, as i've never used any parametrization in Jenkins before.

Second, I'm not familar with Bash either, so i'm not sure about the syntax here.

Lastly, this is more of a programming question, i'm not sure what to put in for the first if statement. What i've tried is setting the aws_account in the if statement to every value in the Multi-Line String Param. Such as:

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

Although, every account has a different name and will need a different PATH, i.e., account_one's path is "http://account_one/this-is-just-an-example/stack-overflow" so I don't want account_three to have the path "http://account_one/this-is-just-an-example/stack-overflow".

I know my understanding is spotty, but Jenkins doesn't have the best examples, most are unanswered questions on the internet. Please help.

EDIT: I created two multi line string parameters, one for the accounts that follow file format and one for the accounts that do not. I've decided to use a for loop for each account:

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

Is this correct?

Answer

Dibakar Aditya picture Dibakar Aditya · Feb 11, 2020

You can make do with a single multi-line string parameter. Let's assume that you have assigned the values account_one thru account_six to the multi-line string parameter named aws_accounts.

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

You can then use an if-else statement inside your for loop to set your environment.

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