Maria
Maria

Reputation: 71

Bash script for Gitlab CI

I have a bash script that will look into the list of jobs running on the pipeline and search for 2 specific jobs. If job A is a match, I want to run one part of the script that in some conditions will retry job B, if job B is found, do another part of the script that will retry job A. In the solution I have now I do the search before I have the if statements, and that means both if conditions are met and the hole script will run. I don't know how to change my condition to avoid that... I am stuck so any suggestion would be awesome. Here is a sketch of the script:

keyWord_A="jobA"
keyword_B="jobB"

# get the job_list with curl command
# search the job_list with jq for 
#            keyWord_A and store the result in match_A 
#            keyword_B and store the result in match_B

if [[ keyWord_A == match_A ]] ; then
#   run this code

if [[ keyWord_B == match_B ]] ; then
#   run this code

Upvotes: 1

Views: 908

Answers (2)

Maria
Maria

Reputation: 71

The solution was simpler than I thought... I can get from the CI the name of the curent job. This way, I can use the name of the current job in my if conditions and get the desired output from the script😊:

keyWord_A="jobA"
keyword_B="jobB"

if [[ keyWord_A == CI_job ]] ; then
#   run this code

if [[ keyWord_B == CI_job ]] ; then
#   run this code

Upvotes: 1

VonC
VonC

Reputation: 1323145

Since you know if both job A and B are found, you can add conditions to your test, or use a simple else if in order to execute one, or the others, but not both:

if [[ keyWord_A == match_A ]] ; then
#   run this code

elif [[ keyWord_B == match_B ]] ; then
#   run this code
fi

Here, if both conditions are met, only the first if will be executed. Not both.

Upvotes: 0

Related Questions