gitlab--services、environment、inherit

发布时间 2023-08-12 18:15:06作者: 邹邹很busy。

services

services 关键字定义了一个 Docker 镜像,该镜像在链接到 image 关键字定义的 Docker 镜像的 job 期间运行。这允许您在构建期间访问服务镜像。

服务镜像可以运行任何应用程序,但最常见的用例是运行数据库容器,例如:

例如,每次构建项目时,使用现有镜像并将其作为附加容器运行比安装 mysql 更容易、更快。

不仅限于数据库服务,您可以在 .gitlab-ci.yml 中添加任意数量的 services 或手动修改 config.toml。 在 Docker Hub 或您的私有容器镜像中找到的任何镜像,都可以用作服务

inherit

使用或禁用全局定义的环境变量(variables)或默认值(default)。

之前我们使用 default 的例子如下

default: # 定义了一个默认的参数
  tags: # 如果 job 里没有 tages,就使用这个 tags
    - build
  retry: # 如果 job 里没有 retry,就使用这个 tags
    max: 2
  before_script: # 如果 job 里没有 before_script,就使用这个 tags
    - echo "before_script"

stages:
  - build
  - test

build:
  stage: build
  before_script:
    - echo "我是 job 里的"
  script:
    - echo "我是 build 的 job"

 
test:
  stage: test
  script:
    - echo "test 的 job"

这样的话,build 和 test 的 job 都会使用 default 下定义的值,如果我们不想某些 job 使用的话,就可以使用 inherit 了,inherit 的值有 true、false 决定是否使用,默认为true

我们也可以取消,如下

default: # 定义了一个默认的参数
  tags: # 如果 job 里没有 tages,就使用这个 tags
    - build
  retry: # 如果 job 里没有 retry,就使用这个 tags
    max: 2
  before_script: # 如果 job 里没有 before_script,就使用这个 tags
    - echo "before_script"

stages:
  - build
  - test

build:
  stage: build
  before_script:
    - echo "我是 job 里的"
  script:
    - echo "我是 build 的 job"
  

 
test:
  stage: test
  tags:
    - build
  script:
    - echo "test 的 job"
  inherit:
    default: false # 不使用定义的 default,全部
    variables: false # 不使用定义的全局变量,全部

 运行流水线,查看结果

我们也可以使用其中的某些默认值或者变量,如下

default: # 定义了一个默认的参数
  tags: # 如果 job 里没有 tages,就使用这个 tags
    - build
  retry: # 如果 job 里没有 retry,就使用这个 tags
    max: 2
  before_script: # 如果 job 里没有 before_script,就使用这个 tags
    - echo "before_script"

stages:
  - build
  - test

build:
  stage: build
  before_script:
    - echo "我是 job 里的"
  script:
    - echo "我是 build 的 job"
  

 
test:
  stage: test
  tags:
    - build
  script:
    - echo "test 的 job"
  inherit:
    default: # 使用默认值下面的两个变量 before_script 和 retry
      - before_script
      - retry
    variables: false # 不使用所有环境变量

流水线运行结果