【问题标题】:while loop inside gitlab yaml pipelinegitlab yaml管道内的while循环
【发布时间】:2021-07-01 06:13:00
【问题描述】:

我的 .yml 文件的脚本部分应该有一个多行命令(while ... do script)。它基本上应该将文件从当前提交复制到 docker 映像中的特定位置。

由于某种原因,管道抛出错误。见下文。

下面是我的 .gitlab-ci.yml 文件的内容

.gitlab-ci.yml

stages:
  - transform
    
Transform:
  stage: transform
  image:
    name: some-ubuntu-image
    entrypoint: [""]
  except:
    - master
  script:
    - mkdir /opt/input-files
    
    - |
      i=0
      while read line
      do
        array[ $i ]="$line"
        cp -p --parents "$line" /opt/input-files/
        ((i++))
      done < <(git diff-tree --no-commit-id --name-only -r $CI_COMMIT_SHORT_SHA);

    - echo "Displaying copied files"
    - ls -a /opt/input-files/

作业错误:

$ mkdir /opt/input-files
$ i=0
$ while read line; do # collapsed multi-line command
Cleaning up file based variables
00:01
ERROR: Job failed: exit code 1

【问题讨论】:

    标签: bash gitlab yaml sh gitlab-ci


    【解决方案1】:

    想象它是一条线。

    然后添加换行符。

    你可以写:

     i=0; while read line; do array[i]="$line"; cp -p --parents "$line" /opt/input-files/;  ((i++)); done < <(git diff-tree --no-commit-id --name-only -r $CI_COMMIT_SHORT_SHA);
    

    或使用换行符:

     i=0;
     while read line; do
        array[i]="$line";
        cp -p --parents "$line" /opt/input-files/;
        ((i++));
    done < <(git diff-tree --no-commit-id --name-only -r $CI_COMMIT_SHORT_SHA);
    

    就在 gitlab 内部:

    script:
    - i=0;
      while read line; do
        array[i]="$line";
        cp -p --parents "$line" /opt/input-files/;
        ((i++));
      done < <(git diff-tree --no-commit-id --name-only -r $CI_COMMIT_SHORT_SHA);
    

    【讨论】:

      猜你喜欢
      • 2014-08-06
      • 2020-03-21
      • 1970-01-01
      • 1970-01-01
      • 2016-04-24
      • 2020-01-21
      • 2021-12-09
      • 2016-03-14
      • 2011-06-19
      相关资源
      最近更新 更多