18

下面是 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
4

3 回答 3

22

由于这个问题仍然没有很好的答案,我会尝试一下。我使用这个片段为我的存储库中的每个目录启动了多个 Docker 构建。请注意>,它允许您将多行命令放入 YAML ( source )。

Linux 示例:

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

窗口示例:

build:
  stage: loop
  script:
    - >
      setlocal enabledelayedexpansion
      for %%a in ("C:\Test\*.txt") do (
        set FileName=%%~a
        echo Filename is: !FileName!
      )
      endlocal
于 2019-03-30T10:37:53.520 回答
2

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

于 2019-02-08T15:46:08.807 回答
-1

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

for var in ${NAME_1} ${NAME_2} ${NAME_3} ; do
 *----computations----*
done
于 2019-08-16T15:01:10.653 回答