GitlabCI学习笔记之四:GitLabRunner pipeline语法之only except rules workflow

简介: GitlabCI学习笔记之四:GitLabRunner pipeline语法之only except rules workflow

1.only & except

参考文档:https://docs.gitlab.com/ee/ci/yaml/#only--except

only和except是两个参数用分支策略来限制jobs构建,后面会逐步被rules替代

only定义哪些分支和标签的git项目将会被job执行。

except定义哪些分支和标签的git项目将不会被job执行

示例

job:
  # use regexp
  only:
    - /^issue-.*$/
  # use special keyword
  except:
    - branches

2.rules

rules允许按顺序评估单个规则对象的列表,直到一个匹配并为作业动态提供属性. 请注意, rules不能only/exceptonly/except组合使用。

可用的规则条款包括:

1.if (类似于only:variables )

2.changes ( only:changes相同)

3。exists

示例1:rules:if

如果DOMAIN的值匹配,则需要手动运行。不匹配则默认匹配到on_success。 条件判断从上到下,匹配即停止。多条件匹配可以使用&& ||

variables:
  DOMAIN: www.example.com
deploy:
  stage: deploy
  script:
    - echo "hello deploy"
    - sleep 2;
  rules:
    - if: '$DOMAIN == "example.com"'
      when: manual
    - if: '$DOMAIN == "www.example.com"'
      when: delayed
      start_in: '10'
    - when: on_success

示例2:rules:changes

接受文件路径数组。 如果提交中Jenkinsfile文件发生的变化则为true。

codescan:
  stage: codescan
  tags:
    - build
  script:
    - echo "codescan"
    - sleep 5;
  #parallel: 5
  rules:
    - changes:
      - Jenkinsfile
      when: manual
    - if: '$DOMAIN == "example.com"'
      when: on_success
    - when: on_success

示例3:rules:exists

接受文件路径数组。当仓库中存在指定的文件时操作。

codescan:
  stage: codescan
  tags:
    - build
  script:
    - echo "codescan"
    - sleep 5;
  #parallel: 5
  rules:
    - exists:
      - Jenkinsfile
      when: manual 
    - changes:
      - Jenkinsfile
      when: on_success
    - if: '$DOMAIN == "example.com"'
      when: on_success
    - when: on_success

示例4:rules:allow_failure

使用allow_failure: true rules:在不停止管道本身的情况下允许作业失败或手动作业等待操作.

job:
  script: "echo Hello, Rules!"
  rules:
    - if: '$CI_MERGE_REQUEST_TARGET_BRANCH_NAME == "master"'
      when: manual
      allow_failure: true

在此示例中,如果第一个规则匹配,则作业将具有以下when: manualallow_failure: true

3.workflow

顶级workflow:关键字适用于整个管道,并将确定是否创建管道。when :可以设置为alwaysnever . 如果未提供,则默认值always

示例

variables:
  DOMAIN: example.com
workflow:
  rules:
    - if: '$DOMAIN == "example.com"'  #如果不匹配则 不执行pipeline
    - when: never

4.综合示例

before_script:
  - echo "before-script!!"
variables:
  DOMAIN: example.com
  
workflow:
  rules:
    - if: '$DOMAIN == "example.com"'
      when: always
    - when: never
    
stages:
  - build
  - test
  - codescan
  - deploy
build:
  before_script:
    - echo "before-script in job"
  stage: build
  script:
    - echo "mvn clean "
    - echo "mvn install"
    - ech "$DOMAIN"
  after_script:
    - echo "after script in buildjob"
  rules:
    - exists:
      - Dockerfile
      when: on_success 
      allow_failure: true
    - changes:
      - Dockerfile
      when: manual
    - when: on_failure
unittest:
  stage: test
  script:
    - ech "run test"
  when: delayed
  start_in: '5'
  allow_failure: true
  retry:
    max: 1
    when:
      - script_failure
  timeout: 1 hours 10 minutes
  
  
deploy:
  stage: deploy
  script:
    - echo "hello deploy"
    - sleep 2;
  rules:
    - if: '$DOMAIN == "example.com"'
      when: manual
    - if: '$DOMAIN == "aexample.com"'
      when: delayed
      start_in: '5'
    - when: on_failure
  
codescan:
  stage: codescan
  script:
    - echo "codescan"
    - sleep 5;
  when: on_success
  parallel: 5
 
after_script:
  - echo "after-script"

 

相关文章
|
4月前
|
存储 缓存 监控
GitlabCI学习笔记之五:GitLabRunner pipeline语法之cache
GitlabCI学习笔记之五:GitLabRunner pipeline语法之cache
|
4月前
|
XML 存储 JSON
GitlabCI学习笔记之五:GitLabRunner pipeline语法之artifacts dependencies
GitlabCI学习笔记之五:GitLabRunner pipeline语法之artifacts dependencies
|
4月前
|
Shell 开发工具 Docker
GitlabCI学习笔记之二:GitLabRunner pipeline语法之job script stages
GitlabCI学习笔记之二:GitLabRunner pipeline语法之job script stages
|
4月前
|
JSON jenkins 持续交付
jenkins学习笔记之四:jenkins常用pipline DSL方法
jenkins学习笔记之四:jenkins常用pipline DSL方法
|
6月前
|
弹性计算 Java Serverless
Serverless 应用引擎操作报错合集之在执行环境 custom pre-deploy 时,命令 "go mod tidy" 失败了,是什么导致的
Serverless 应用引擎(SAE)是阿里云提供的Serverless PaaS平台,支持Spring Cloud、Dubbo、HSF等主流微服务框架,简化应用的部署、运维和弹性伸缩。在使用SAE过程中,可能会遇到各种操作报错。以下是一些常见的报错情况及其可能的原因和解决方法。
417 0
|
7月前
|
小程序 API 决策智能
Multi-Agent实践第1期:5分钟上手AgentScope
阿里云与魔搭社区联合举办Create@AI创客松,邀请开发者探索基于多智能体的人机协作模式。活动提供资源支持和专家指导,获胜者可获得近5万元现金奖励及6亿次千问调用额度。参赛者需准备大模型API,如DashScope或OpenAI,使用AgentScope开源框架开发多智能体应用。立即报名参加:[报名链接](https//startup.aliyun.com/special/aihackathon4)。
ES6系列笔记-参数
ES6系列笔记-参数
47 1
|
数据可视化 jenkins Java
Jenkins之pipeline语法——2023.07
Jenkins之pipeline语法——2023.07
251 0
jira学习案例17-hook+ts实现useArray小作业
jira学习案例17-hook+ts实现useArray小作业
72 0
jira学习案例17-hook+ts实现useArray小作业