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

查看:211
本文介绍了适用于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.

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

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 "\n    echo 'hi';\n    echo 'bye';\n"

,并且由于标量未加引号(即,它以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.

普通表示标量不加引号,并且与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天全站免登陆