gitlab--Stages、job、.pre 、.post 、before_script、after_script、variables 环境变量

发布时间 2023-04-16 21:19:25作者: 邹邹很busy。

Stages 和 job

在 pipeline 中,有几个名词需要知道,Stages、job、stage、before_script、after_script 等

Stages:一个流水线可以包含若干个阶段,一个阶段可以包含若干个作业

stages 用于定义作业可以使用的阶段,并且是全局定义的。同一阶段的作业并行运行,不同阶段按顺序执行。

这里定义了三个阶段,首先 Build 阶段运行,然后 Deploy 阶段运行,最后 Test 阶段并行运行。Test 阶段运行成功后将提交状态标记为 passed 状态。如果任何一个阶段运行失败,最后提交状态为 failed

全局定义的 stages 是来自于每个job。如果 job 没有定义 stage 则默认是 test 阶段。如果全局未定义 stages, 则按 yaml 文件里的顺序运行

job:作业是具体要执行的任务,命令脚本语句的集合

在每个项目中,使用名为 .gitlab-ci.yml 的 yaml 文件配置中,可以定义一个或多个作业(job)。每个 job 必须具有唯一的名称(不能使用关键字),每个 job 是独立运行的,job 定义了在约束条件下进行的相关操作,每个 job 至少要包含一个 script

.pre 和 .post

  • .pre:在流水线运行之前运行
  • .post:在流水线运行之后运行

.pre 和 .post 是 stage 的名称,不需要在 stages 里指定,就会运行

.pre 始终是整个流水线的第一个运行阶段,.post 始终是整个流水线的最后一个运行阶段。 用户定义的阶段都在两者之间运行。.pre.post的顺序无法更改。如果管道仅包含.pre.post阶段的作业,则不会创建流水线

stages: # 指定运行的步骤,pre 和 post 不需要指定
  - build
  - test
  - deploy

ciinit: # job 的名称
  tags: 
    - k8s
  stage: .pre # pipeline 运行之前运行
  script:
    - echo "pipeline init first job"

ciend: # job 的名称
  tags: 
    - k8s
  stage: .post # pipeline 运行之后运行
  script:
    - echo "pipeline end job"

build1: # job 的名称
  tags: # 指定要运行的 runner,指定的是 runner 的 tag
    - k8s
  stage: build
  script:
    - echo "Do your build here"

test1:  # 没有指定要运行的 runner,就在可以运行的 runner 上选择一台运行
  stage: test
  script:
    - echo "Do a test here"
    - echo "For example run a test suite"

test2: # job 的名称
  stage: test
  script:
    - echo "Do another parallel test here"
    - echo "For example run a lint test"

deploy1: # job 的名称
  tags: # 指定要运行的 runner,指定的是 runner 的 tag
    - k8s
  stage: deploy
  script:
    - echo "Do your deploy here"

可以看到运行的步骤为

如果两个或者多个 job,指向同一个阶段名称,则该阶段下的所有 job  都并行运行。如果不能并行运行,需要检查 runner 的配置文件中的 concurrent 的值是不是大于 1。

例如上面的 Test 阶段,有两个 job,test1 和 test2。如果 runner 的配置文件中的 concurrent 为 2 ,则两个 job 会并行运行

before_script 和 after_script

before_script

用于定义一个命令,该命令在每个作业之前运行。必须是一个数组。指定的script与主脚本中指定的任何脚本串联在一起,并在单个 shell 中一起执行

after_script

用于定义将在每个作业(包括失败的作业)之后运行的命令。这必须是一个数组。指定的脚本在新的 shell 中执行,与任何before_scriptscript脚本分开。

可以在全局定义,也可以在 job 中定义。在 job 中定义会覆盖全局。

before_script 失败导致整个作业失败,其他作业将不再执行。作业失败不会影响 after_script 运行。哪怕 job 失败了,after-script 也会执行

before_script: # 在流水线之前运行
  - echo "我是在流水线运行之前运行"

variables: # 设置了一个全局的环境变量
  DOMAIN: example.com

stages: # 指定运行的顺序
  - test
  - build
  - deploy


build: # job 名称
  before_script: # 在 job 运行 script 之前运行
    - echo "job 中的 job 运行之前运行"
  stage: build
  script:
    - echo "mvn clean "
    - echo "mvn install"
  after_script: # 在 job 运行 script 之后运行
    - echo "job 中的 job 运行之后运行"


deploy:
  stage: deploy
  script:
    - echo "我是部署阶段"
  
test:
  stage: test
  script:
    - echo "我是测试阶段"

after_script: # 所有的 job 运行完成之后运行
  - echo "我是在流水线运行之后运行"

运行流水线,查看阶段

查看 test job 的信息

在来查看 build 的 job

在来查看 deploy 的 job

variables 环境变量

变量可以分为全局变量和局部变量,全局变量是整个流水线都可以使用的,局部变量是只在 job 中生效的

stages:
  - build
  - test
  - deploy

variables: # 全局变量
  VERSIONS: "1.32.1"

deploy2:
  tags: # 指定要运行的 runner,指定的是 runner 的 tag
    - k8s
  variables: # 局部变量
    VERSIONS: "6.78.9"
  stage: deploy
  script:
    - echo "${VERSIONS}" # 局部变量的优先级大于全局变量的


build1:
  tags: # 指定要运行的 runner,指定的是 runner 的 tag
    - k8s
  stage: build
  script:
    - echo "${VERSIONS}" # 打印的是全局变量

test1:  # 没有指定要运行的 runner,就在可以运行的 runner 上选择一台运行
  stage: test
  script:
    - echo "Do a test here"
    - echo "For example run a test suite"

test2:
  stage: test
  script:
    - echo "Do another parallel test here"
    - echo "For example run a lint test"

deploy1:
  tags: 
    - k8s
  stage: deploy
  script:
    - echo "Do your deploy here"