GitLab CI/CD Variables 中文文档
<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>variables 的执行顺序</li><li>
Predefined variables(Environment variables)</li><li>
9.0 Renaming</li><li>
<code>.gitlab-ci.yaml</code> defined variables</li><li>
Secret variables</li><li>
Protected secret variables</li><li>
Deploment variables</li><li>
Debug tracing</li><li>
Using the CI variables in your job scripts</li></ul></div><h1>Variables</h1>
<p>官方文档:https://docs.gitlab.com/ce/ci/variables/README.html</p>
<p>当GitLab CI 中接受到一个job后,Runner就开始准备构建环境。开始设置预定义的变量(环境变量)和用户自定义的变量。</p>
<p class="maodian"></p><h2>
variables 的执行顺序</h2>
<p>变量可以被重写,并且是按照下面的顺序进行执行:</p>
<ol>
<li>
Trigger variables(优先级最高)</li>
<li>Secret variables</li>
<li>YAML-defined job-level variables
</li>
<li>YAML-defined global variables
</li>
<li>Deployment variables</li>
<li>
Predefined variables (优先级最低)</li>
</ol>
<p>举个例子,如果你定义了私有变量<code>API_TOKEN=secure</code>,并且在<code>.gitlab-ci.yml</code>中定义了 <code>API_TOKEN=yaml</code>,那么私有变量<code>API_TOKEN</code>的值将是<code>secure</code>,因为secret variables的优先级较高。</p>
<p class="maodian"></p><h2>
Predefined variables(Environment variables)</h2>
<p>有部分预定义的环境变量仅仅只能在最小版本的GitLab Runner中使用。请参考下表查看对应的Runner版本要求。</p>
<p><strong>注意</strong>:从GitLab 9.0 开始,部分变量已经不提倡使用。请查看9.0 Renaming部分来查找他们的替代变量。强烈建议使用新的变量,我们也会在将来的GitLab版本中将他们移除。</p>
<table>
<thead><tr>
<th align="left">Variable</th>
<th>GitLab</th>
<th>Runner</th>
<th>Description</th>
</tr></thead>
<tbody>
<tr>
<td align="left"><strong>CI</strong></td>
<td>all</td>
<td>0.4</td>
<td>标识该job是在CI环境中执行</td>
</tr>
<tr>
<td align="left"><strong>CI_COMMIT_REF_NAME</strong></td>
<td>9.0</td>
<td>all</td>
<td>用于构建项目的分支或tag名称</td>
</tr>
<tr>
<td align="left"><strong>CI_COMMIT_REF_SLUG</strong></td>
<td>9.0</td>
<td>all</td>
<td>先将<code>$CI_COMMIT_REF_NAME</code>的值转换成小写,最大不能超过63个字节,然后把除了<code>0-9</code>和<code>a-z</code>的其他字符转换成<code>-</code>。在URLs和域名名称中使用。</td>
</tr>
<tr>
<td align="left"><strong>CI_COMMIT_SHA</strong></td>
<td>9.0</td>
<td>all</td>
<td>commit的版本号</td>
</tr>
<tr>
<td align="left"><strong>CI_COMMIT_TAG</strong></td>
<td>9.0</td>
<td>0.5</td>
<td>commit的tag名称。只有创建了tags才会出现。</td>
</tr>
<tr>
<td align="left"><strong>CI_DEBUG_TRACE</strong></td>
<td>9.0</td>
<td>1.7</td>
<td>
debug tracing开启时才生效</td>
</tr>
<tr>
<td align="left"><strong>CI_ENVIRONMENT_NAME</strong></td>
<td>8.15</td>
<td>all</td>
<td>job的环境名称</td>
</tr>
<tr>
<td align="left"><strong>CI_ENVIRONMENT_SLUG</strong></td>
<td>8.15</td>
<td>all</td>
<td>环境名称的简化版本,适用于DNS,URLs,Kubernetes labels等</td>
</tr>
<tr>
<td align="left"><strong>CI_JOB_ID</strong></td>
<td>9.0</td>
<td>all</td>
<td>GItLab CI内部调用job的一个唯一ID</td>
</tr>
<tr>
<td align="left"><strong>CI_JOB_MANUAL</strong></td>
<td>8.12</td>
<td>all</td>
<td>表示job启用的标识</td>
</tr>
<tr>
<td align="left"><strong>CI_JOB_NAME</strong></td>
<td>9.0</td>
<td>0.5</td>
<td>
<code>.gitlab-ci.yml</code>中定义的job的名称</td>
</tr>
<tr>
<td align="left"><strong>CI_JOB_STAGE</strong></td>
<td>9.0</td>
<td>0.5</td>
<td>
<code>.gitlab-ci.yml</code>中定义的stage的名称</td>
</tr>
<tr>
<td align="left"><strong>CI_JOB_TOKEN</strong></td>
<td>9.0</td>
<td>1.2</td>
<td>用于同GitLab容器仓库验证的token</td>
</tr>
<tr>
<td align="left"><strong>CI_REPOSITORY_URL</strong></td>
<td>9.0</td>
<td>all</td>
<td>git仓库地址,用于克隆</td>
</tr>
<tr>
<td align="left"><strong>CI_RUNNER_DESCRIPTION</strong></td>
<td>8.10</td>
<td>0.5</td>
<td>GitLab中存储的Runner描述</td>
</tr>
<tr>
<td align="left"><strong>CI_RUNNER_ID</strong></td>
<td>8.10</td>
<td>0.5</td>
<td>Runner所使用的唯一ID</td>
</tr>
<tr>
<td align="left"><strong>CI_RUNNER_TAGS</strong></td>
<td>8.10</td>
<td>0.5</td>
<td>Runner定义的tags</td>
</tr>
<tr>
<td align="left"><strong>CI_PIPELINE_ID</strong></td>
<td>8.10</td>
<td>0.5</td>
<td>GitLab CI 在内部使用的当前pipeline的唯一ID</td>
</tr>
<tr>
<td align="left"><strong>CI_PIPELINE_TRIGGERED</strong></td>
<td>all</td>
<td>all</td>
<td>用于指示该job被触发的标识</td>
</tr>
<tr>
<td align="left"><strong>CI_PROJECT_DIR</strong></td>
<td>all</td>
<td>all</td>
<td>仓库克隆的完整地址和job允许的完整地址</td>
</tr>
<tr>
<td align="left"><strong>CI_PROJECT_ID</strong></td>
<td>all</td>
<td>all</td>
<td>GitLab CI在内部使用的当前项目的唯一ID</td>
</tr>
<tr>
<td align="left"><strong>CI_PROJECT_NAME</strong></td>
<td>8.10</td>
<td>0.5</td>
<td>当前正在构建的项目名称(事实上是项目文件夹名称)</td>
</tr>
<tr>
<td align="left"><strong>CI_PROJECT_NAMESPACE</strong></td>
<td>8.10</td>
<td>0.5</td>
<td>当前正在构建的项目命名空间(用户名或者是组名称)</td>
</tr>
<tr>
<td align="left"><strong>CI_PROJECT_PATH</strong></td>
<td>8.10</td>
<td>0.5</td>
<td>命名空间加项目名称</td>
</tr>
<tr>
<td align="left"><strong>CI_PROJECT_PATH_SLUG</strong></td>
<td>9.3</td>
<td>all</td>
<td>
<code>$CI_PROJECT_PATH</code>小写字母、除了<code>0-9</code>和<code>a-z</code>的其他字母都替换成<code>-</code>。用于地址和域名名称。</td>
</tr>
<tr>
<td align="left"><strong>CI_PROJECT_URL</strong></td>
<td>8.10</td>
<td>0.5</td>
<td>项目的访问地址(http形式)</td>
</tr>
<tr>
<td align="left"><strong>CI_REGISTRY</strong></td>
<td>8.10</td>
<td>0.5</td>
<td>如果启用了Container Registry,则返回GitLab的Container Registry的地址</td>
</tr>
<tr>
<td align="left"><strong>CI_REGISTRY_IMAGE</strong></td>
<td>8.10</td>
<td>0.5</td>
<td>如果为项目启用了Container Registry,它将返回与特定项目相关联的注册表的地址</td>
</tr>
<tr>
<td align="left"><strong>CI_REGISTRY_PASSWORD</strong></td>
<td>9.0</td>
<td>all</td>
<td>用于push containers到GitLab的Container Registry的密码</td>
</tr>
<tr>
<td align="left"><strong>CI_REGISTRY_USER</strong></td>
<td>9.0</td>
<td>all</td>
<td>用于push containers到GItLab的Container Registry的用户名</td>
</tr>
<tr>
<td align="left"><strong>CI_SERVER</strong></td>
<td>all</td>
<td>all</td>
<td>标记该job是在CI环境中执行</td>
</tr>
<tr>
<td align="left"><strong>CI_SERVER_NAME</strong></td>
<td>all</td>
<td>all</td>
<td>用于协调job的CI服务器名称</td>
</tr>
<tr>
<td align="left"><strong>CI_SERVER_REVISION</strong></td>
<td>all</td>
<td>all</td>
<td>用于调度job的GitLab修订版</td>
</tr>
<tr>
<td align="left"><strong>CI_SERVER_VERSION</strong></td>
<td>all</td>
<td>all</td>
<td>用于调度job的GItLab版本</td>
</tr>
<tr>
<td align="left"><strong>ARTIFACT_DOWNLOAD_ATTEMPTS</strong></td>
<td>8.15</td>
<td>1.9</td>
<td>尝试运行下载artifacts的job的次数</td>
</tr>
<tr>
<td align="left"><strong>GET_SOURCES_ATTEMPTS</strong></td>
<td>8.15</td>
<td>1.9</td>
<td>尝试运行获取源的job次数</td>
</tr>
<tr>
<td align="left"><strong>GITLAB_CI</strong></td>
<td>all</td>
<td>all</td>
<td>用于指示该job是在GItLab CI环境中运行</td>
</tr>
<tr>
<td align="left"><strong>GITLAB_USER_ID</strong></td>
<td>8.12</td>
<td>all</td>
<td>开启该job的用户ID</td>
</tr>
<tr>
<td align="left"><strong>GITLAB_USER_EMAIL</strong></td>
<td>8.12</td>
<td>all</td>
<td>开启该job的用户邮箱</td>
</tr>
<tr>
<td align="left"><strong>RESTORE_CACHE_ATTEMPTS</strong></td>
<td>8.15</td>
<td>1.9</td>
<td>尝试运行存储缓存的job的次数</td>
</tr>
</tbody>
</table>
<p class="maodian"></p><h2>
9.0 Renaming</h2>
<p>根据GitLab的命名规则,在9.0以后将从<code>build</code>术语转到<code>job</code>CI变量中,并且已经被重命名。</p>
<table>
<thead><tr>
<th>8.x name</th>
<th>9.0+ name</th>
</tr></thead>
<tbody>
<tr>
<td><code>CI_BUILD_ID</code></td>
<td><code>CI_JOB_ID</code></td>
</tr>
<tr>
<td><code>CI_BUILD_REF</code></td>
<td><code>CI_COMMIT_SHA</code></td>
</tr>
<tr>
<td><code>CI_BUILD_TAG</code></td>
<td><code>CI_COMMIT_TAG</code></td>
</tr>
<tr>
<td><code>CI_BUILD_REF_NAME</code></td>
<td><code>CI_COMMIT_REF_NAME</code></td>
</tr>
<tr>
<td><code>CI_BUILD_REF_SLUG</code></td>
<td><code>CI_COMMIT_REF_SLUG</code></td>
</tr>
<tr>
<td><code>CI_BUILD_NAME</code></td>
<td><code>CI_JOB_NAME</code></td>
</tr>
<tr>
<td><code>CI_BUILD_STAGE</code></td>
<td><code>CI_JOB_STAGE</code></td>
</tr>
<tr>
<td><code>CI_BUILD_REPO</code></td>
<td><code>CI_REPOSITORY_URL</code></td>
</tr>
<tr>
<td><code>CI_BUILD_TRIGGERED</code></td>
<td><code>CI_PIPELINE_TRIGGERED</code></td>
</tr>
<tr>
<td><code>CI_BUILD_MANUAL</code></td>
<td><code>CI_JOB_MANUAL</code></td>
</tr>
<tr>
<td><code>CI_BUILD_TOKEN</code></td>
<td><code>CI_JOB_TOKEN</code></td>
</tr>
</tbody>
</table>
<p class="maodian"></p><h2>
<code>.gitlab-ci.yaml</code> defined variables</h2>
<blockquote><p>注意:此功能要求GitLab Runner 0.5或者更高版本,并且GitLab CI 7.14或者更高版本</p></blockquote>
<p>GitLab CI允许你向<code>.gitlab-ci.yml</code>中添加变量,这个变量在构建环境中设置。因此,变量将保存在存储中,他们用于存储非敏感的项目配置,例如:<code>RAILS_ENV</code>或者<code>DATABASE_URL</code>。</p>
<p>举个例子,如果将变量设置为全局以下(不是在一个作业中),则它将用于所有执行的命令脚本中:</p><pre class="brush:bash;toolbar:false">variables:
DATABASE_URL: "postgres://postgres@postgres/my_database"</pre><p>YAML中定义的变量也将应用到所有创建的服务容器中,因此可以对它进行微调。</p>
<p>变量可以定义为全局,同时也可以定义为job级别。若要关闭作业中的全局定义变量,请定义一个空hash:</p><pre class="brush:bash;toolbar:false">job_name:
variables: {}</pre><p>您可以在变量定义中使用其他变量(或使用$$将其转义):</p><pre class="brush:bash;toolbar:false">variables:
LS_CMD: 'ls $FLAGS $$TMP_DIR'
FLAGS: '-al'
script:
- 'eval $LS_CMD'# will execute 'ls -al $TMP_DIR'</pre><p></p>
<p class="maodian"></p><h2>
Secret variables</h2>
<blockquote>
<p>注意:</p>
<ul>
<li>这个功能需要GitLab Runner 0.4.0或者更高版本。</li>
<li>请注意,私有变量不会隐藏,如果明确要这么做,他们的值可以显示在job日志中。如果您的项目是公共的或内部的,你可以在项目的pipeline中设置pipeline为私有的。关于私有变量的讨论在issue *###&*_16_*###&*。</li>
</ul>
</blockquote>
<p>GitLab CI允许你在构建环境过程中设置项目的私有变量。私有变量存储在仓库(.gitlab-ci.yml)中,并被安全的传递给GitLab Runner,使其在构建环境中可用。建议使用该方法存储诸如密码、秘钥和凭据之类的东西。</p>
<p>可用通过<strong>Settings ➔ Pipelines</strong>来增加私有变量,通过<strong>Settings ➔ Pipelines</strong>找到的模块称之为私有变量。</p>
<p>一次设置,所有的后续pipeline是都可以使用它们。</p>
<p class="maodian"></p><h2>
Protected secret variables</h2>
<blockquote><p>注意:此功能要求GitLab 9.3或者更高。</p></blockquote>
<p>私有变量可以被保护。每当一个私有变量被保护时,它只会安全的传递到在受保护的分支或受保护的标签上运行的pipeline。其他的pipeline将不会得到受保护的变量。</p>
<p>可用通过<strong>Settings ➔ Pipelines</strong>来增加私有变量,通过<strong>Settings ➔ Pipelines</strong>找到的模块称之为私有变量,然后点击<em>Protected</em>。</p>
<p>一次设置,所有的后续pipeline是都可以使用它们。</p>
<p class="maodian"></p><h2>
Deploment variables</h2>
<blockquote><p>注意:此功能要求GitLab CI 8.15或者更高版本。</p></blockquote>
<p>负责部署配置的项目服务可以定义在构建环境中设置自己的变量。这些变量只定义用于部署job。请参考您正在使用的项目服务的文档,以了解他们定义的变量。</p>
<p>一个定义有部署变量的项目服务示例Kubernetes Service。</p>
<p class="maodian"></p><h2>
Debug tracing</h2>
<blockquote>
<p>GitLab Runner 1.7开始引入。</p>
<p><strong>警告</strong>:启用调试跟踪可能会严重的安全隐患。输出内容将包含所有的私有变量和其他的隐私!输出的内容将被上传到GitLab服务器并且将会在job记录中明显体现。</p>
</blockquote>
<p>默认情况下,GitLab Runner会隐藏了处理job时正在做的大部分细节。这种行为使job跟踪很短,并且防止秘密泄露到跟踪中,除非您的脚本将他们输出到屏幕中。</p>
<p>如果job没有按照预期的运行,这也会让问题查找变得更加困难;在这种情况下,你可以在<code>.gitlab-ci.yml</code>中开启调试记录。它需要GitLab Runner v1.7版本以上,此功能可启用shell的执行记录,从而产生详细的job记录,列出所有执行的命令,设置变量等。</p>
<p>在启用此功能之前,您应该确保job只对团队成员可见。您也应该https://docs.gitlab.com/ce/ci/pipelines.html#seeing-build-status所有生成的job记录,然后使其可见。</p>
<p>设置<code>CI_DEBUG_TRACE</code>变量的值为<code>true</code>来开启调试记录。</p><pre class="brush:bash;toolbar:false">job_name:
variables:
CI_DEBUG_TRACE: "true"</pre><p>调试记录设置为TRUE的截断输出示例:</p><pre class="brush:bash;toolbar:false">...
export CI_SERVER_TLS_CA_FILE="/builds/gitlab-examples/ci-debug-trace.tmp/CI_SERVER_TLS_CA_FILE"
if [[ -d "/builds/gitlab-examples/ci-debug-trace/.git" ]]; then
echo $'\''\x1b[32;1mFetching changes...\x1b[0;m'\''
$'\''cd'\'' "/builds/gitlab-examples/ci-debug-trace"
$'\''git'\'' "config" "fetch.recurseSubmodules" "false"
$'\''rm'\'' "-f" ".git/index.lock"
$'\''git'\'' "clean" "-ffdx"
$'\''git'\'' "reset" "--hard"
$'\''git'\'' "remote" "set-url" "origin" "https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@example.com/gitlab-examples/ci-debug-trace.git"
$'\''git'\'' "fetch" "origin" "--prune" "+refs/heads/*:refs/remotes/origin/*" "+refs/tags/*:refs/tags/*"
else
$'\''mkdir'\'' "-p" "/builds/gitlab-examples/ci-debug-trace.tmp/git-template"
$'\''rm'\'' "-r" "-f" "/builds/gitlab-examples/ci-debug-trace"
$'\''git'\'' "config" "-f" "/builds/gitlab-examples/ci-debug-trace.tmp/git-template/config" "fetch.recurseSubmodules" "false"
echo $'\''\x1b[32;1mCloning repository...\x1b[0;m'\''
$'\''git'\'' "clone" "--no-checkout" "https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@example.com/gitlab-examples/ci-debug-trace.git" "/builds/gitlab-examples/ci-debug-trace" "--template" "/builds/gitlab-examples/ci-debug-trace.tmp/git-template"
$'\''cd'\'' "/builds/gitlab-examples/ci-debug-trace"
fi
echo $'\''\x1b[32;1mChecking out dd648b2e as master...\x1b[0;m'\''
$'\''git'\'' "checkout" "-f" "-q" "dd648b2e48ce6518303b0bb580b2ee32fadaf045"
'
+++ hostname
++ echo 'Running on runner-8a2f473d-project-1796893-concurrent-0 via runner-8a2f473d-machine-1480971377-317a7d0f-digital-ocean-4gb...'
Running on runner-8a2f473d-project-1796893-concurrent-0 via runner-8a2f473d-machine-1480971377-317a7d0f-digital-ocean-4gb...
++ export CI=true
++ CI=true
++ export CI_DEBUG_TRACE=false
++ CI_DEBUG_TRACE=false
++ export CI_COMMIT_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ CI_COMMIT_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ export CI_COMMIT_BEFORE_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ CI_COMMIT_BEFORE_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ export CI_COMMIT_REF_NAME=master
++ CI_COMMIT_REF_NAME=master
++ export CI_JOB_ID=7046507
++ CI_JOB_ID=7046507
++ export CI_REPOSITORY_URL=https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@example.com/gitlab-examples/ci-debug-trace.git
++ CI_REPOSITORY_URL=https://gitlab-ci-token:xxxxxxxxxxxxxxxxxxxx@example.com/gitlab-examples/ci-debug-trace.git
++ export CI_JOB_TOKEN=xxxxxxxxxxxxxxxxxxxx
++ CI_JOB_TOKEN=xxxxxxxxxxxxxxxxxxxx
++ export CI_PROJECT_ID=1796893
++ CI_PROJECT_ID=1796893
++ export CI_PROJECT_DIR=/builds/gitlab-examples/ci-debug-trace
++ CI_PROJECT_DIR=/builds/gitlab-examples/ci-debug-trace
++ export CI_SERVER=yes
++ CI_SERVER=yes
++ export 'CI_SERVER_NAME=GitLab CI'
++ CI_SERVER_NAME='GitLab CI'
++ export CI_SERVER_VERSION=
++ CI_SERVER_VERSION=
++ export CI_SERVER_REVISION=
++ CI_SERVER_REVISION=
++ export GITLAB_CI=true
++ GITLAB_CI=true
++ export CI=true
++ CI=true
++ export GITLAB_CI=true
++ GITLAB_CI=true
++ export CI_JOB_ID=7046507
++ CI_JOB_ID=7046507
++ export CI_JOB_TOKEN=xxxxxxxxxxxxxxxxxxxx
++ CI_JOB_TOKEN=xxxxxxxxxxxxxxxxxxxx
++ export CI_COMMIT_REF=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ CI_COMMIT_REF=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ export CI_COMMIT_BEFORE_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ CI_COMMIT_BEFORE_SHA=dd648b2e48ce6518303b0bb580b2ee32fadaf045
++ export CI_COMMIT_REF_NAME=master
++ CI_COMMIT_REF_NAME=master
++ export CI_COMMIT_NAME=debug_trace
++ CI_JOB_NAME=debug_trace
++ export CI_JOB_STAGE=test
++ CI_JOB_STAGE=test
++ export CI_SERVER_NAME=GitLab
++ CI_SERVER_NAME=GitLab
++ export CI_SERVER_VERSION=8.14.3-ee
++ CI_SERVER_VERSION=8.14.3-ee
++ export CI_SERVER_REVISION=82823
++ CI_SERVER_REVISION=82823
++ export CI_PROJECT_ID=17893
++ CI_PROJECT_ID=17893
++ export CI_PROJECT_NAME=ci-debug-trace
++ CI_PROJECT_NAME=ci-debug-trace
++ export CI_PROJECT_PATH=gitlab-examples/ci-debug-trace
++ CI_PROJECT_PATH=gitlab-examples/ci-debug-trace
++ export CI_PROJECT_NAMESPACE=gitlab-examples
++ CI_PROJECT_NAMESPACE=gitlab-examples
++ export CI_PROJECT_URL=https://example.com/gitlab-examples/ci-debug-trace
++ CI_PROJECT_URL=https://example.com/gitlab-examples/ci-debug-trace
++ export CI_PIPELINE_ID=52666
++ CI_PIPELINE_ID=52666
++ export CI_RUNNER_ID=1337
++ CI_RUNNER_ID=1337
++ export CI_RUNNER_DESCRIPTION=shared-runners-manager-1.example.com
++ CI_RUNNER_DESCRIPTION=shared-runners-manager-1.example.com
++ export 'CI_RUNNER_TAGS=shared, docker, linux, ruby, mysql, postgres, mongo'
++ CI_RUNNER_TAGS='shared, docker, linux, ruby, mysql, postgres, mongo'
++ export CI_REGISTRY=registry.example.com
++ CI_REGISTRY=registry.example.com
++ export CI_DEBUG_TRACE=true
++ CI_DEBUG_TRACE=true
++ export GITLAB_USER_ID=42
++ GITLAB_USER_ID=42
++ export GITLAB_USER_EMAIL=user@example.com
++ GITLAB_USER_EMAIL=user@example.com
++ export VERY_SECURE_VARIABLE=imaverysecurevariable
++ VERY_SECURE_VARIABLE=imaverysecurevariable
++ mkdir -p /builds/gitlab-examples/ci-debug-trace.tmp
++ echo -n '-----BEGIN CERTIFICATE-----
MIIFQzCCBCugAwIBAgIRAL/ElDjuf15xwja1ZnCocWAwDQYJKoZIhvcNAQELBQAw'
...</pre><p></p>
<p class="maodian"></p><h2>
Using the CI variables in your job scripts</h2>
<p>在构建环境变量时,所有的变量都被设置为环境变量,他们可以使用普通方法访问这些变量。在大多数情况下,用于执行job脚本都是通过bash或者是sh。</p>
<p>想要访问环境变量,请示使用一下Runner对应的语法:</p>
<table>
<thead><tr>
<th>Shell</th>
<th>用法</th>
</tr></thead>
<tbody>
<tr>
<td>bash/sh</td>
<td><code>$variable</code></td>
</tr>
<tr>
<td>
windows batch</td>
<td><code>%variable%</code></td>
</tr>
<tr>
<td>PowerShell</td>
<td><code>$env:variable</code></td>
</tr>
</tbody>
</table>
<p>在bash中访问环境变量,需要给变量名称加上前缀(<code>$</code>):</p><pre class="brush:bash;toolbar:false">job_name:
script:
- echo $CI_JOB_ID</pre><p>在Windows系统的PowerShell中访问环境变量,需要给变量名称加上前缀(<code>$env:</code>):</p><pre class="brush:bash;toolbar:false">job_name:
script:
- echo $env:CI_JOB_ID</pre><p>您可以使用<code>export</code>命令来列出所有的环境变量。在使用此命令时要注意,此命令也会在job记录中列出所有私有变量的值:</p><pre class="brush:bash;toolbar:false">job_name:
script:
- export</pre><p>实例的值:</p><pre class="brush:bash;toolbar:false">export CI_JOB_ID="50"
export CI_COMMIT_SHA="1ecfd275763eff1d6b4844ea3168962458c9f27a"
export CI_COMMIT_REF_NAME="master"
export CI_REPOSITORY_URL="https://gitlab-ci-token:abcde-1234ABCD5678ef@example.com/gitlab-org/gitlab-ce.git"
export CI_COMMIT_TAG="1.0.0"
export CI_JOB_NAME="spec:other"
export CI_JOB_STAGE="test"
export CI_JOB_MANUAL="true"
export CI_JOB_TRIGGERED="true"
export CI_JOB_TOKEN="abcde-1234ABCD5678ef"
export CI_PIPELINE_ID="1000"
export CI_PROJECT_ID="34"
export CI_PROJECT_DIR="/builds/gitlab-org/gitlab-ce"
export CI_PROJECT_NAME="gitlab-ce"
export CI_PROJECT_NAMESPACE="gitlab-org"
export CI_PROJECT_PATH="gitlab-org/gitlab-ce"
export CI_PROJECT_URL="https://example.com/gitlab-org/gitlab-ce"
export CI_REGISTRY="registry.example.com"
export CI_REGISTRY_IMAGE="registry.example.com/gitlab-org/gitlab-ce"
export CI_RUNNER_ID="10"
export CI_RUNNER_DESCRIPTION="my runner"
export CI_RUNNER_TAGS="docker, linux"
export CI_SERVER="yes"
export CI_SERVER_NAME="GitLab"
export CI_SERVER_REVISION="70606bf"
export CI_SERVER_VERSION="8.9.0"
export GITLAB_USER_ID="42"
export GITLAB_USER_EMAIL="user@example.com"
export CI_REGISTRY_USER="gitlab-ci-token"
export CI_REGISTRY_PASSWORD="longalfanumstring"</pre><p></p>
頁:
[1]