26

Below is the script mentioned in the gitlab-ci.yml file. This GitLab CI configuration is valid. But, when the CI/CD build is run, the job fails. Is it something to do with the FOR loop syntax?

deploy_dv:
  stage: deploy_dv
  variables:
    GIT_STRATEGY: none
script:
  - echo "Deploying Artifacts..."
  - echo "Configure JFrog CLI with parameters of your Artifactory instance"
  - 'c:\build-tools\JFROG-CLI\jfrog rt config --url  %ARTIFACTORY_WEBSITE% --user %ARTIFACTORY_USER% --apikey %APIKEY%'
  - 'cd ..\artifacts'
  - 'SETLOCAL ENABLEDELAYEDEXPANSION'
  - FOR %%i in (*) do (
        'c:\build-tools\curl\bin\curl.exe --header "PRIVATE-TOKEN:%HCA_ACCESS_TOKEN%" --insecure https://code.example.com/api/repository/tags/%CI_COMMIT_TAG% | c:\build-tools\jq\jq-win64.exe ".release.description" > temp.txt'
         'set /p releasenote=<temp.txt'
         'rem del temp.txt'
         'set mydate=%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%'
         'c:\build-tools\JFROG-CLI\jfrog rt u "%%i" %ARTIFACTORY_ROOT_PATH%/%PROJECT_NAME%/%%i --build-name=%%i --build-number=%BUILDVERSION%  --props releasenote=%releasenote%;releaseversion=%BUILDVERSION%;releasedate=%mydate% --flat=false'
     )

    - '%CURL% -X POST -F token=%REPOSITORY_TOKEN% -F ref=master  -F "variables[RELEASE]=false" -F "variables[PROGRAM]=test" --insecure https://code.example.com/api/repository/trigger'

  only:
  - /^(dv-)(\d+\.)(\d+\.)(\d+)$/

I get this below error:

  $ echo "Deploying Artifacts..."
"Deploying Artifacts..."
$ echo "Configure JFrog CLI with parameters of your Artifactory instance"
"Configure JFrog CLI with parameters of your Artifactory instance"
$ c:\build-tools\JFROG-CLI\jfrog rt config --url  %ARTIFACTORY_WEBSITE% --user %ARTIFACTORY_USER% --apikey %APIKEY%
Artifactory server ID [Default-Server]: $ cd ..\artifacts
$ SETLOCAL ENABLEDELAYEDEXPANSION
$ FOR %%i in (*) do ( 'c:\build-tools\curl\bin\curl.exe --header "PRIVATE-TOKEN:%HCA_ACCESS_TOKEN%" --insecure  https://code.example.com/api/repository/tags/%CI_COMMIT_TAG% | c:\build-tools\jq\jq-win64.exe ".release.description" > temp.txt' 'set /p releasenote=<temp.txt' 'rem del temp.txt' 'set mydate=%DATE:~6,4%-%DATE:~3,2%-%DATE:~0,2%' 'c:\build-tools\JFROG-CLI\jfrog rt u "%%i" %ARTIFACTORY_ROOT_PATH%/%PROJECT_NAME%/%%i --build-name=%%i --build-number=%BUILDVERSION%  --props releasenote=%releasenote%;releaseversion=%BUILDVERSION%;releasedate=%mydate% --flat=false' )
The filename, directory name, or volume label syntax is incorrect.
ERROR: Job failed: exit status 255
user2301
  • 1,857
  • 6
  • 32
  • 63
  • 1
    This seems to be more a problem with the script itself and not related to GitLab CI, which is just a wrapper around the script. I was looking for a loop solution within GitlLab and ended up here. – ddorsogna Mar 14 '22 at 20:11

3 Answers3

30

Since there is still no good answer to this question, I will give it a try. I used this snippet to start multiple Docker builds for every directory in my repository. Notice the |+ and the > characters, which lets you put multi-line commands in YAML and are part of GitLab syntax.

Linux example:

build:
  stage: loop
  script:
    - |+
      for i in $(seq 1 3)
      do
        echo "Hello $i"
      done

Windows example:

build:
  stage: loop
  script:
    - >
      setlocal enabledelayedexpansion
      for %%a in ("C:\Test\*.txt") do (
        set FileName=%%~a
        echo Filename is: !FileName!
      )
      endlocal
Cloudkollektiv
  • 11,852
  • 3
  • 44
  • 71
  • 13
    This may do what the OP wants to do, but let me still add a warning here: This executes the whole for loop as one CI script, so Gitlab will only see the return code of the entire loop. In bash, a for loop has the return code of the last statement of the last iteration, so **the CI target will only fail if that last statements fails and hide failures in any other part of the loop**. In many cases, this seems way too brittle to be worth doing in CI... – ingomueller.net Jun 20 '20 at 10:21
  • @ingomueller.net would it be ok if there was a `set -e` at the beginning? or adding an `|| exit 1` at the end of every command. – HosseyNJF Nov 17 '20 at 07:27
  • @HosseyNJF Yes, I think both solutions should work. – ingomueller.net Nov 17 '20 at 19:52
  • 1
    @Nebulastic I wasted almost day on this, thanks mate this helped alot – Mohammed Ali Nov 18 '20 at 07:43
  • I am runnning the exact same thing, but it only works for the 1st iteration, and stops once its done – Mohammed Ali Dec 11 '20 at 12:31
  • Looks like the OP is configuring a Gitlab runner for Windows while you provide an example for a Linux bash. – bjhend Apr 26 '21 at 19:06
3

Here is a working example of a job in a .gitlab-ci with a loop running on GNU/Linux OS and using Sh/Bash shell :

edit:
  stage: edit
  script:
     - for file in  $(find ${CI_PROJECT_DIR} -type f -name deployment.yml)
       do 
         CURRENT_IMAGE=$(grep "image:" $file | cut -d':' -f2- | tr -d '[:space:]' | cut -d':' -f3)
         sed -ie "s/$CURRENT_IMAGE/$VERSION/g" "$file"
       done
  only:
     - master

I'm not an expert on Gitlab-Runner on Windows but Windows Batch is default shell used but you can also use Powershell.

Nicolas Pepinster
  • 5,413
  • 2
  • 30
  • 48
-1

In .gitlab.yml anything you write under "script" is shell. Thus for loop will be same as it works in shell script.

for var in ${NAME_1} ${NAME_2} ${NAME_3} ; do
 *----computations----*
done