Jenkins Multi Line String Parameter,

14,825

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
Share:
14,825
Jason000
Author by

Jason000

Updated on December 17, 2022

Comments

  • Jason000
    Jason000 over 1 year

    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?

    • Dibakar Aditya
      Dibakar Aditya about 4 years
      Do you want to run through all your accounts in a loop in every build or select one account per build?
    • Jason000
      Jason000 about 4 years
      I'd like to run through all my accounts in a loop. Eventually I'm printing out a report and I want all accounts on there.
  • Jason000
    Jason000 about 4 years
    Hello! Thank you so much for your response. I'm still having one issue though. I'm getting all the items in the Multi-line String Param as one long string, i.e., "account_one account_two account_three". It makes me think that the M-L string param does not work like an array. Any thoughts on how to access items individually?
  • Dibakar Aditya
    Dibakar Aditya about 4 years
    You are correct, MLS just facilitates entering the values in new lines as compared to a string parameter, where everything is in a single line. When I had tested, it returned the values in new lines as I had entered. If you still get space separated values, just remove the double quotes around "${aws_accounts}" to make it for account in ${aws_accounts}. That should work.
  • Jason000
    Jason000 about 4 years
    Yep, that fixed my problem. Thank you!
  • FairDinkum82
    FairDinkum82 about 3 years
    I'm trying to User Input multiline parameter to want to use in a loop for vSphere action. The input that I receive is a list of servers.