GitLab CI 的多行 YAML 字符串 (.gitlab-ci.yml) [英] Multiline YAML string for GitLab CI (.gitlab-ci.yml)

查看:118
本文介绍了GitLab CI 的多行 YAML 字符串 (.gitlab-ci.yml)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个 gitlab-ci.yml 文件,该文件使用多行字符串作为命令.但是,它似乎没有被解析.我已经尝试了 - |- > ,结果相同.

I'm trying to write a gitlab-ci.yml file which uses a multi-line string for the command. However, it seems like it is not being parsed. I've tried both the - | and - > with identical results.

stages:
  - mystage

Build:
  stage: mystage
  script:
    - |
        echo -e "
            echo 'hi';
            echo 'bye';
        "

当它尝试运行时,它只显示 echo -e ' 作为要运行的脚本,而不是整个多行字符串.这给我带来了问题.

When it tries to run, it only shows echo -e ' as the script to run, and not the whole multiline string. This causes issues for me.

写这样的东西的正确语法是什么?

What would be the correct syntax to write something like this?

推荐答案

TL;DR;你想使用一个多行 YAML 标量(为了可读性),它作为单行字符串加载,可以由 Gitlab-CI 作为命令发出.为此,请在 YAML 中使用分布在多行中的普通(不带引号)标量:

TL;DR; You want to use a multi-line YAML scalar (for readability) that is loaded as a single line string that can be issued as a command by Gitlab-CI. To do so use a plain (without quotes) scalar in YAML that is spread out over multiple lines:

script:
- echo -e 
   "echo 'hi';
    echo 'bye';"

请注意,YAML 对此类标量施加了一些限制.您当然需要知道的是,接下来的每一行都比 echo -e 缩进至少一个位置(相对于它的集合节点缩进两个位置,根本不缩进),并且加载时每个换行符都会被一个空格替换(因此您需要注意放置换行符的位置).

Please be aware that there are some restrictions imposed by YAML on such scalars. What you certainly need to know is that each following line is indented at least one more position than echo -e (which is indented two positions relative to its collection node, which is not indented at all), and that every new-line is replaced by a space when loaded (so you need to take a bit care of where to put newlines).

您的帖子中有多种误解,导致您提出错误的问题.

There are multiple misconceptions in your post, that lead to you asking the wrong question.

没有多行 YAML 字符串这种东西.YAML 有标量,其中一些标量可以由程序加载为字符串,而另一些则可以加载为整数、浮点数等.

There is no such thing as a multi-line YAML string. YAML has scalars and some of these scalars can be loaded by a program as strings, while some others will be loaded as integers, floats, etc.

您显然对作为字符串加载的标量节点感兴趣,因为该字符串可以被解释为命令行.但是您不想拥有多行命令行(即带有嵌入式换行符),因为 多行脚本(如@Jordan 所示).

You are obviously interested in scalar nodes that are being loaded as a string, since that string can be then be interpreted as a command-line. But you don't want to have multi-line command-line (i.e. with embedded newlines), since multi-line scripts are not supported in Gitlab CI (as @Jordan indicated).

为了便于阅读,您希望使用 YAML 的标准功能将多行标量加载为单行字符串.

For readability you want to use the, standard, capability of YAML to load multi-line scalars as single line string.

如果您不关心可读性,您可以使用:

If you wouldn't care about readability you could use:

- echo -e "
    echo 'hi';
    echo 'bye';
"

并且由于您的标量没有被引用(即它以 echo 开头),您不需要在 YAML 中为反斜杠或引号做任何特殊的事情.

and since your scalar is not quoted (i.e. it starts with echo) you don't need to do anything special in YAML for the backslashes or quotes.

脚本的结果是一样的(打印一个空行,打印echo 'hi';在一行缩进四个空格,打印echo 'bye';在一行缩进四个空格.)

The result of the script is the same (print an empty line, print echo 'hi'; on a line indented four spaces, print echo 'bye'; on a line indented four spaces.)

如果您想使用多行输入以提高可读性,即作为单行加载,基本上有两种选择:使用多行平面标量或在 YAML 中使用折叠标量.

If you want to use the multi-line input for readability, that are loaded as a single line, there are essentially two options: use a multi-line plane scalar or use a folded scalar in your YAML.

Plain 表示标量未加引号,与 YAML 中的任何多行内容一样,多行表示后面的行需要适当缩进,在这种情况下比初始行更远

Plain means the scalar is non-quoted, and as with any multi-line thing in YAML multi-line means following lines need to be indented appropriately, in this case further than the initial line

script:
- echo -e 
   "echo 'hi';
    echo 'bye';"

换行符被空格替换,所以不要这样做:

newlines are replaced by spaces so don't do:

script:
- echo -e 
   "echo 'hi';
    echo '
   bye';"

因为您将在 bye 之前获得一个可见空间.

as you will get a visible space before bye.

有一些限制,例如在这样的标量中不能有冒号后跟空格(这会使它看起来像键值对).

There are some restrictions like that you cannot have a colon followed by a space within such a scalar (which would make it look like key-value pair).

没有必要在纯标量中转义反斜杠,因为您无法在纯标量中转义任何字符,但当然您可以包含反斜杠,它最终会出现在从 YAML 加载的字符串中,可以 对从该字符串执行的命令有意义.

There is no need to escape backslashes in plain scalars, as you cannot escape any characters in a plain scalar, but of course you can include a backslash, which will end up in the string loaded from the YAML and can have meaning for the command executed from that string.

折叠标量类似于普通标量,在加载期间所有(单个)换行符都被空格替换:

A folded scalar is similar to a plain scalar in that all (single) newlines are substituted by a space during loading:

script:
- >
  echo -e 
  "echo 'hi';
  echo 'bye';"

您需要缩进实际命令信息至少与折叠标量指示符 (>) 一样多.

You need to indent the actual command information at least as much as the folded scalar indicator (>).

与普通标量相反,像 : 这样的东西没有特殊含义.因此,如果普通标量因抛出 YAML 错误而失败,则类似的折叠标量很可能不会.

Contrary to plain scalars things like : have no special meaning. So if plain scalars fail by throwing a YAML error, similar folded scalars most likely won't.

这篇关于GitLab CI 的多行 YAML 字符串 (.gitlab-ci.yml)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆