• 使用trigger-forward跨流水线传递参数


    参考文档:https://docs.gitlab.com/ee/ci/yaml/#triggerforward

    今天给大家介绍一个gitlab CI/CD的关键字 - forward,该关键字是一个比较偏的功能,但同时也是一个很实用的功能,我们通过在gitlab的ci文件中使用forward关键字,可以将变量传递到下游的流水线。

    1. 介绍

    1.1 forward关键字

    默认情况下,只有yaml定义的变量被传递给下游管道,使用forward关键字,现在可以传递它手动管道变量下游管道。

    • forward:yaml_variables是一个已经存在的行为,默认为true。当为true时,将传递给yaml定义的变量

    到下游管道。

    • forward:pipeline_variables是一个新特性,默认为false。当为true时,手动管道变量被传递给下游管道。

    1.2 yaml_variables和pipeline_variables的区别

    • pipeline_variables可以将参数传递到下级pipeline,即便是在当前的pipeline中定义好参数,也会随着传入的参数来覆盖,见场景4和5。

    • yaml_variables也可以将参数传递到下级pipeline,但是需要在当前的pipeline中定义好参数,不会随着传入的参数来覆盖,见场景4和5。

    2. 项目配置

    2.1 项目信息

    在pipeline-trigger组(定义全局变量:GLOBAL_VAR=master,release)下有四个项目:

    • project-a: 父pipeline;
    • project-b(项目级变量:PROJECT_VAR=project): 子pipeline;
    • project-c(无项目级变量): 子pipeline;
    • centralized-ci : 存放pipeline ci的文件。

    2.2 pipeline ci文件

    存放在centralized-ci项目中,打印全局变量GLOBAL_VAR,项目变量PROJECT_VAR,trigger传入的变量TRIGGER_VAR):

    template/common-temp.yml

    image: busybox:latest
    
    build1:
      stage: build
      script:
        - echo "Do your build here"
    
    test1:
      stage: test
      script:
        - echo "Do a test here"
        - echo "For example run a test suite"
        - echo "$GLOBAL_VAR $PROJECT_VAR $TRIGGER_VAR"
    
    deploy1:
      stage: deploy
      script:
        - echo "Do your deploy here"
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    2.3 project-b和project-c ci文件

    include:
      - project: "cs-test-group1/kxwang/pipeline-trigger/centralized-ci"
        ref: main
        file: '/template/common-temp.yml'
    
    • 1
    • 2
    • 3
    • 4

    2.4 project-a ci文件(开启pipeline trigger)

    stages:
        - build
        - trigger
    variables:
      GLOBAL_VAR: haha
      PROJECT_VAR: xixi
      TRIGGER_VAR: hehe
    
    build_job:
        stage: build
        script:
            - echo "$GLOBAL_VAR $PROJECT_VAR $TRIGGER_VAR"
            
    trigger_project-b:
      stage: trigger
      variables: # default variables for each job
        GLOBAL_VAR: haha123
        PROJECT_VAR: xixi456
        TRIGGER_VAR: hehe789
      trigger:
        project: cs-test-group1/kxwang/pipeline-trigger/project-b
        branch: master
        #include:
        #  #- cs-test-group1/kxwang/pipeline-trigger/centralized-ci/template/common-temp.yml
        #  #- cs-test-group1/kxwang/pipeline-trigger/project-b/.gitlab-ci.yml'
        #  - project: cs-test-group1/kxwang/pipeline-trigger/project-b
        #    ref: master
        #    file: '.gitlab-ci.yml'
        strategy: depend
        forward:
            pipeline_variables: true
      rules:
        - if: '$PROJECT_LIST =~ /.*project-b.*/'
    
    trigger_project-c:
      stage: trigger
      trigger:
        project: cs-test-group1/kxwang/pipeline-trigger/project-c
        branch: master
        #include:
        #  #- cs-test-group1/kxwang/pipeline-trigger/centralized-ci/template/common-temp.yml
        #  - project: cs-test-group1/kxwang/pipeline-trigger/project-c
        #    ref: master
        #    file: '.gitlab-ci.yml'
        strategy: depend
        forward:
            yaml_variables: true
        #  GLOBAL_VAR: haha
        #  PROJECT_VAR: xixi
        #  TRIGGER_VAR: hehe
      rules:
        - if: '$PROJECT_LIST =~ /.*project-c.*/'
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    3. 场景使用

    3.1 场景1: 任何参数都不传递

    curl -X POST \
         --fail \
         -F token=glptt-30f3a36ac8f789cff5404f92d1d0a0be61d48491 \
         -F ref=master \
         https://jihulab.com/api/v4/projects/103863/trigger/pipeline
    
    • 1
    • 2
    • 3
    • 4
    • 5

    只运行project-a的build_job项目,此时

    GLOBAL_VAR继承群组级设置的变量;

    PROJECT_VAR在当前pipeline中有定义,因此会打印;

    TRIGGER_VAR在当前pipeline中有定义,因此会打印。
    在这里插入图片描述

    3.2 场景2: 传参数,并覆盖当前流水线的变量

    curl -X POST \
         --fail \
         -F token=glptt-30f3a36ac8f789cff5404f92d1d0a0be61d48491 \
         -F ref=master \
         -F variables[GLOBAL_VAR]="123" \
         -F variables[PROJECT_VAR]="456" \
         -F variables[TRIGGER_VAR]="789" \
         https://jihulab.com/api/v4/projects/103863/trigger/pipeline
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    只运行project-a的build_job项目,此时

    GLOBAL_VAR继承群组级设置的变量;

    PROJECT_VAR在当前pipeline中有定义,因此会打印;

    TRIGGER_VAR在当前pipeline中有定义,因此会打印。
    在这里插入图片描述

    3.3 场景3:触发项目b和c,不传入trigger参数

    curl -X POST \
         --fail \
         -F token=glptt-30f3a36ac8f789cff5404f92d1d0a0be61d48491 \
         -F ref=master \
         -F variables[PROJECT_LIST]="project-b,project-c" \
         https://jihulab.com/api/v4/projects/103863/trigger/pipeline
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    Project-b(由于在pipeline的项目b的job段定义了变量,因此变量也继承了下来)
    在这里插入图片描述
    Project-c(由于在pipeline的全局定义了变量,因此变量也继承了下来)
    在这里插入图片描述

    3.4 场景4:触发项目b和c,传入trigger参数

    curl -X POST \
         --fail \
         -F token=glptt-30f3a36ac8f789cff5404f92d1d0a0be61d48491 \
         -F ref=master \
         -F variables[TRIGGER_VAR]="customer success" \
         -F variables[PROJECT_LIST]="project-b,project-c" \
         https://jihulab.com/api/v4/projects/103863/trigger/pipeline
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    project-b(在pipeline中定义项目b的job段定义了变量,且使用了forwaord:pipeline_variables,传入的变量可以覆盖了job中定义的variables)
    在这里插入图片描述
    project-c(由于在pipeline中未定义c的变量,而全局定义了变量,且使用了forwaord:yaml_variables,因此将pipeline全局中的变量继承传递了下来)
    在这里插入图片描述

    3.5 场景5: 触发项目b和c,传入trigger参数(验证)

    调整project-a的ci文件

    stages:
        - build
        - trigger
    
    #variables: # default variables for each job
    #  GLOBAL_VAR: haha
    #  PROJECT_VAR: xixi
    #  TRIGGER_VAR: hehe
    
    build_job:
        stage: build
        script:
            - echo "$GLOBAL_VAR $PROJECT_VAR $TRIGGER_VAR"
    
    trigger_project-b:
      stage: trigger
      variables:
        GLOBAL_VAR: haha123
        PROJECT_VAR: xixi456
        TRIGGER_VAR: hehe789
      trigger:
        project: cs-test-group1/kxwang/pipeline-trigger/project-b
        branch: master
        #include:
        #  #- cs-test-group1/kxwang/pipeline-trigger/centralized-ci/template/common-temp.yml
        #  #- cs-test-group1/kxwang/pipeline-trigger/project-b/.gitlab-ci.yml'
        #  - project: cs-test-group1/kxwang/pipeline-trigger/project-b
        #    ref: master
        #    file: '.gitlab-ci.yml'
        strategy: depend
        forward:
            yaml_variables: true
      rules:
        - if: '$PROJECT_LIST =~ /.*project-b.*/'
    
    trigger_project-c:
      stage: trigger
      variables:
        TRIGGER_VAR: hehe
      trigger:
        project: cs-test-group1/kxwang/pipeline-trigger/project-c
        branch: master
        #include:
        #  #- cs-test-group1/kxwang/pipeline-trigger/centralized-ci/template/common-temp.yml
        #  - project: cs-test-group1/kxwang/pipeline-trigger/project-c
        #    ref: master
        #    file: '.gitlab-ci.yml'
        strategy: depend
        forward:
            pipeline_variables: true
        #  GLOBAL_VAR: haha
        #  PROJECT_VAR: xixi
        #  TRIGGER_VAR: hehe
      rules:
        - if: '$PROJECT_LIST =~ /.*project-c.*/'
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    再次触发

    curl -X POST \
         --fail \
         -F token=glptt-30f3a36ac8f789cff5404f92d1d0a0be61d48491 \
         -F ref=master \
         -F variables[TRIGGER_VAR]="customer success" \
         -F variables[PROJECT_LIST]="project-b,project-c" \
         https://jihulab.com/api/v4/projects/103863/trigger/pipeline
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    project-b(在pipeline中定义项目b的job段定义了变量,且使用了forwaord:yaml_variables,传入的变量不会覆盖了job中定义的variables)
    在这里插入图片描述
    project-c(由于在pipeline中未定义c的变量,而全局定义了变量,且使用了forwaord:pipeline_variables,因此将pipeline全局中的变量覆盖后传递了下来)
    在这里插入图片描述

  • 相关阅读:
    前端岗位初入职场后的最初一段时间需要做什么
    sqlserver2012性能优化配置:设置性能相关的服务器参数
    P6510 奶牛排队
    开源数据库postgresql在统信系统上的离线安装
    负载均衡的常见实现方式
    八股文之jvm
    Sentinel 与 Hystrix:云原生时代的故障隔离与服务降级
    Mysql的安装配置教程(详细)
    微信小程序关键词排名优化:提升你的小程序可见性
    Java 微信公众号每日自动给女朋友推送问候
  • 原文地址:https://blog.csdn.net/weixin_44729138/article/details/134758531