热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

【GitLabCI/CD】:条件、分支(rules)

目录1.用途2.机制2.1.Rulesattributes2.2.Rulesclauses2.3.rules:if2.3.1.Commonifclausesforrules2.

目录
1. 用途
2. 机制
    2.1. Rules attributes
    2.2. Rules clauses
2.3. rules:if
      2.3.1. Common if clauses for rules
    2.4. rules:changes
    2.5. rules:exists
3. 综合示例    
3.1. 示例1:根据 commit meesage 触发
    3.2. 示例2:根据 Tag 触发

1. 用途?

.gitlab-ci.yml 的 rules 配置,能让我们根据自定义的筛选条件,控制 job 是否执行:

  • 这段脚本我只想在 dev 分支执行,另一段只想在 master 分支执行;

  • 这段脚本我只想在 master 分支上打 tag 的时候触发,其他情况不触发;

  • 这段脚本我只想在 这些文件产生变更时触发,其他情况不触发;

  • 这段脚本我只想在 commit message 中包含某些特定关键字时才触发;

  • ....

一个例子:

job:
script: echo "Hello, Rules!"
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: manual
allow_failure: true
- if: '$CI_PIPELINE_SOURCE == "schedule"'

2. 机制

The rules keyword can be used to include or exclude jobs in pipelines.

Rules are evaluated in order until the first matchWhen matched, the job is either included or excluded from the pipeline, depending on the configuration. If included, the job also has certain attributes added to it.

2.1. Rules attributes

The job attributes you can use with rules are:

  • when: If not defined, defaults to when: on_success.

    • If used as when: delayedstart_in is also required.

  • allow_failure: If not defined, defaults to allow_failure: false.

  • variables: If not defined, uses the variables defined elsewhere.

If a rule evaluates to true, and when has any value except never, the job is included in the pipeline.

For example:

docker build:
script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
rules:
- if: '$CI_COMMIT_BRANCH == "master"'
when: delayed
start_in: '3 hours'
allow_failure: true

2.2. Rules clauses

Available rule clauses are:

  • if:Add or exclude jobs from a pipeline by evaluating an if statement.

  • changes:Add or exclude jobs from a pipeline based on what files are changed.

  • exists:Add or exclude jobs from a pipeline based on the presence of specific files.

Rules are evaluated in order until a match is found. If a match is found, the attributes are checked to see if the job should be added to the pipeline. If no attributes are defined, the defaults are:

  • when: on_success

  • allow_failure: false

The job is added to the pipeline:

  • If a rule matches and has when: on_successwhen: delayed or when: always.

  • If no rules match, but the last clause is when: on_successwhen: delayed or when: always (with no rule).

The job is not added to the pipeline:

  • If no rules match, and there is no standalone when: on_success, when: delayed or when: always.

  • If a rule matches, and has when: never as the attribute.

For example, using if clauses to strictly limit when jobs run:

job:
script: echo "Hello, Rules!"
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: manual
allow_failure: true
- if: '$CI_PIPELINE_SOURCE == "schedule"'

In this example:

  • If the pipeline is for a merge request, the first rule matches, and the job is added to the merge request pipeline with attributes of:

    • when: manual (manual job)

    • allow_failure: true (the pipeline continues running even if the manual job is not run)

  • If the pipeline is not for a merge request, the first rule doesn’t match, and the second rule is evaluated.

  • If the pipeline is a scheduled pipeline, the second rule matches, and the job is added to the scheduled pipeline. No attributes were defined, so it is added with:

    • when: on_success (default)

    • allow_failure: false (default)

  • In all other cases, no rules match, so the job is not added to any other pipeline.

Alternatively, you can define a set of rules to exclude jobs in a few cases, but run them in all other cases:

job:
script: echo "Hello, Rules!"
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
when: never
- if: '$CI_PIPELINE_SOURCE == "schedule"'
when: never
- when: on_success

  • If the pipeline is for a merge request, the job is not be added to the pipeline.

  • If the pipeline is a scheduled pipeline, the job is not be added to the pipeline.

  • In all other cases, the job is added to the pipeline, with when: on_success.

2.3. rules:if

rules:if clauses determine whether or not jobs are added to a pipeline by evaluating an if statement. If the if statement is true, the job is either included or excluded from a pipeline. In plain English, if rules can be interpreted as one of:

  • “If this rule evaluates to true, add the job” (default).

  • “If this rule evaluates to true, do not add the job” (by adding when: never).

Any set of expressions to be evaluated can be conjoined into a single expression by using && or ||, and the variable matching operators (==, !=, =~ and !~).

Unlike variables in script sections, variables in rules expressions are always formatted as $VARIABLE.

For example:

job:
script: echo "Hello, Rules!"
rules:
- if: '$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ ^feature/ && $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"'
when: always
- if: '$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME =~ ^feature/'
when: manual
allow_failure: true
- if: '$CI_MERGE_REQUEST_SOURCE_BRANCH_NAME' # Checking for the presence of a variable is possible

Some details regarding the logic that determines the when for the job:

  • If none of the provided rules match, the job is set to when: never and is not included in the pipeline.

  • A rule without any conditional clause, such as a when or allow_failure rule without if or changes, always matches, and is always used if reached.

  • If a rule matches and has no when defined, the rule uses the when defined for the job, which defaults to on_success if not defined.

  • You can define when once per rule, or once at the job-level, which applies to all rules. You can’t mix when at the job-level with when in rules.

2.3.1. Common if clauses for rules

You can check the value of the $CI_PIPELINE_SOURCE variable:

For example:

job:
script: echo "Hello, Rules!"
rules:
- if: '$CI_PIPELINE_SOURCE == "schedule"'
when: manual
allow_failure: true
- if: '$CI_PIPELINE_SOURCE == "push"'

  • This example runs the job as a manual job in scheduled pipelines or in push pipelines (to branches or tags), with when: on_success (default). It does not add the job to any other pipeline type.

Another example:

job:
script: echo "Hello, Rules!"
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
- if: '$CI_PIPELINE_SOURCE == "schedule"'

  • This example runs the job as a when: on_success job in merge request pipelines and scheduled pipelines. It does not run in any other pipeline type.

Other commonly used variables for if clauses:

  • if: $CI_COMMIT_TAG: If changes are pushed for a tag.

  • if: $CI_COMMIT_BRANCH: If changes are pushed to any branch.

  • if: '$CI_COMMIT_BRANCH == "master"': If changes are pushed to master.

  • if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH': If changes are pushed to the default branch (usually master). Use when you want to have the same configuration in multiple projects with different default branches.

  • if: '$CI_COMMIT_BRANCH =~ regex-expression/': If the commit branch matches a regular expression.

  • if: '$CUSTOM_VARIABLE !~ regex-expression/': If the custom variable CUSTOM_VARIABLE does not match a regular expression.

  • if: '$CUSTOM_VARIABLE == "value1"': If the custom variable CUSTOM_VARIABLE is exactly value1.

2.4. rules:changes

rules:changes determines whether or not to add jobs to a pipeline by checking for changes to specific files.

It’s recommended to only use rules: changes with branch pipelines or merge request pipelines. For example, it’s common to use rules: changes with merge request pipelines:

docker build:
script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"'
changes:
- Dockerfile
when: manual
allow_failure: true

In this example:

  • If the pipeline is a merge request pipeline, check Dockerfile for changes.

  • If Dockerfile has changed, add the job to the pipeline as a manual job, and the pipeline continues running even if the job is not triggered (allow_failure: true).

  • If Dockerfile has not changed, do not add job to any pipeline (same as when: never).

To use rules: changes with branch pipelines instead of merge request pipelines, change the if: clause in the example above to:

rules:
- if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH

To implement a rule similar to except:changes, use when: never.

  • You can use rules: changes with other pipeline types, but it is not recommended because rules: changes always evaluates to true when there is no Git push event. Tag pipelines, scheduled pipelines, and so on do not have a Git push event associated with them. A rules: changes job is always added to those pipeline if there is no if: statement that limits the job to branch or merge request pipelines.

2.5. rules:exists

exists accepts an array of paths and matches if any of these paths exist as files in the repository:

job:
script: docker build -t my-image:$CI_COMMIT_REF_SLUG .
rules:
- exists:
- Dockerfile

You can also use glob patterns to match multiple files in any directory in the repository:

job:
script: bundle exec rspec
rules:
- exists:
- spec/**.rb

For performance reasons, using exists with patterns is limited to 10,000 checks. After the 10,000th check, rules with patterned globs always match.

3. 综合示例

3.1. 示例1:根据 commit meesage 触发

publish-modern-docs:
stage: build
trigger:
include: sef/sef_docs_modern/.gitlab-ci.yml
rules:
- if: '$CI_COMMIT_MESSAGE =~ /^docs/'
changes:
- sef/sef_docs_modern/**/*

3.2. 示例2:根据 Tag 触发

build_sef:
stage: build
trigger:
include: sef/.gitlab-ci_sef.yml
rules:
- if: $CI_COMMIT_TAG


推荐阅读
  • sklearn数据集库中的常用数据集类型介绍
    本文介绍了sklearn数据集库中常用的数据集类型,包括玩具数据集和样本生成器。其中详细介绍了波士顿房价数据集,包含了波士顿506处房屋的13种不同特征以及房屋价格,适用于回归任务。 ... [详细]
  • CF:3D City Model(小思维)问题解析和代码实现
    本文通过解析CF:3D City Model问题,介绍了问题的背景和要求,并给出了相应的代码实现。该问题涉及到在一个矩形的网格上建造城市的情景,每个网格单元可以作为建筑的基础,建筑由多个立方体叠加而成。文章详细讲解了问题的解决思路,并给出了相应的代码实现供读者参考。 ... [详细]
  • 基于dlib的人脸68特征点提取(眨眼张嘴检测)python版本
    文章目录引言开发环境和库流程设计张嘴和闭眼的检测引言(1)利用Dlib官方训练好的模型“shape_predictor_68_face_landmarks.dat”进行68个点标定 ... [详细]
  • 在Kubernetes上部署JupyterHub的步骤和实验依赖
    本文介绍了在Kubernetes上部署JupyterHub的步骤和实验所需的依赖,包括安装Docker和K8s,使用kubeadm进行安装,以及更新下载的镜像等。 ... [详细]
  • 如何自行分析定位SAP BSP错误
    The“BSPtag”Imentionedintheblogtitlemeansforexamplethetagchtmlb:configCelleratorbelowwhichi ... [详细]
  • vue使用
    关键词: ... [详细]
  • 云原生边缘计算之KubeEdge简介及功能特点
    本文介绍了云原生边缘计算中的KubeEdge系统,该系统是一个开源系统,用于将容器化应用程序编排功能扩展到Edge的主机。它基于Kubernetes构建,并为网络应用程序提供基础架构支持。同时,KubeEdge具有离线模式、基于Kubernetes的节点、群集、应用程序和设备管理、资源优化等特点。此外,KubeEdge还支持跨平台工作,在私有、公共和混合云中都可以运行。同时,KubeEdge还提供数据管理和数据分析管道引擎的支持。最后,本文还介绍了KubeEdge系统生成证书的方法。 ... [详细]
  • 本文介绍了Hyperledger Fabric外部链码构建与运行的相关知识,包括在Hyperledger Fabric 2.0版本之前链码构建和运行的困难性,外部构建模式的实现原理以及外部构建和运行API的使用方法。通过本文的介绍,读者可以了解到如何利用外部构建和运行的方式来实现链码的构建和运行,并且不再受限于特定的语言和部署环境。 ... [详细]
  • MyBatis错题分析解析及注意事项
    本文对MyBatis的错题进行了分析和解析,同时介绍了使用MyBatis时需要注意的一些事项,如resultMap的使用、SqlSession和SqlSessionFactory的获取方式、动态SQL中的else元素和when元素的使用、resource属性和url属性的配置方式、typeAliases的使用方法等。同时还指出了在属性名与查询字段名不一致时需要使用resultMap进行结果映射,而不能使用resultType。 ... [详细]
  • 怀疑是每次都在新建文件,具体代码如下 ... [详细]
  • Python正则表达式学习记录及常用方法
    本文记录了学习Python正则表达式的过程,介绍了re模块的常用方法re.search,并解释了rawstring的作用。正则表达式是一种方便检查字符串匹配模式的工具,通过本文的学习可以掌握Python中使用正则表达式的基本方法。 ... [详细]
  • 本文讨论了clone的fork与pthread_create创建线程的不同之处。进程是一个指令执行流及其执行环境,其执行环境是一个系统资源的集合。在调用系统调用fork创建一个进程时,子进程只是完全复制父进程的资源,这样得到的子进程独立于父进程,具有良好的并发性。但是二者之间的通讯需要通过专门的通讯机制,另外通过fork创建子进程系统开销很大。因此,在某些情况下,使用clone或pthread_create创建线程可能更加高效。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • 本文介绍了在满足特定条件时如何在输入字段中使用默认值的方法和相应的代码。当输入字段填充100或更多的金额时,使用50作为默认值;当输入字段填充有-20或更多(负数)时,使用-10作为默认值。文章还提供了相关的JavaScript和Jquery代码,用于动态地根据条件使用默认值。 ... [详细]
  • 判断编码是否可立即解码的程序及电话号码一致性判断程序
    本文介绍了两个编程题目,一个是判断编码是否可立即解码的程序,另一个是判断电话号码一致性的程序。对于第一个题目,给出一组二进制编码,判断是否存在一个编码是另一个编码的前缀,如果不存在则称为可立即解码的编码。对于第二个题目,给出一些电话号码,判断是否存在一个号码是另一个号码的前缀,如果不存在则说明这些号码是一致的。两个题目的解法类似,都使用了树的数据结构来实现。 ... [详细]
author-avatar
杭ai君浩
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有