004.Gitlab CICD流水线模型

gitlab cicd流水线模型

gitlab cicd模型介绍

gitlab cicd流水线是基于gitops,即所有对流水线的配置都是代码的形式,且保存在当前代码仓的根目录。
文件名也是固定的,为 .gitlab-ci.yml 。
实际项目中,针对流水线会定义多个步骤:编译、构建、部署、测试、发布、上线等。

gitlab 流水线实践

配置gitlab runner

将当前已注册的gitlab runner,添加一个标签,便于后期采用标签调用。
059

简单流水线

  • 编写流水线
[root@gitclient myapp]# vim .gitlab-ci.yml
stages:
  - test

test_01:
  stage: test
  script: 
    - echo "hello world!"
    - hostname
  tags:
    - study-runner

示例释义:
stages:固定语法,定义流水线的步骤,如上定义了test步骤,所定义的名称是可自定义的。
test_01:任务的名称,在gitlab cicd中也叫job。
test_01.stage:在 test_01 的job中,stage是关键字,用于指定当前job属于stages中所定义步骤中的哪个步骤。
script:关键字,用于在runner上执行的命令。
tags:关键字,用来指定在哪个runner上执行,即具体匹配哪个标签的runner。

提示:若指定的runner不存在与当前环境,则流水线会一直处于等待中。

  • 提交流水线

将编写好的流水线提交至仓库。

[root@gitclient myapp]# git add .

[root@gitclient myapp]# git status 
On branch main
Your branch is up to date with 'origin/main'.

Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
        new file:   .gitlab-ci.yml

[root@gitclient myapp]# git commit -m "Add git cicd file" 
[main 73e8dd7] Add git cicd file
 1 file changed, 10 insertions(+)
 create mode 100644 .gitlab-ci.yml
       
[root@gitclient myapp]# git push origin main 
Enumerating objects: 4, done.
Counting objects: 100% (4/4), done.
Delta compression using up to 2 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 417 bytes | 417.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To ssh://gitlab.linuxsb.com:32222/mygroup/myapp.git
   284a2d9..73e8dd7  main -> main

提交后能在图形界面查看此流水线。

060

查看作业。
061

查看详细任务执行。
062

CICD流水线本质就是自动将代码下载到runner上进行执行。
实际中,流水线涉及多个步骤,每个步骤涉及多个job。

基本类型流水线

多步骤基本流水线示例一

重新定义一个流水线,增加多个不同阶段和步骤,每个步骤增加一个job。

  • 编写流水线
[root@gitclient myapp]# git rm .gitlab-ci.yml                   #删除旧流水线

[root@gitclient myapp]# vim .gitlab-ci.yml
stages:
  - compile
  - build
  - deploy
  - test
  - release

compile:
  stage: compile
  script:
    - echo -e "\nbegin to compile\n"
  tags:
    - study-runner

build:
  stage: build
  script:
    - echo -e "\nbegin to build\n"
  tags:
    - study-runner

deploy:
  stage: deploy
  script:
    - echo -e "\nbegin to deploy\n"
  tags:
    - study-runner
  
test:
  stage: test
  script:
    - echo -e "\nbegin to test\n"
  tags:
    - study-runner

release:
  stage: release
  script:
    - echo -e "\nbegin to release\n"
  tags:
    - study-runner
  • 提交流水线
[root@gitclient myapp]# git add .gitlab-ci.yml
[root@gitclient myapp]# git commit -m "Add second cicd file"
[root@gitclient myapp]# git push origin main

查看流水线。

063

查看流水线详情。

065

066

查看作业任务。
067

多步骤基本流水线示例二

在基本类型的流水线中,每个阶段可以设置多个任务,比较编译阶段,通常可能涉及多个编译工作,多个开发语言。
则编译、构建和发布都需要分别操作。

[root@gitclient myapp]# git rm .gitlab-ci.yml

[root@gitclient myapp]# vim .gitlab-ci.yml   
stages:
  - compile
  - build
  - deploy
  - test
  - release

compile_java:
  stage: compile
  script:
    - echo -e "\nbegin to compile java\n"
  tags:
    - study-runner

compile_c:
  stage: compile
  script:
    - echo -e "\nbegin to compile c\n"
    - sleep 5
  tags:
    - study-runner

compile_node:
  stage: compile
  script:
    - echo -e "\nbegin to compile node\n"
  tags:
    - study-runner

build_java:
  stage: build
  script:
    - echo -e "\nbegin to build java\n"
  tags:
    - study-runner

build_c:
  stage: bui
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

木二_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值