1
0
wiki/Technology/SoftwareEngineering/自动化流程/工具使用/GitLab-Runner.html

974 lines
605 KiB
HTML
Raw Normal View History

2024-10-10 16:56:32 +08:00
<!DOCTYPE html>
<html lang="zh"><head><title>GitLab Runner</title><meta charset="utf-8"/><link rel="preconnect" href="https://fonts.googleapis.com"/><link rel="preconnect" href="https://fonts.gstatic.com"/><link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=IBM Plex Mono&amp;family=Noto Serif Simplified Chinese:wght@400;700&amp;family=Source Sans Pro:ital,wght@0,400;0,600;1,400;1,600&amp;display=swap"/><meta name="viewport" content="width=device-width, initial-scale=1.0"/><meta property="og:title" content="GitLab Runner"/><meta property="og:description" content="GitLab CI/CD 是一套自动化工具链,用于实现软件开发的持续集成和持续交付/部署。GitLab Runner 是执行这些 CI/CD 任务的开源服务,它根据 GitLab 项目中的 .gitlab-ci.yml 配置文件来运行任务。Runner 支持 Shell、Docker、Kubernetes 等多种执行环境能够并行处理作业提供日志记录和结果报告并支持缓存和构建产物artifacts来优化构建过程。 ..."/><meta property="og:image" content="https://wiki.7wate.com/static/og-image.png"/><meta property="og:width" content="1200"/><meta property="og:height" content="675"/><link rel="icon" href="../../../../static/icon.png"/><meta name="description" content="GitLab CI/CD 是一套自动化工具链,用于实现软件开发的持续集成和持续交付/部署。GitLab Runner 是执行这些 CI/CD 任务的开源服务,它根据 GitLab 项目中的 .gitlab-ci.yml 配置文件来运行任务。Runner 支持 Shell、Docker、Kubernetes 等多种执行环境能够并行处理作业提供日志记录和结果报告并支持缓存和构建产物artifacts来优化构建过程。 ..."/><meta name="generator" content="Quartz"/><link href="../../../../index.css" rel="stylesheet" type="text/css" spa-preserve/><link href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/katex.min.css" rel="stylesheet" type="text/css" spa-preserve/><script src="../../../../prescript.js" type="application/javascript" spa-preserve></script><script type="application/javascript" spa-preserve>const fetchData = fetch("../../../../static/contentIndex.json").then(data => data.json())</script></head><body data-slug="Technology/SoftwareEngineering/自动化流程/工具使用/GitLab-Runner"><div id="quartz-root" class="page"><div id="quartz-body"><div class="left sidebar"><h2 class="page-title"><a href="../../../..">🪴 X·Eden</a></h2><div class="spacer mobile-only"></div><div class="search"><button class="search-button" id="search-button"><p>搜索</p><svg role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 19.9 19.7"><title>Search</title><g class="search-path" fill="none"><path stroke-linecap="square" d="M18.5 18.3l-5.4-5.4"></path><circle cx="8" cy="8" r="7"></circle></g></svg></button><div id="search-container"><div id="search-space"><input autocomplete="off" id="search-bar" name="search" type="text" aria-label="搜索些什么" placeholder="搜索些什么"/><div id="search-layout" data-preview="true"></div></div></div></div><button class="darkmode" id="darkmode"><svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" id="dayIcon" x="0px" y="0px" viewBox="0 0 35 35" style="enable-background:new 0 0 35 35" xml:space="preserve" aria-label="暗色模式"><title>暗色模式</title><path d="M6,17.5C6,16.672,5.328,16,4.5,16h-3C0.672,16,0,16.672,0,17.5 S0.672,19,1.5,19h3C5.328,19,6,18.328,6,17.5z M7.5,26c-0.414,0-0.789,0.168-1.061,0.439l-2,2C4.168,28.711,4,29.086,4,29.5 C4,30.328,4.671,31,5.5,31c0.414,0,0.789-0.168,1.06-0.44l2-2C8.832,28.289,9,27.914,9,27.5C9,26.672,8.329,26,7.5,26z M17.5,6 C18.329,6,19,5.328,19,4.5v-3C19,0.672,18.329,0,17.5,0S16,0.672,16,1.5v3C16,5.328,16.671,6,17.5,6z M27.5,9 c0.414,0,0.789-0.168,1.06-0.439l2-2C30.832,6.289,31,5.914,31,5.5C31,4.672,30.329,4,29.5,4c-0.414,0-0.789,0.168-1.061,0.44 l-2,2C26.168,6.711,26,7.086,26,7.5C26,8.328,26.671,9,27.5,9z M6.439,8.561C6.711,8.832,7.086,9,7.5,9C8.328,9,9,8.328,9,7.5 c0-0.414-0.168-0.789-0.439-1.061l-2-2C6.289,4.16
<h3 id="基本概念和工作原理">基本概念和工作原理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#基本概念和工作原理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>GitLab CI/CD (Continuous Integration and Continuous Delivery/Deployment) 是一种自动化工具链用于软件开发过程中从代码提交到生产部署的全过程。它包括连续集成CI和连续交付/部署CD的实践。</p>
<p><img src="https://static.7wate.com/2024/08/09/5c741873f5bad.png" alt="GitLab CI/CD"/></p>
<ul>
<li><strong>Continuous Integration (CI)</strong>: CI 主要关注代码的频繁集成和测试。每当开发人员提交代码到版本控制系统时CI 系统会自动触发构建和测试流程,确保代码的正确性和质量。通过频繁集成和测试,能够快速发现和修复问题,减少集成风险。</li>
<li><strong>Continuous Delivery (CD)</strong>: CD 的目标是确保软件在任何时候都可以安全地部署到生产环境。它通过自动化的方式将经过测试的代码推送到预生产或生产环境中。CD 包括自动化的部署、测试和发布过程。</li>
<li><strong>Continuous Deployment</strong>: 是 Continuous Delivery 的延伸,它将经过验证的代码自动部署到生产环境中,无需人工干预。这种实践要求更高的自动化和监控能力。</li>
</ul>
<p><img src="https://static.7wate.com/2024/08/09/48ccee97c1a9b.jpeg" alt="ci-gitlab.jpeg"/></p>
<p><strong>GitLab CI/CD 通过 .gitlab-ci.yml 文件定义 CI/CD 流程,该文件包含了所有的构建、测试和部署步骤。</strong> GitLab Runner 是 GitLab CI/CD 的执行者,它运行在不同的环境中,执行 .gitlab-ci.yml 文件中定义的任务。</p>
<h3 id="gitlab-runner">GitLab Runner<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#gitlab-runner" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>GitLab Runner 是一个开源项目,用于运行由 GitLab CI/CD 定义的任务。它从 GitLab 服务器获取 .gitlab-ci.yml 文件中读取配置和作业并在不同的执行环境中执行这些作业。GitLab Runner 支持多种执行模式,并能与 GitLab 实例无缝集成。</p>
<ul>
<li>执行构建、测和部署任务</li>
<li>支持并行作业和多平台执行</li>
<li>提供日志记录和结果报告</li>
<li>支持缓存和 artifacts优化构建过程</li>
</ul>
<h4 id="gitlab-runner-的不同执行模式如-shelldockerkubernetes">GitLab Runner 的不同执行模式(如 Shell、Docker、Kubernetes<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#gitlab-runner-的不同执行模式如-shelldockerkubernetes" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab Runner 支持多种执行模式,每种模式适用于不同的使用场景:</p>
<div class="table-container"><table><thead><tr><th><strong>执行模式</strong></th><th><strong>描述</strong></th><th><strong>优点</strong></th><th><strong>缺点</strong></th><th><strong>适用场景</strong></th></tr></thead><tbody><tr><td><strong>Shell</strong></td><td>使用主机的 Shell 环境执行作业。</td><td>配置简单,直接使用主机环境,开销低。</td><td>缺乏环境隔离,可能影响主机系统,安全性较低。</td><td>适用于简单的构建和测试任务,不需要复杂依赖或隔离环境的场景。</td></tr><tr><td><strong>Docker</strong></td><td>在 Docker 容器中执行作业,提供隔离的执行环境。</td><td>提供环境隔离,易于管理依赖和一致性,支持多种语言和工具。</td><td>需要配置 Docker容器启动有额外开销可能增加复杂性。</td><td>需要特定依赖和环境配置的任务,如使用特定版本的编译器、库或工具的构建和测试任务。</td></tr><tr><td><strong>Docker Machine</strong></td><td>动态创建和销毁 Docker 主机,支持弹性扩展。</td><td>自动化资源管理,弹性扩展能力强,适合大规模分布式系统。</td><td>配置和管理复杂,资源使用成本较高,创建和销毁主机耗时较长。</td><td>需要弹性扩展、动态分配资源的场景,适合高并发或资源密集型的 CI/CD 管道。</td></tr><tr><td><strong>Kubernetes</strong></td><td>在 Kubernetes 集群中运行作业,提供容器编排和管理能力。</td><td>强大的容器编排和管理功能,适合复杂的部署和扩展需求。</td><td>需要熟悉 Kubernetes 的配置和管理,初始设置和维护较为复杂。</td><td>适用于大规模分布式系统和云原生应用的 CI/CD 管道,特别是在微服务架构下的持续集成和部署。</td></tr><tr><td><strong>Custom</strong></td><td>允许用户定义自定义执行器,满足特定需求。</td><td>灵活性极高,能够满足特定的、非标准的需求。</td><td>需要自行开发和维护自定义执行器,配置复杂,调试困难。</td><td>需要特殊硬件或自定义环境的场景,如 FPGA 编程、特殊设备测试,或使用非标准的执行环境。</td></tr></tbody></table></div>
<h4 id="gitlab-runner-的安装步骤和配置方法">GitLab Runner 的安装步骤和配置方法<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#gitlab-runner-的安装步骤和配置方法" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>安装 GitLab Runner 的过程因操作系统和执行模式不同而有所差异,<strong><a href="https://docs.gitlab.com/runner/install/" class="external">建议参考官方文档手册<svg aria-hidden="true" class="external-icon" viewBox="0 0 512 512"><path d="M320 0H288V64h32 82.7L201.4 265.4 178.7 288 224 333.3l22.6-22.6L448 109.3V192v32h64V192 32 0H480 320zM32 32H0V64 480v32H32 456h32V480 352 320H424v32 96H64V96h96 32V32H160 32z"></path></svg></a></strong></p>
<h3 id="gitlab-ciyml">.gitlab-ci.yml<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#gitlab-ciyml" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p><code>.gitlab-ci.yml</code> 是 GitLab 用于定义 CI/CD持续集成和持续部署管道的配置文件采用 YAML 语法编写。它位于 GitLab 项目的根目录中描述了项目的自动化构建、测试和部署流程。每次代码提交或合并时GitLab 会根据 <code>.gitlab-ci.yml</code> 文件中的配置自动触发相应的管道任务。基本结构包括以下部分:</p>
<pre><code class="mermaid">graph LR
A[.gitlab-ci.yml]
A --> B[stages]
A --> C[jobs]
A --> D[scripts]
A --> E[artifacts]
A --> F[cache]
A --> G[variables]
A --> H[dependencies]
A --> I[only/except]
A --> J[include]
B --> B1[build]
B --> B2[test]
B --> B3[deploy]
C --> C1[build-job]
C --> C2[test-job]
C --> C3[deploy-job]
D --> D1[build commands]
D --> D2[test commands]
D --> D3[deploy commands]
E --> E1[build/]
F --> F1[.m2/repository]
G --> G1[DEPLOY_ENV: production]
G --> G2[NODE_ENV: test]
H --> H1[build-job]
H --> H2[test-job]
I --> I1[only: master]
I --> I2[except: tags]
J --> J1[template: Jobs/Build.gitlab-ci.yml]
J --> J2[local: 'my-custom-config.yml']
</code></pre>
<div class="table-container"><table><thead><tr><th><strong>元素</strong></th><th><strong>描述</strong></th></tr></thead><tbody><tr><td><strong>stages</strong></td><td>定义 CI/CD 管道的不同阶段按顺序执行例如构建build、测试test和部署deploy</td></tr><tr><td><strong>jobs</strong></td><td>定义在不同阶段中执行的具体任务,每个任务必须属于某个阶段。</td></tr><tr><td><strong>scripts</strong></td><td>指定每个任务执行的命令和脚本,通常包括构建、测试和部署的具体操作。</td></tr><tr><td><strong>artifacts</strong></td><td>定义构建产物的存储和使用,通常用于保存构建结果以供后续作业使用或为后续步骤提供数据。</td></tr><tr><td><strong>cache</strong></td><td>配置缓存以优化构建速度,减少重复下载或构建的时间,尤其是在使用外部依赖时。</td></tr><tr><td><strong>variables</strong></td><td>定义环境变量,配置不同任务的执行环境,变量可以在脚本中引用,以提供灵活的配置。</td></tr><tr><td><strong>dependencies</strong></td><td>指定当前作业依赖的前一阶段作业,以确保当前作业可以访问前一作业的产物或输出。</td></tr><tr><td><strong>only/except</strong></td><td>定义作业的执行条件,<code>only</code> 指定作业只在某些情况下执行,<code>except</code> 指定作业在某些情况下不执行。</td></tr><tr><td><strong>include</strong></td><td>从外部文件或模板中引入配置,便于配置的复用和管理,常用于共享公共配置或集成现有配置文件。</td></tr></tbody></table></div>
<h4 id="阶段stages">阶段stages<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#阶段stages" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><strong>Stages</strong> 是 GitLab CI/CD 管道的核心组成部分,用于定义管道的执行顺序。每个 <code>stage</code> 代表一个阶段,按顺序执行。例如,常见的阶段包括 <code>build</code>(构建)、<code>test</code>(测试)和 <code>deploy</code>(部署)。所有在同一个 <code>stage</code> 中的作业会并行执行,整个 <code>stage</code> 成功后,管道会进入下一个阶段。</p>
<pre><code class="mermaid">graph TD
A[stages]
A --> B[build]
A --> C[test]
A --> D[deploy]
</code></pre>
<h4 id="作业jobs">作业jobs<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#作业jobs" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><strong>Jobs</strong> 是在 CI/CD 管道中实际执行的任务,每个 <code>job</code> 都属于一个特定的 <code>stage</code>。一个 <code>job</code> 通常包括编译代码、运行测试或部署应用的操作。每个 <code>job</code> 都会指定一个 <code>stage</code>并包含执行这些操作的具体命令和脚本。GitLab 会按照 <code>stage</code> 的顺序执行相应的 <code>job</code></p>
<pre><code class="mermaid">graph TB
A[jobs]
A --> B[build-job]
A --> C[test-job]
A --> D[deploy-job]
B --> B1[stage: build]
C --> C1[stage: test]
D --> D1[stage: deploy]
</code></pre>
<h4 id="脚本scripts">脚本scripts<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#脚本scripts" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><strong>Scripts</strong><code>job</code> 中的核心部分,定义了在 CI/CD 管道中执行的具体命令或操作。这些脚本可以是简单的命令,如打印消息、编译代码、运行测试,或是调用外部工具。<code>script</code> 是实现构建、测试、部署等操作的关键,通过这些命令来自动化 CI/CD 流程。</p>
<pre><code class="mermaid">graph TB
A[scripts]
A --> B[make]
A --> C[make test]
A --> D[make deploy]
</code></pre>
<h4 id="变量variables">变量Variables<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#变量variables" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>变量用于在 <code>.gitlab-ci.yml</code> 文件中定义环境变量,帮助配置不同任务的执行环境。变量可以在脚本中引用,以实现动态配置。例如,环境变量 <code>DEPLOY_ENV</code> 可以用于指定部署的目标环境。</p>
<pre><code class="mermaid">graph TB
A[variables]
A --> B[DEPLOY_ENV: production]
A --> C[NODE_ENV: test]
</code></pre>
<h4 id="缓存cache">缓存Cache<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#缓存cache" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>缓存用于保存构建过程中生成的中间文件或依赖项,减少重复下载或构建的时间,从而加快 CI/CD 管道的执行速度。例如,可以缓存 Maven 的依赖库。</p>
<pre><code class="mermaid">graph TB
A[cache]
A --> B[.m2/repository]
A --> C[node_modules/]
</code></pre>
<h4 id="构建产物artifacts">构建产物Artifacts<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#构建产物artifacts" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>Artifacts 是作业运行后生成的文件或构建产物可以在后续作业中使用或下载。Artifacts 通常用于保存构建结果、测试报告等内容。</p>
<pre><code class="mermaid">graph TB
A[artifacts]
A --> B[build/]
A --> C[dist/]
</code></pre>
<h4 id="依赖dependencies">依赖Dependencies<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#依赖dependencies" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>通过 <code>dependencies</code> 关键字,可以指定当前作业依赖于前一个阶段的某些作业。这样可以确保作业在正确的依赖产物生成后才会运行。例如,测试作业可以依赖于构建作业生成的构建产物。</p>
<pre><code class="mermaid">graph TB
A[dependencies]
A --> B[build-job]
A --> C[test-job]
</code></pre>
<h4 id="执行条件conditions">执行条件Conditions<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#执行条件conditions" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>通过 <code>only</code><code>except</code><code>when</code> 等关键字,可以指定作业的执行条件。<code>only</code><code>except</code> 用于限定作业在哪些分支或条件下执行,而 <code>when</code> 用于指定作业何时执行(例如:手动触发)。</p>
<pre><code class="mermaid">graph TB
A[执行条件]
A --> B[only]
B --> B1[master]
A --> C[except]
C --> C1[tags]
</code></pre>
<h4 id="包含includes">包含Includes<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#包含includes" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab 提供了一些预定义的 CI/CD 模板,可以通过 <code>include</code> 关键字直接在 <code>.gitlab-ci.yml</code> 文件中引用。这些模板包含常见的构建、测试和部署任务,从而复用和共享配置。这对于大型项目或需要在多个项目间共享 CI/CD 配置时非常有用。</p>
<pre><code class="mermaid">graph TB
A[include]
A --> B[template: Jobs/Build.gitlab-ci.yml]
A --> C[local: 'my-custom-config.yml']
</code></pre>
<h4 id="示例配置">示例配置<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#示例配置" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义不同的 CI/CD 阶段,按顺序执行</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义全局变量,可在所有作业中引用</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">variables</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> DEPLOY_ENV</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">production</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> NODE_ENV</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 使用缓存加速构建过程,避免重复下载依赖</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">cache</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> paths</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">.m2/repository</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">node_modules/</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 构建作业,属于 build 阶段</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">build-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 运行构建命令</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Building the project...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> artifacts</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 保存构建产物以供后续作业使用</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> paths</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build/</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> tags</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 使用带有 docker 标签的 Runner 运行作业</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 测试作业,属于 test 阶段</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">test-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 运行测试命令</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running tests...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> dependencies</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 指定依赖的作业,确保使用 build-job 的构建产物</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build-job</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> only</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 仅在 master 分支上执行测试作业</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">master</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> artifacts</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 保存测试结果</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> paths</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test-reports/</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 部署作业,属于 deploy 阶段</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 部署到生产环境</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Deploying to $DEPLOY_ENV environment...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> only</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 仅在 master 分支或带有生产标签的提交上执行部署</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">master</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> except</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 排除带有 ci-skip 标签的提交</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">tags</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 定义部署的环境</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">production</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> url</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">https://production.example.com</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 包含外部模板配置</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">include</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">template</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Jobs/Build.gitlab-ci.yml</span></span></code></pre></figure>
<h2 id="基础实践">基础实践<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#基础实践" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="创建第一个-gitlab-cicd-管道pipeline">创建第一个 GitLab CI/CD 管道Pipeline<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#创建第一个-gitlab-cicd-管道pipeline" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>创建一个简单的 .gitlab-ci.yml 文件是开始使用 GitLab CI/CD 的第一步。该文件定义了管道中的作业和阶段。以下是一个简单的示例:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">build-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Compiling the code...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">test-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running tests...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test</span></span></code></pre></figure>
<h4 id="在-gitlab-中运行第一个管道">在 GitLab 中运行第一个管道<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#在-gitlab-中运行第一个管道" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<ol>
<li>将 .gitlab-ci.yml 文件提交到项目的根目录。</li>
<li>登录 GitLab导航到项目页面。</li>
<li>在左侧菜单中选择 “CI / CD” <span></span> “Pipelines”。</li>
<li>查看新创建的管道,点击查看详细信息。</li>
</ol>
<p><strong>GitLab 会自动检测 .gitlab-ci.yml 文件并运行管道。你可以查看每个作业的日志,了解执行情况。</strong></p>
<h4 id="监控和管理管道执行">监控和管理管道执行<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#监控和管理管道执行" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<ol>
<li><strong>监控管道状态</strong>: 在 Pipelines 页面查看所有管道的状态(成功、失败、进行中)。</li>
<li><strong>查看作业日志</strong>: 点击具体作业查看详细日志,了解作业的执行情况和输出。</li>
<li><strong>重试失败的作业</strong>: 对于失败的作业,可以点击 Retry 按钮重新执行。</li>
<li><strong>取消进行中的管道</strong>: 如果需要终止进行中的管道,可以点击 Cancel 按钮。</li>
</ol>
<h3 id="使用预定义的-gitlab-ci-模板">使用预定义的 GitLab CI 模板<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用预定义的-gitlab-ci-模板" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<p>GitLab 提供了一些预定义的 CI 模板,帮助用户快速上手常见的 CI/CD 流程。这些模板涵盖了多种编程语言和框架,如 Java、Node.js、Python 等。</p>
<ul>
<li>
<p>常见模板:</p>
<ul>
<li><code>Jobs/Build.gitlab-ci.yml</code></li>
<li><code>Jobs/Test.gitlab-ci.yml</code></li>
<li><code>Jobs/Deploy.gitlab-ci.yml</code></li>
</ul>
</li>
</ul>
<h4 id="如何在项目中引用和使用预定义模板">如何在项目中引用和使用预定义模板<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#如何在项目中引用和使用预定义模板" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>你可以通过 <code>include</code> 关键字在 .gitlab-ci.yml 文件中引用预定义模板。例如下述示例中,引用了 GitLab 提供的构建模板,同时添加了一个自定义的测试作业。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">include</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">template</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Jobs/Build.gitlab-ci.yml</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">test-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running custom tests...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test</span></span></code></pre></figure>
<h4 id="根据需求自定义和扩展模板">根据需求自定义和扩展模板<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#根据需求自定义和扩展模板" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>使用预定义模板后,你可以根据项目的具体需求进行自定义和扩展。例如通过自定义模板,你可以在标准流程基础上添加特定的测试和部署逻辑。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">include</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">template</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Jobs/Build.gitlab-ci.yml</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">test-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running custom tests...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Deploying to production...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> only</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">master</span></span></code></pre></figure>
<h3 id="运行基本的-ci-流程如构建测试">运行基本的 CI 流程(如构建、测试)<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#运行基本的-ci-流程如构建测试" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="在管道中定义和运行构建作业">在管道中定义和运行构建作业<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#在管道中定义和运行构建作业" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>构建作业是 CI 流程的核心部分,通常包括编译代码和生成构建产物。以下是一个构建作业的示例:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">build-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 定义构建过程中需要执行的命令。</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Compiling the code...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 定义构建产物的路径,供后续作业使用。</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> artifacts</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> paths</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build/</span></span></code></pre></figure>
<h4 id="集成单元测试和集成测试">集成单元测试和集成测试<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#集成单元测试和集成测试" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>测试作业用于运行单元测试和集成测试,确保代码的正确性。以下是一个测试作业的示例:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 运行单元测试</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">unit-test-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running unit tests...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test-unit</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 运行集成测试</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">integration-test-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running integration tests...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test-integration</span></span></code></pre></figure>
<h4 id="查看和分析测试结果">查看和分析测试结果<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#查看和分析测试结果" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab CI/CD 提供了详细的测试结果报告,帮助开发人员分析和解决问题。</p>
<ol>
<li><strong>查看测试日志</strong>: 在 Pipelines 页面点击具体作业,查看测试的详细日志和输出。</li>
<li><strong>分析测试结果</strong>: 使用 GitLab 提供的测试报告工具,如 JUnit 报告,查看测试通过率和失败的测试用例。</li>
</ol>
<h4 id="使用-gitlab-ci-的代码质量分析功能">使用 GitLab CI 的代码质量分析功能<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-gitlab-ci-的代码质量分析功能" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab CI/CD 提供了代码质量分析功能,帮助识别代码中的潜在问题和改进点。以下是一个代码质量分析的示例:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">code_quality</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">code_quality</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">code_quality</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running code quality checks...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">eslint .</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 使用 ESLint 等工具运行代码质量检查。</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 生成代码质量报告并存储在 GitLab 中,供后续分析和查看。</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> artifacts</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> reports</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> codequality</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">gl-code-quality-report.json</span></span></code></pre></figure>
<h2 id="进阶技能">进阶技能<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#进阶技能" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="使用-gitlab-runner-执行自定义作业">使用 GitLab Runner 执行自定义作业<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-gitlab-runner-执行自定义作业" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="编写和配置自定义作业脚本">编写和配置自定义作业脚本<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#编写和配置自定义作业脚本" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>自定义作业脚本使得 CI/CD 流程能够灵活适应不同的项目需求。通过在 <code>.gitlab-ci.yml</code> 文件中定义脚本,可以执行特定任务,例如编译、测试、部署等。以下是一个示例,在 <code>script</code> 部分中,可以编写任意 Bash 命令或调用外部脚本文件,以执行具体的任务。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">build-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Compiling the code...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">test-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running tests...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Deploying to production...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make deploy</span></span></code></pre></figure>
<h4 id="在不同执行环境中运行自定义作业">在不同执行环境中运行自定义作业<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#在不同执行环境中运行自定义作业" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab Runner 支持多种执行环境,使得 CI/CD 管道可以在不同的操作系统和容器中运行。以下是一些常见的执行环境配置示例:</p>
<h5 id="shell-执行环境">Shell 执行环境<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#shell-执行环境" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">build-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running in shell...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> tags</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">shell</span></span></code></pre></figure>
<h5 id="docker-执行环境">Docker 执行环境<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#docker-执行环境" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">build-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">maven:3.6.3-jdk-8</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running in Docker...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mvn install</span></span></code></pre></figure>
<h5 id="kubernetes-执行环境">Kubernetes 执行环境<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#kubernetes-执行环境" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">build-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running in Kubernetes...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">kubectl apply -f deployment.yaml</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> tags</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">kubernetes</span></span></code></pre></figure>
<h4 id="利用并行作业和矩阵构建提高效率">利用并行作业和矩阵构建提高效率<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#利用并行作业和矩阵构建提高效率" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>并行作业和矩阵构建使得多个作业可以同时运行,从而加快 CI/CD 管道的执行速度。以下是并行作业和矩阵构建的示例:</p>
<h5 id="并行作业">并行作业<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#并行作业" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">test-job-1</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running test job 1...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test-1</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">test-job-2</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running test job 2...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test-2</span></span></code></pre></figure>
<h5 id="矩阵构建">矩阵构建<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#矩阵构建" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">test-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running tests...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> parallel</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> matrix</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">TEST_ENV</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;python2.7&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;python3.6&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">DB</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;mysql&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;postgres&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span></code></pre></figure>
<h3 id="使用-gitlab-secrets-进行安全管理">使用 GitLab Secrets 进行安全管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-gitlab-secrets-进行安全管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="gitlab-cicd-中的秘密变量和环境变量管理">GitLab CI/CD 中的秘密变量和环境变量管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#gitlab-cicd-中的秘密变量和环境变量管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab 提供了管理秘密变量和环境变量的功能,以确保敏感信息(如 API 密钥、密码等)不会泄露。可以在 GitLab 项目或组的设置中定义这些变量。</p>
<p><strong>定义秘密变量:</strong></p>
<ol>
<li>进入项目设置: Settings <span></span> CI/CD <span></span> Variables</li>
<li>点击 “Add Variable”</li>
<li>输入变量名称和值,并选择保护选项(如保护、掩码)</li>
</ol>
<h4 id="在-gitlab-ciyml-中安全使用秘密">在 .gitlab-ci.yml 中安全使用秘密<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#在-gitlab-ciyml-中安全使用秘密" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p><code>.gitlab-ci.yml</code> 文件中,可以通过 <code>${VARIABLE_NAME}</code> 的形式引用环境变量和秘密变量。通过这种方式,可以确保敏感信息不会暴露在代码库中。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Deploying to production...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy --api-key=${API_KEY} --password=${PASSWORD}</span></span></code></pre></figure>
<h4 id="配置和管理-gitlab-项目和组的秘密">配置和管理 GitLab 项目和组的秘密<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置和管理-gitlab-项目和组的秘密" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab 允许在项目和组级别管理秘密变量,便于多个项目共享相同的配置。</p>
<h5 id="项目级变量">项目级变量<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#项目级变量" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<ol>
<li>进入项目设置: Settings <span></span> CI/CD <span></span> Variables</li>
<li>添加或编辑变量</li>
</ol>
<h5 id="组级变量">组级变量<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#组级变量" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<ol>
<li>进入组设置: Group Settings <span></span> CI/CD <span></span> Variables</li>
<li>添加或编辑变量</li>
</ol>
<h3 id="多阶段管道和依赖管理">多阶段管道和依赖管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#多阶段管道和依赖管理" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="定义多阶段multi-stage管道">定义多阶段multi-stage管道<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#定义多阶段multi-stage管道" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>多阶段管道通过定义不同的阶段(如 build、test、deploy来组织 CI/CD 流程。以下是一个多阶段管道的示例:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">build-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Building the project...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make build</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">test-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Testing the project...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Deploying the project...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make deploy</span></span></code></pre></figure>
<h4 id="阶段之间的依赖管理和条件执行">阶段之间的依赖管理和条件执行<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#阶段之间的依赖管理和条件执行" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>**通过定义阶段之间的依赖关系,可以控制作业的执行顺序和条件执行。**例如,可以使用 <code>needs</code> 关键字定义依赖关系:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">build-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Building the project...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make build</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">test-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Testing the project...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> needs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;build-job&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Deploying the project...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> only</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">master</span></span></code></pre></figure>
<h4 id="使用-needs-关键字优化管道执行顺序">使用 Needs 关键字优化管道执行顺序<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-needs-关键字优化管道执行顺序" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>使用 <code>needs</code> 关键字可以显著优化管道的执行顺序,避免不必要的等待时间:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">build-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Building the project...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make build</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">unit-test-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running unit tests...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test-unit</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> needs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;build-job&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">integration-test-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running integration tests...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test-integration</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> needs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;build-job&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Deploying the project...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> needs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;unit-test-job&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;integration-test-job&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span></code></pre></figure>
<h3 id="集成第三方服务如-docker-registrykubernetes">集成第三方服务(如 Docker Registry、Kubernetes<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#集成第三方服务如-docker-registrykubernetes" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="配置和使用-gitlab-内置的-docker-registry">配置和使用 GitLab 内置的 Docker Registry<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置和使用-gitlab-内置的-docker-registry" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab 提供内置的 Docker Registry方便用户存储和管理 Docker 镜像。以下是配置和使用的步骤:</p>
<ol>
<li>
<p><strong>启用 Docker Registry</strong>:</p>
<ul>
<li>进入项目设置: Settings <span></span> CI/CD <span></span> Container Registry</li>
<li>确认 Docker Registry 已启用</li>
</ul>
</li>
<li>
<p><strong>登录 Docker Registry</strong>:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="shell" data-theme="github-light github-dark"><code data-language="shell" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> docker</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> login</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> registry.gitlab.com</span></span></code></pre></figure>
</li>
<li>
<p><strong>构建和推送 Docker 镜像</strong>:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">build-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker:latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker:dind</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker build -t registry.gitlab.com/your-namespace/your-project:latest .</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker push registry.gitlab.com/your-namespace/your-project:latest</span></span></code></pre></figure>
</li>
</ol>
<h4 id="在管道中构建和推送-docker-镜像">在管道中构建和推送 Docker 镜像<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#在管道中构建和推送-docker-镜像" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>通过在管道中构建和推送 Docker 镜像,可以实现自动化的容器化部署:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">build-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker:latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> services</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker:dind</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker build -t registry.gitlab.com/your-namespace/your-project:latest .</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker push registry.gitlab.com/your-namespace/your-project:latest</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker pull registry.gitlab.com/your-namespace/your-project:latest</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">docker run -d registry.gitlab.com/your-namespace/your-project:latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> only</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">master</span></span></code></pre></figure>
<h4 id="集成-kubernetes-进行持续部署">集成 Kubernetes 进行持续部署<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#集成-kubernetes-进行持续部署" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab CI/CD 可以与 Kubernetes 集成,实现自动化的持续部署:</p>
<ol>
<li>
<p><strong>配置 Kubernetes 集群</strong>:</p>
<ul>
<li>在 GitLab 项目的 Kubernetes 集成页面添加集群信息</li>
</ul>
</li>
<li>
<p><strong>在管道中使用 Kubernetes</strong>:</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">kubectl apply -f deployment.yaml</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> tags</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">kubernetes</span></span></code></pre></figure>
</li>
</ol>
<h4 id="使用-gitlab-kubernetes-集群管理工具">使用 GitLab Kubernetes 集群管理工具<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-gitlab-kubernetes-集群管理工具" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab 提供了 Kubernetes 集群管理工具,简化集群的配置和管理:</p>
<ol>
<li><strong>添加 Kubernetes 集群</strong>:
<ul>
<li>进入项目设置: Operations <span></span> Kubernetes</li>
<li>添加集群信息API URL、CA 证书、Token 等)</li>
</ul>
</li>
<li><strong>管理集群</strong>:
<ul>
<li>使用 GitLab 提供的界面管理集群中的资源,如 Pods、Services 等</li>
<li>配置自动化的 CI/CD 管道,实现持续部署</li>
</ul>
</li>
</ol>
<h2 id="高级实践">高级实践<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#高级实践" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h2>
<h3 id="持续交付cd流程">持续交付CD流程<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#持续交付cd流程" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="定义和实现持续交付cd管道">定义和实现持续交付CD管道<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#定义和实现持续交付cd管道" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>持续交付CD是指将代码在经过一系列自动化测试后自动部署到预生产或生产环境。以下是一个持续交付管道的示例</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 定义构建阶段</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 定义测试阶段</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 定义部署阶段</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">build-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 指定作业所属的阶段</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Building the project...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> artifacts</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> paths</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build/</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 保存构建产物供后续使用</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">test-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 指定作业所属的阶段</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Running tests...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> dependencies</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build-job</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 使用构建阶段的产物</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 指定作业所属的阶段</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Deploying to production...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> only</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">master</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 仅在 master 分支上执行部署</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">production</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 部署环境名称</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> url</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">https://production.example.com</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 部署环境的 URL</span></span>
<span data-line> </span></code></pre></figure>
<h4 id="配置环境部署策略">配置环境部署策略<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置环境部署策略" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab 提供了丰富的环境和部署策略配置选项,确保部署过程的安全性和稳定性。</p>
<h5 id="动态环境">动态环境<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#动态环境" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>用于临时创建环境,进行测试或演示。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">environment</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">review/$CI_COMMIT_REF_NAME</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> url</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">https://$CI_COMMIT_REF_NAME.example.com</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> on_stop</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">stop_review</span></span></code></pre></figure>
<h5 id="手动部署">手动部署<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#手动部署" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>需要手动确认才能执行部署。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Deploying to production...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> when</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">manual</span></span></code></pre></figure>
<h4 id="使用-gitlab-的环境和部署管理功能">使用 GitLab 的环境和部署管理功能<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-gitlab-的环境和部署管理功能" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab 提供环境和部署管理功能,帮助开发者和运维人员轻松管理不同的部署环境。</p>
<ul>
<li><strong>查看环境状态</strong>: 在项目的 Environments 页面查看所有环境的状态和部署历史。</li>
<li><strong>手动触发部署</strong>: 在 Environments 页面手动触发部署任务。</li>
<li><strong>环境保护</strong>: 配置环境保护规则,限制谁可以部署到特定环境。</li>
</ul>
<h4 id="回滚和部署策略优化">回滚和部署策略优化<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#回滚和部署策略优化" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>部署过程中可能会遇到问题,需要回滚到之前的版本。可以通过以下方式实现回滚和优化部署策略:</p>
<p><strong>自动回滚</strong>: 在部署失败时自动回滚到上一个稳定版本。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Deploying to production...&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">make deploy || make rollback</span></span></code></pre></figure>
<ul>
<li>
<p><strong>蓝绿部署</strong>: 同时运行两个版本的应用,切换流量以实现无缝升级。</p>
</li>
<li>
<p><strong>金丝雀发布</strong>: 部分用户先访问新版本,观察其稳定性,然后逐步扩展。</p>
</li>
</ul>
<h3 id="使用缓存和-artifacts-提高构建效率">使用缓存和 Artifacts 提高构建效率<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用缓存和-artifacts-提高构建效率" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="配置和使用缓存加速构建过程">配置和使用缓存加速构建过程<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置和使用缓存加速构建过程" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>缓存可以显著加快构建过程,通过缓存依赖和构建中间结果,减少重复工作。</p>
<h5 id="配置缓存">配置缓存<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置缓存" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">cache</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> paths</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">.m2/repository</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">node_modules/</span></span></code></pre></figure>
<h5 id="清理缓存">清理缓存<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#清理缓存" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>在需要时清理缓存以确保使用最新的依赖。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">cache</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> key</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;$CI_COMMIT_REF_SLUG&quot;</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> paths</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">.m2/repository</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> policy</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">push</span></span></code></pre></figure>
<h4 id="定义和管理-artifacts">定义和管理 Artifacts<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#定义和管理-artifacts" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>Artifacts 是管道运行过程中生成的文件,通常用于后续阶段或作为构建产物存储。并在 GitLab 界面查看和下载 artifacts。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">artifacts</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> paths</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build/</span></span></code></pre></figure>
<h4 id="优化缓存和-artifacts-的存储策略">优化缓存和 Artifacts 的存储策略<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#优化缓存和-artifacts-的存储策略" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>为了更好地利用缓存和 artifacts优化存储策略非常重要。</p>
<h5 id="按分支缓存">按分支缓存<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#按分支缓存" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>不同分支使用不同的缓存,避免相互影响。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">cache</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> key</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;$CI_COMMIT_REF_NAME&quot;</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> paths</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">.m2/repository</span></span></code></pre></figure>
<h5 id="长期存储">长期存储<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#长期存储" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<p>重要的构建产物可以配置长期存储,避免被清理。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">artifacts</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> paths</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build/</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> expire_in</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">1 month</span></span></code></pre></figure>
<h3 id="部署到-kubernetes-和其他云平台">部署到 Kubernetes 和其他云平台<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#部署到-kubernetes-和其他云平台" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="使用-gitlab-cicd-管道部署到-kubernetes-集群">使用 GitLab CI/CD 管道部署到 Kubernetes 集群<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-gitlab-cicd-管道部署到-kubernetes-集群" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>将应用部署到 Kubernetes 集群,可以利用其强大的容器编排和管理能力。</p>
<p><strong>配置 Kubernetes 集群</strong>:</p>
<ol>
<li>在 GitLab 项目的 Kubernetes 集成页面添加集群信息。</li>
<li>配置 <code>kubeconfig</code> 文件,用于访问 Kubernetes 集群。</li>
</ol>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">kubectl apply -f k8s/deployment.yaml</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">kubectl apply -f k8s/service.yaml</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> tags</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">kubernetes</span></span></code></pre></figure>
<h4 id="配置和使用-gitlab-的-auto-devops-功能">配置和使用 GitLab 的 Auto DevOps 功能<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#配置和使用-gitlab-的-auto-devops-功能" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>Auto DevOps 是 GitLab 提供的自动化 CI/CD 流程,适用于常见的开发场景。启用 Auto DevOps 后GitLab 会自动检测并运行合适的 CI/CD 管道。</p>
<ul>
<li><strong>启用 Auto DevOps</strong>:
<ol>
<li>在项目设置页面启用 Auto DevOps。</li>
<li>配置 Kubernetes 集群和相关的 CI/CD 设置。</li>
</ol>
</li>
<li><strong>定制 Auto DevOps</strong>: 可以通过自定义 <code>.gitlab-ci.yml</code> 文件来扩展和定制 Auto DevOps 流程。</li>
</ul>
<h4 id="集成其他云平台进行部署">集成其他云平台进行部署<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#集成其他云平台进行部署" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>通过 GitLab CI/CD 管道,可以将应用部署到各种云平台。</p>
<h5 id="部署到-aws">部署到 AWS<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#部署到-aws" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">amazon/aws-cli</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">aws s3 cp build/ s3://my-bucket/ --recursive</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> only</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">master</span></span></code></pre></figure>
<h5 id="部署到-gcp">部署到 GCP<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#部署到-gcp" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">google/cloud-sdk</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">gcloud auth activate-service-account --key-file $GCP_KEY_FILE</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">gcloud app deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> only</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">master</span></span></code></pre></figure>
<h5 id="部署到-azure">部署到 Azure<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#部署到-azure" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h5>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="yaml" data-theme="github-light github-dark"><code data-language="yaml" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">stages</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy-job</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> stage</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mcr.microsoft.com/azure-cli</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> script</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">az login --service-principal -u $AZURE_CLIENT_ID -p $AZURE_CLIENT_SECRET --tenant $AZURE_TENANT_ID</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">az webapp up --name myapp --resource-group myResourceGroup --location &quot;East US&quot;</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> only</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">master</span></span></code></pre></figure>
<h3 id="性能优化和故障排查">性能优化和故障排查<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#性能优化和故障排查" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h3>
<h4 id="优化管道执行时间和资源使用">优化管道执行时间和资源使用<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#优化管道执行时间和资源使用" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>通过以下方法可以优化 CI/CD 管道的执行时间和资源使用:</p>
<ul>
<li><strong>并行执行</strong>: 将作业并行化,减少整体执行时间。</li>
<li><strong>缓存利用</strong>: 使用缓存减少重复工作。</li>
<li><strong>按需执行</strong>: 使用 <code>only</code><code>except</code> 关键字控制作业执行条件。</li>
</ul>
<h4 id="监控和分析管道性能指标">监控和分析管道性能指标<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#监控和分析管道性能指标" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab 提供了多种监控和分析工具,帮助优化管道性能:</p>
<ul>
<li><strong>Pipeline Analytics</strong>: 查看管道执行时间和成功率等指标。</li>
<li><strong>Job Artifacts</strong>: 分析作业的日志和生成的报告。</li>
<li><strong>GitLab CI/CD Monitoring</strong>: 使用外部监控工具(如 Prometheus、Grafana集成 GitLab 的 CI/CD 监控。</li>
</ul>
<h4 id="常见错误和问题的排查方法">常见错误和问题的排查方法<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#常见错误和问题的排查方法" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>常见错误包括作业失败、超时、依赖问题等。排查方法如下:</p>
<ul>
<li><strong>查看日志</strong>: 详细查看作业日志,分析错误信息。</li>
<li><strong>重试作业</strong>: 对于临时性问题,可以尝试重试作业。</li>
<li><strong>环境检查</strong>: 确认执行环境的配置和依赖是否正确。</li>
</ul>
<h4 id="使用-gitlab-cicd-的调试和日志功能">使用 GitLab CI/CD 的调试和日志功能<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-gitlab-cicd-的调试和日志功能" class="internal"><svg width="18" height="18" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"></path><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"></path></svg></a></h4>
<p>GitLab CI/CD 提供了丰富的调试和日志功能,帮助定位和解决问题:</p>
<ul>
<li>
<p><strong>Debug 作业</strong>: 在 <code>.gitlab-ci.yml</code> 中添加调试信息。</p>
<pre><code>script:
- echo &quot;Debug info:&quot;
- env
- make test
</code></pre>
</li>
<li>
<p><strong>查看作业日志</strong>: 在 GitLab 界面查看详细的作业执行日志。</p>
</li>
<li>
<p><strong>使用 SSH 访问 Runner</strong>: 在调试模式下使用 SSH 访问 Runner进行更深入的调试。</p>
</li>
</ul></article><hr/><div class="page-footer"></div></div><div class="right sidebar"><div class="graph"><h3>关系图谱</h3><div class="graph-outer"><div id="graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:1,&quot;scale&quot;:1.1,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:false}"></div><button id="global-graph-icon" aria-label="Global Graph"><svg version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" viewBox="0 0 55 55" fill="currentColor" xml:space="preserve"><path d="M49,0c-3.309,0-6,2.691-6,6c0,1.035,0.263,2.009,0.726,2.86l-9.829,9.829C32.542,17.634,30.846,17,29,17
s-3.542,0.634-4.898,1.688l-7.669-7.669C16.785,10.424,17,9.74,17,9c0-2.206-1.794-4-4-4S9,6.794,9,9s1.794,4,4,4
c0.74,0,1.424-0.215,2.019-0.567l7.669,7.669C21.634,21.458,21,23.154,21,25s0.634,3.542,1.688,4.897L10.024,42.562
C8.958,41.595,7.549,41,6,41c-3.309,0-6,2.691-6,6s2.691,6,6,6s6-2.691,6-6c0-1.035-0.263-2.009-0.726-2.86l12.829-12.829
c1.106,0.86,2.44,1.436,3.898,1.619v10.16c-2.833,0.478-5,2.942-5,5.91c0,3.309,2.691,6,6,6s6-2.691,6-6c0-2.967-2.167-5.431-5-5.91
v-10.16c1.458-0.183,2.792-0.759,3.898-1.619l7.669,7.669C41.215,39.576,41,40.26,41,41c0,2.206,1.794,4,4,4s4-1.794,4-4
s-1.794-4-4-4c-0.74,0-1.424,0.215-2.019,0.567l-7.669-7.669C36.366,28.542,37,26.846,37,25s-0.634-3.542-1.688-4.897l9.665-9.665
C46.042,11.405,47.451,12,49,12c3.309,0,6-2.691,6-6S52.309,0,49,0z M11,9c0-1.103,0.897-2,2-2s2,0.897,2,2s-0.897,2-2,2
S11,10.103,11,9z M6,51c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S8.206,51,6,51z M33,49c0,2.206-1.794,4-4,4s-4-1.794-4-4
s1.794-4,4-4S33,46.794,33,49z M29,31c-3.309,0-6-2.691-6-6s2.691-6,6-6s6,2.691,6,6S32.309,31,29,31z M47,41c0,1.103-0.897,2-2,2
s-2-0.897-2-2s0.897-2,2-2S47,39.897,47,41z M49,10c-2.206,0-4-1.794-4-4s1.794-4,4-4s4,1.794,4,4S51.206,10,49,10z"></path></svg></button></div><div id="global-graph-outer"><div id="global-graph-container" data-cfg="{&quot;drag&quot;:true,&quot;zoom&quot;:true,&quot;depth&quot;:-1,&quot;scale&quot;:0.9,&quot;repelForce&quot;:0.5,&quot;centerForce&quot;:0.3,&quot;linkDistance&quot;:30,&quot;fontSize&quot;:0.6,&quot;opacityScale&quot;:1,&quot;showTags&quot;:true,&quot;removeTags&quot;:[],&quot;focusOnHover&quot;:true}"></div></div></div><div class="toc desktop-only"><button type="button" id="toc" class aria-controls="toc-content" aria-expanded="true"><h3>目录</h3><svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="fold"><polyline points="6 9 12 15 18 9"></polyline></svg></button><div id="toc-content" class><ul class="overflow"><li class="depth-0"><a href="#gitlab-cicd" data-for="gitlab-cicd">GitLab CI/CD</a></li><li class="depth-1"><a href="#基本概念和工作原理" data-for="基本概念和工作原理">基本概念和工作原理</a></li><li class="depth-1"><a href="#gitlab-runner" data-for="gitlab-runner">GitLab Runner</a></li><li class="depth-1"><a href="#gitlab-ciyml" data-for="gitlab-ciyml">.gitlab-ci.yml</a></li><li class="depth-0"><a href="#基础实践" data-for="基础实践">基础实践</a></li><li class="depth-1"><a href="#创建第一个-gitlab-cicd-管道pipeline" data-for="创建第一个-gitlab-cicd-管道pipeline">创建第一个 GitLab CI/CD 管道Pipeline</a></li><li class="depth-1"><a href="#使用预定义的-gitlab-ci-模板" data-for="使用预定义的-gitlab-ci-模板">使用预定义的 GitLab CI 模板</a></li><li class="depth-1"><a href="#运行基本的-ci-流程如构建测试" data-for="运行基本的-ci-流程如构建测试">运行基本的 CI 流程(如构建、测试)</a></li><li class="depth-0"><a href="#进阶技能" data-for="进阶技能">进阶技能</a></li><li class="depth-1"><a href="#使用-gitlab-runner-执行自定义作业" data-for="使用-gitlab-runner-执行自定义作业">使用 GitLab Runner 执行自定义作业</a></li><li class="depth-1"><a href="#使用-gitlab-secrets-进行安全管理" data-for="使用-gitlab-secrets-进行安全管理">使用 GitLab Secrets 进行安全管理</a></li><li class="depth-1"><a href="#多阶段管道和依赖管理" data-for="多阶段管道和依赖管理">多阶段管道和依赖管理</a></li><li class="depth-1"><a href="#集成第三方服务如-docker-registrykubernetes" data-for="集成第三方服务如-docker-registrykubernetes">集成第三方服务(如 Docker Registry、Kubernetes</a></li><li class="depth-0"><a href="#高级实践" data-for="高级实践">高级实践</a></li><li class="depth-1"><a href="#持续交付cd流程" data-for="持续交付cd流程">持续交付CD流程</a></li><li class="depth-1"><a href="#使用缓存和-artifacts-提高构建效率" data-for="使用缓存和-artifacts-提高构建效率">使用缓存和 Artifacts 提高构建效率</a></li><li class="depth-1"><a href="#部署到-kubernetes-和其他云平台" data-for="部署到-kubernetes-和其他云平台">部署到 Kubernetes 和其他云平台</a></li><li class="depth-1"><a href="#性能优化和故障排查" data-for="性能优化和故障排查">性能优化和故障排查</a></li></ul></div></div><div class="explorer mobile-only"><button type="button" id="explorer" data-behavior="collapse" data-collapsed="collapsed" data-savestate="true" data-tree="[{&quot;path&quot;:&quot;Personal&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2018&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2020&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2021&quot;,&quot;collapsed&quot;:true},{&quot;path&quot;:&quot;Personal/Blog/2022&quot;,&qu
</script><script type="module">
let mermaidImport = undefined
document.addEventListener('nav', async () => {
if (document.querySelector("code.mermaid")) {
mermaidImport ||= await import('https://cdnjs.cloudflare.com/ajax/libs/mermaid/10.7.0/mermaid.esm.min.mjs')
const mermaid = mermaidImport.default
const darkMode = document.documentElement.getAttribute('saved-theme') === 'dark'
mermaid.initialize({
startOnLoad: false,
securityLevel: 'loose',
theme: darkMode ? 'dark' : 'default'
})
await mermaid.run({
querySelector: '.mermaid'
})
}
});
</script><script src="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.9/contrib/copy-tex.min.js" type="application/javascript"></script><script src="../../../../postscript.js" type="module"></script></html>