1

I want to write a while loop in a GitLab CI file and here is the syntax that I've tried but seems to not be working.

Is the while loop authorized in GitLab or YAML files? Or are there other ways to write it?

Here is where I used it:

  - while ($(curl -X GET ${URL} | jq -r '.task.status') != "SUCCESS")
   ANALYSIS_ID=$(curl -X GET ${URL} | jq -r '.task.analysisId')
jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
MadHer
  • 91
  • 3
  • 11
  • 1
    Possible duplicate of [In YAML, how do I break a string over multiple lines?](https://stackoverflow.com/questions/3790454/in-yaml-how-do-i-break-a-string-over-multiple-lines) – jonrsharpe May 27 '19 at 16:45
  • Not really as I can read – MadHer May 27 '19 at 18:44
  • Gitlab CI just runs the script as bash/sh. So what you really need is to look on how to write a bash while loop and then how to break it over multiple lines. – Jakub Kania May 28 '19 at 10:47

2 Answers2

2

Why don't you write yourself a shell/python/whatever script and just run it from the CI?

YAML is not the suitable language to perform such a things (e.g. while loops, large conditions, for loops) and should not be used that way...

Václav
  • 990
  • 1
  • 12
  • 28
  • 1
    Yes I had this idea , I will try to – MadHer May 27 '19 at 22:35
  • 1
    Do that. Doing loops and other complex things correctly is nearly impossible in YAML. Why taking the hard way, when 5 lines of shell will do it easily. – Václav May 27 '19 at 22:40
  • I did it I will test it in real and reply here after – MadHer May 28 '19 at 01:36
  • Please note that the loop is not written in actual YAML, YAML is used here just to store a string and that string is bash/sh/powershell depending on what is configured. – Jakub Kania May 28 '19 at 10:44
  • @JakubKania yes, I know, but still - executing shell loop/conditions commands directly from `script` in yaml is not the best practice. – Václav May 28 '19 at 11:23
  • @Václav what if I want to use include:file, script seems not be included together with yml file, then have to put script inside yml file – Jack.W Apr 19 '20 at 01:58
  • 1
    @Jack.W can u explain more what u want to do ? – MadHer Apr 21 '20 at 09:56
  • @MadHer define a common used gitlab ci yml file to let other projects to `include` the common one into their own repo. if also want to separate bash script from yml file, seems not work as expected. – Jack.W May 07 '20 at 10:37
0

So I did this to resolve my issue , its to create a script in which I ve wrote the loop while and this script return the value that I needed, and then I called this script in my gitlab_ci file as below :

  - ANALYSIS_ID=$(**./checkUrl.sh** $URL)

And if needed as an example the script that I used

  #!/bin/bash
    success="SUCCESS"
    condition="$(curl -X GET "$1" | jq -r '.task.status')"

    while [ "$condition" != "$success" ]
    do
    ANALYSIS_Id="$(curl -X GET "$1" | jq -r '.task.analysisId')"
    done
    return "$ANALYSIS_Id"
MadHer
  • 91
  • 3
  • 11