【问题标题】:GitLab CI syntax to write FOR loop statement?GitLab CI 语法编写 FOR 循环语句?
【发布时间】:2019-02-22 13:59:48
【问题描述】:

以下是 gitlab-ci.yml 文件中提到的脚本。此 GitLab CI 配置有效。但是,当 CI/CD 构建运行时,作业会失败。它与 FOR 循环语法有关吗?

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+)$/

我收到以下错误:

  $ 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

【问题讨论】:

    标签: gitlab yaml jq gitlab-ci jfrog-cli


    【解决方案1】:

    由于这个问题还没有很好的答案,我会试一试。我使用这个 sn-p 为我的存储库中的每个目录启动多个 Docker 构建。请注意&gt;,它允许您将多行命令放入 YAML (source)。

    Linux 示例:

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

    Windows 示例:

    build:
      stage: loop
      script:
        - >
          setlocal enabledelayedexpansion
          for %%a in ("C:\Test\*.txt") do (
            set FileName=%%~a
            echo Filename is: !FileName!
          )
          endlocal
    

    【讨论】:

    • 这可能会做OP想要做的事情,但我还是在这里添加一个警告:这会将整个for循环作为一个CI脚本执行,因此Gitlab只会看到整个循环的返回码.在 bash 中,for 循环具有最后一次迭代的最后一条语句的返回码,因此只有在最后一条语句失败并在循环的任何其他部分隐藏失败时,CI 目标才会失败。在许多情况下,这似乎太脆弱了,不值得在 CI 中做......
    • @ingomueller.net 如果开头有set -e 可以吗?或在每个命令的末尾添加|| exit 1
    • @HosseyNJF 是的,我认为这两种解决方案都应该有效。
    • @Nebulastic 我在这上面浪费了差不多一天,谢谢伙计,帮了大忙
    • 我正在运行完全相同的东西,但它只适用于第一次迭代,一旦完成就停止
    【解决方案2】:

    在 .gitlab.yml 中,您在“脚本”下编写的任何内容都是 shell。因此 for 循环将与它在 shell 脚本中的工作方式相同。

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

    【讨论】:

      【解决方案3】:

      这是一个在 .gitlab-ci 中运行的工作示例,其中一个循环在 GNU/Linux 操作系统上运行并使用 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
      

      我不是 Windows 上 Gitlab-Runner 方面的专家,但 Windows Batch 是默认使用的 shell,但您也可以使用 Powershell

      【讨论】:

        猜你喜欢
        • 2018-06-06
        • 1970-01-01
        • 2019-11-05
        • 2020-10-07
        • 2013-02-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多