本篇目录链接

  • GitLab CI/CD variables 说明:
  • 使用变量的方式
  • 1、预定义变量(Predefined CI/CD variables)
  • 2、自定义变量(Custom CI/CD variables)
  • 3、.gitlab-ci.yml 中定义变量
  • 实现方式
  • 1、项目中定义变量
  • 2、通过api
  • 1) 前置条件:
  • i 查看项目id
  • ii 创建项目级令牌
  • iii 查看此项目是否有变量
  • 2) 列出项目变量的
  • 3) 创建变量
  • 4) 查看变量key的值
  • 5) 更新变量
  • 6) 删除变量
  • 3、.gitlab-ci.yml 中定义变量



出自:https://docs.gitlab.com/ee/ci/variables/index.html

GitLab CI/CD variables 说明:

CI/CD variables are a type of environment variable. You can use them to:

  • Control the behavior of jobs and pipelines.
  • Store values you want to re-use.
  • Avoid hard-coding values in your .gitlab-ci.yml file.

You can use predefined CI/CD variables or define custom:

  • Variables in the .gitlab-ci.yml file.
  • Project CI/CD variables.
  • Group CI/CD variables.
  • Instance CI/CD variables.

说人话就是:
环境变量是一个动态命名的值,它可以影响正在运行的进程在操作系统上的行为方式。
环境变量是进程在其中运行的环境的一部分. 例如,正在运行的进程可以查询TEMP环境变量的值以发现合适的位置来存储临时文件,或者为可以在不同脚本中重用的数据库定义URL 。
变量对于在 GitLab CI / CD 中自定义作业很有用,使用变量时,不必对值进行硬编码。

使用变量的方式

1、预定义变量(Predefined CI/CD variables)

我们可以预定义变量在.gitlab-ci.yml中且无需声明,更相当于一种内置变量

test_variable:
  stage: test
  script:
    - echo "$CI_JOB_STAGE"

gitlab ci trigger语法 gitlab ci variables_git

2、自定义变量(Custom CI/CD variables)

You can create custom CI/CD variables:

  • For a project:
  1. In the project’s .gitlab-ci.yml file.
  2. In the project’s settings.
  3. With the API.
  • For all projects in a group in the group’s setting.
  • For all projects in a GitLab instance in the instance’s settings.

You can override variable values manually for a specific pipeline, or have them prefilled in manual pipelines.
There are two types of variables: File or Variable.

Variable names are limited by the shell the runner uses to execute scripts. Each shell has its own set of reserved variable names.
Make sure each variable is defined for the scope you want to use it in.
By default, pipelines from forked projects can’t access CI/CD variables in the parent project. If you run a merge request pipeline in the parent project for a merge request from a fork, all variables become available to the pipeline.

大意就是:

  • 对于一个项目而言:
  1. 可以在 .gitlab-ci.yml 中定义变量
  2. 可以在项目上定义
  3. 通过api来传递
  • 对在一个组内的所有的项目而言,通过组设置来定义
  • 对于一个GitLab实例下的所有项目而言,通过实例的设置来定义

你可以手动覆盖某个流水线的变量值,或者在手动管道中预先填充它们。

  • 有两种类型的变量:文件或变量。
  1. 变量名受到运行程序用于执行脚本的shell的限制。每个shell都有自己的一组保留变量名;确保每个变量都是为你想要使用它的范围定义的。
  2. 默认情况下,来自分支项目的管道不能访问父项目中的CI/CD变量。如果你在父项目中为一个来自fork的合并请求运行一个合并请求管道,那么所有的变量对该管道都是可用的。

3、.gitlab-ci.yml 中定义变量

variables:
  TEST_VAR: "All jobs can use this variable's value"

job1:
  variables:
    TEST_VAR_JOB: "Only job1 can use this variable's value"
  script:
    - echo "$TEST_VAR" and "$TEST_VAR_JOB"

实现方式

1、项目中定义变量

To add or update variables in the project settings:

  1. Go to your project’s Settings > CI/CD and expand the Variables section.
  2. Select the Add Variable button and fill in the details:
    i - Key: Must be one line, with no spaces, using only letters, numbers, or _.
    ii - Value: No limitations.
    iii - Type: File or Variable.
    iv - Environment scope: Optional. All, or specific environments.
    v - Protect variable Optional. If selected, the variable is only available in pipelines that run on protected branches or tags.
    vi - Mask variable Optional. If selected, the variable’s Value is masked in job logs. The variable fails to save if the value does not meet the masking requirements.
  3. gitlab ci trigger语法 gitlab ci variables_自定义_02

2、通过api

基于gitlab自身提供的api接口

1) 前置条件:
  • 1、其中id为项目的id好,可以在setting-general中查到,案例中我的项目id为7
  • 2、创建PRIVATE-TOKEN,可以在setting-访问令牌(选择需要的权限和过期时间,项目级),也可以直接此用户下创建(用户级)
  • 3、此项目下没有任何变量
i 查看项目id

项目名称—>设置—>通用—>命名、主题、头像

gitlab ci trigger语法 gitlab ci variables_自定义_03

ii 创建项目级令牌

项目名称—>设置—>访问令牌—>项目访问令牌

gitlab ci trigger语法 gitlab ci variables_gitlab ci trigger语法_04

iii 查看此项目是否有变量

项目名称—>设置—>CI/CD—>变量

gitlab ci trigger语法 gitlab ci variables_gitlab ci trigger语法_05

2) 列出项目变量的
GET /projects/:id/variables

如:

curl --header "PRIVATE-TOKEN: kJZ9Ahxs39-xhsptT1bn" "http://192.168.137.14/api/v4/projects/7/variables"

由于我的项目没有变量,所以为空。

gitlab ci trigger语法 gitlab ci variables_ci_06

3) 创建变量
POST /projects/:id/variables

如:

curl --request POST --header "PRIVATE-TOKEN: kJZ9Ahxs39-xhsptT1bn" \
     "https://192.168.137.14/api/v4/projects/7/variables" --form "key=name" --form "value=wangkx"

gitlab ci trigger语法 gitlab ci variables_gitlab ci trigger语法_07


gitlab ci trigger语法 gitlab ci variables_自定义_08

4) 查看变量key的值
GET /projects/:id/variables/:key

如:

curl --header "PRIVATE-TOKEN: kJZ9Ahxs39-xhsptT1bn" "http://192.168.137.14/api/v4/projects/7/variables/name"

gitlab ci trigger语法 gitlab ci variables_gitlab ci trigger语法_09

5) 更新变量
PUT /projects/:id/variables/:key

如:

curl --request PUT --header "PRIVATE-TOKEN: kJZ9Ahxs39-xhsptT1bn" "http://192.168.137.14/api/v4/projects/7/variables/name" --form "value=wangkaixuan"

gitlab ci trigger语法 gitlab ci variables_git_10


gitlab ci trigger语法 gitlab ci variables_自定义_11

6) 删除变量
DELETE /projects/:id/variables/:key

如:

curl --header "PRIVATE-TOKEN: kJZ9Ahxs39-xhsptT1bn" "http://192.168.137.14/api/v4/projects/7/variables/name"

gitlab ci trigger语法 gitlab ci variables_自定义_12

3、.gitlab-ci.yml 中定义变量

不再赘述,通“使用变量的方式”中的“3”。