如果 yaml 文件中可能存在条件? [英] If condition possible in yaml file?

查看:21
本文介绍了如果 yaml 文件中可能存在条件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据 if 条件再添加一个名称.如果如果来自另一个 .yml 文件的变量值为是",则在列表中添加一个新名称

I need to add one more name based on if condition. If If variable value from another .yml file is "yes" then add a new name in the list

我的 yaml 文件中有以下代码:

I've the following code in my yaml file:

JsNames:
 - name: 'jquery.min.js'
 - name: 'script.js'

我需要根据 if 条件添加更多名称.如果来自另一个 .yml 文件的变量值为yes",则添加一个新名称,如下所示:

I need to add more name based on if condition. If variable value from another .yml file is "yes" then add a new name like this:

JsNames:
 - name: 'jquery.min.js'
 - name: 'script.js'
 - name: 'new.js'

这可能吗?

尝试了以下方法:

JsNames:
 - name: 'jquery.min.js'
 - name: 'script.js'
 - | # Test multiline yaml support
   if [[ "site.data.settings.requiredjs" == "yes" ]]; then
     name: 'new.js'
   fi

我需要根据 if 条件添加更多名称.如果来自另一个 .yml 文件的变量值为yes",则添加一个新名称,如下所示:

I need to add more name based on if condition. If variable value from another .yml file is "yes" then add a new name like this:

JsNames:
 - name: 'jquery.min.js'
 - name: 'script.js'
 - name: 'new.js'

推荐答案

作为对 Vaibhav 回应的补充:虽然 yml 本身并不支持 if 语句,但一些使用 yml 文件进行指令的应用程序可能能够解析包含在yml.GitLab 是一个值得注意的例子.在 GitLab 的 CI/CD 中,.gitlab-ci.yml 文件可以配置为包含 if 语句,例如:

As a supplement to Vaibhav's response: although yml does not inherently support if statements, some applications that use yml files for instructions may be able to parse if statements included in the yml. GitLab is one notable example. In GitLab's CI/CD, the .gitlab-ci.yml file can be configured to include if statements such as:

job:
  script: "echo Hello, Rules!"
  rules:
    - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"'
      when: always
    - if: '$VAR =~ /pattern/'
      when: manual
    - when: on_success

这是来自 https://docs.gitlab.com/ee/ci/yaml/#rules.

这篇关于如果 yaml 文件中可能存在条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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