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

1589 lines
669 KiB
HTML
Raw Normal View History

2024-10-10 16:56:32 +08:00
<!DOCTYPE html>
<html lang="zh"><head><title>GitHub Actions</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="GitHub Actions"/><meta property="og:description" content="GitHub Actions 是 GitHub 提供的持续集成和持续交付CI/CD平台允许开发者自动化他们的软件项目构建、测试和部署流程。通过在 GitHub 仓库中定义 YAML 格式的工作流文件,可以响应如代码提交或发布标签等 GitHub 事件来触发任务执行。GitHub Actions 支持跨平台运行,具备事件驱动、易于编写的 ..."/><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="GitHub Actions 是 GitHub 提供的持续集成和持续交付CI/CD平台允许开发者自动化他们的软件项目构建、测试和部署流程。通过在 GitHub 仓库中定义 YAML 格式的工作流文件,可以响应如代码提交或发布标签等 GitHub 事件来触发任务执行。GitHub Actions 支持跨平台运行,具备事件驱动、易于编写的 ..."/><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/自动化流程/工具使用/GitHub-Actions"><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.168,5.914,4,5.5,4C4.672,4,4,4.672,4,5.5c0,0.414,0.168,0.789,0.439,1.06 L6.439,8.561z M33.5,16h-3c-0.828,0-1.5,
<p><a href="https://docs.github.com/en/actions" class="external">GitHub Actions<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> 是一个持续集成和持续交付CI/CD平台它允许开发者在 GitHub 仓库中自动化构建、测试和部署软件项目。通过定义工作流文件workflow开发者可以在特定事件如代码提交或发布标签触发时执行一系列任务。</p>
<p>GitHub Actions 的主要特性:</p>
<ul>
<li><strong>事件驱动</strong>:工作流可以基于 GitHub 上的各种事件(如 push、pull request、issue触发。</li>
<li><strong>基于 YAML 文件</strong>:使用易于编写和阅读的 YAML 文件定义工作流。</li>
<li><strong>跨平台支持</strong>:支持在 Linux、macOS 和 Windows 环境下运行作业。</li>
<li><strong>安全性</strong>:通过 GitHub Secrets 管理敏感信息,确保工作流的安全性。</li>
</ul>
<p>GitHub Actions 的主要用途:</p>
<ul>
<li><strong>持续集成CI</strong>:在代码每次变更时自动运行构建和测试流程,确保代码的正确性和稳定性。</li>
<li><strong>持续交付CD</strong>:将代码自动部署到开发、测试或生产环境,确保软件可以快速、安全地发布。</li>
<li><strong>自动化任务</strong>:执行其他重复性任务,如代码格式化、依赖管理、安全扫描等。</li>
</ul>
<h2 id="github-actions-基本概念">GitHub Actions 基本概念<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#github-actions-基本概念" 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="工作流workflow">工作流Workflow<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#工作流workflow" 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><strong>工作流Workflow是 GitHub Actions 的核心单元</strong>,通过 YAML 文件定义,位于 <code>.github/workflows/</code> 目录下。工作流文件描述了自动化流程的具体操作步骤包括何时触发on、在何种环境中运行runs-on、以及执行的任务jobs 和 steps</p>
<pre><code class="mermaid">graph TD
A[Workflow]
A --> B1[name]
A --> B2[on]
B2 --> C1[push]
B2 --> C2[pull_request]
B2 --> C3[schedule]
B2 --> C4[workflow_dispatch]
A --> B3[jobs]
B3 --> D1[Job 1]
B3 --> D2[Job 2]
D1 --> E1[runs-on]
D1 --> E2[needs]
D1 --> E3[env]
D1 --> E4[steps]
E4 --> F1[Step 1]
E4 --> F2[Step 2]
F1 --> G1[uses]
F1 --> G2[run]
F1 --> G3[env]
F1 --> G4[with]
F2 --> G5[uses]
F2 --> G6[run]
F2 --> G7[env]
F2 --> G8[with]
D2 --> H1[runs-on]
D2 --> H2[needs]
D2 --> H3[env]
D2 --> H4[steps]
H4 --> I1[Step 1]
H4 --> I2[Step 2]
I1 --> J1[uses]
I1 --> J2[run]
I1 --> J3[env]
I1 --> J4[with]
I2 --> J5[uses]
I2 --> J6[run]
I2 --> J7[env]
I2 --> J8[with]
</code></pre>
<div class="table-container"><table><thead><tr><th>关键字</th><th>描述</th><th>示例</th></tr></thead><tbody><tr><td><code>name</code></td><td>工作流的名称(可选)</td><td><code>name: CI/CD Pipeline</code></td></tr><tr><td><code>on</code></td><td>定义触发工作流的事件</td><td><code>on: [push, pull_request, schedule, workflow_dispatch]</code></td></tr><tr><td><code>push</code></td><td>代码推送事件</td><td><code>push: { branches: [main] }</code></td></tr><tr><td><code>pull_request</code></td><td>拉取请求事件</td><td><code>pull_request: { branches: [main] }</code></td></tr><tr><td><code>schedule</code></td><td>定时器触发</td><td><code>schedule: [{ cron: '0 0 * * *' }]</code></td></tr><tr><td><code>workflow_dispatch</code></td><td>手动触发工作流</td><td><code>workflow_dispatch:</code></td></tr><tr><td><code>jobs</code></td><td>包含一个或多个作业</td><td><code>jobs: { build: { ... }, test: { ... }, deploy: { ... } }</code></td></tr><tr><td><code>runs-on</code></td><td>指定作业运行的虚拟环境</td><td><code>runs-on: ubuntu-latest</code></td></tr><tr><td><code>needs</code></td><td>指定作业的依赖关系</td><td><code>needs: build</code></td></tr><tr><td><code>env</code></td><td>设置环境变量(作业级或步骤级)</td><td><code>env: { NODE_ENV: production }</code></td></tr><tr><td><code>steps</code></td><td>作业中的具体操作步骤</td><td><code>steps: [ { name: Checkout code, uses: actions/checkout@v2 }, ... ]</code></td></tr><tr><td><code>name</code> (Step)</td><td>步骤的名称</td><td><code>name: Checkout code</code></td></tr><tr><td><code>uses</code></td><td>调用预定义的 action</td><td><code>uses: actions/checkout@v2</code></td></tr><tr><td><code>run</code></td><td>运行命令行命令</td><td><code>run: npm install</code></td></tr><tr><td><code>with</code></td><td>传递给 action 的参数</td><td><code>with: { node-version: '14' }</code></td></tr></tbody></table></div>
<h3 id="作业job">作业Job<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#作业job" 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>**作业Job是工作流中独立运行的一组步骤。**作业可以在不同的虚拟环境中运行,并且可以并行或按顺序执行;作业之间可以是独立的,也可以相互依赖。</p>
<pre><code class="mermaid">graph TD
A[jobs]
A --> B1[Job 1]
A --> B2[Job 2]
B1 --> C1[runs-on]
B1 --> C2[steps]
B1 --> C3[needs] --> D1[依赖的Job]
B1 --> C4[env] --> D2[环境变量]
C2 --> D3[Step 1]
C2 --> D4[Step 2]
D3 --> E1[uses]
D3 --> E2[run]
D3 --> E3[name]
D3 --> E4[with] --> F1[参数]
D3 --> E5[env] --> F2[环境变量]
D3 --> E6[if] --> F3[条件]
D4 --> G1[uses]
D4 --> G2[run]
D4 --> G3[name]
D4 --> G4[with] --> H1[参数]
D4 --> G5[env] --> H2[环境变量]
D4 --> G6[if] --> H3[条件]
B2 --> I1[runs-on]
B2 --> I2[steps]
B2 --> I3[needs] --> J1[依赖的Job]
B2 --> I4[env] --> J2[环境变量]
I2 --> K1[Step 1]
I2 --> K2[Step 2]
K1 --> L1[uses]
K1 --> L2[run]
K1 --> L3[name]
K1 --> L4[with] --> M1[参数]
K1 --> L5[env] --> M2[环境变量]
K1 --> L6[if] --> M3[条件]
K2 --> N1[uses]
K2 --> N2[run]
K2 --> N3[name]
K2 --> N4[with] --> O1[参数]
K2 --> N5[env] --> O2[环境变量]
K2 --> N6[if] --> O3[条件]
</code></pre>
<div class="table-container"><table><thead><tr><th><strong>关键字</strong></th><th><strong>描述</strong></th></tr></thead><tbody><tr><td><code>jobs</code></td><td>顶级节点,包含所有作业的定义。</td></tr><tr><td><code>Job</code></td><td>每个作业的定义,包含运行环境、步骤、依赖关系和环境变量。</td></tr><tr><td><code>runs-on</code></td><td>指定作业运行的虚拟环境,如 <code>ubuntu-latest</code><code>windows-latest</code><code>macos-latest</code></td></tr><tr><td><code>steps</code></td><td>包含作业中所有步骤的列表。</td></tr><tr><td><code>Step</code></td><td>每个步骤的定义,可以包含 <code>uses</code><code>run</code><code>name</code><code>with</code><code>env</code><code>if</code> 等子字段。</td></tr><tr><td><code>uses</code></td><td>调用预定义的 Action例如 <code>actions/checkout@v2</code></td></tr><tr><td><code>run</code></td><td>运行命令行命令,如 <code>npm install</code></td></tr><tr><td><code>name</code></td><td>步骤名称,用于描述步骤的功能。</td></tr><tr><td><code>with</code></td><td>传递给 Action 的参数,如 <code>node-version: '14'</code></td></tr><tr><td><code>env</code></td><td>环境变量,可以在作业级别或步骤级别定义。</td></tr><tr><td><code>if</code></td><td>条件表达式,控制步骤或作业的执行,例如 <code>if: github.ref == 'refs/heads/main'</code></td></tr><tr><td><code>needs</code></td><td>定义作业的依赖关系,确保按顺序执行,例如 <code>needs: [build]</code></td></tr><tr><td><code>matrix</code></td><td>定义矩阵策略,用于在多个环境组合上并行运行作业。</td></tr><tr><td><code>timeout-minutes</code></td><td>设置作业超时时间,以分钟为单位,防止作业无限制地运行。</td></tr><tr><td><code>continue-on-error</code></td><td>如果设置为 <code>true</code>,即使作业失败也会继续执行后续作业。</td></tr><tr><td><code>container</code></td><td>在指定的 Docker 容器中运行作业。</td></tr><tr><td><code>services</code></td><td>为作业提供依赖服务,如数据库服务。</td></tr></tbody></table></div>
<h3 id="步骤step">步骤Step<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#步骤step" 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>**步骤Step是作业中的单个任务。**步骤可以是运行命令行命令run或调用预定义的 actionuses。步骤在同一个作业中按顺序执行且共享相同的上下文包括工作目录、环境变量等。</p>
<pre><code class="mermaid">graph TD
A[steps]
A --> B1[Step 1]
A --> B2[Step 2]
B1 --> C1[name]
B1 --> C2[uses]
B1 --> C3[run]
B1 --> C4[with] --> D1[参数]
B1 --> C5[env] --> D2[环境变量]
B1 --> C6[if] --> D3[条件]
B1 --> C7[continue-on-error] --> D4[错误继续]
B1 --> C8[timeout-minutes] --> D5[超时时间]
B2 --> E1[name]
B2 --> E2[uses]
B2 --> E3[run]
B2 --> E4[with] --> F1[参数]
B2 --> E5[env] --> F2[环境变量]
B2 --> E6[if] --> F3[条件]
B2 --> E7[continue-on-error] --> F4[错误继续]
B2 --> E8[timeout-minutes] --> F5[超时时间]
</code></pre>
<div class="table-container"><table><thead><tr><th><strong>关键字</strong></th><th><strong>描述</strong></th></tr></thead><tbody><tr><td><code>steps</code></td><td>包含作业中所有步骤的列表。</td></tr><tr><td><code>name</code></td><td>步骤名称,用于描述步骤的功能。</td></tr><tr><td><code>uses</code></td><td>调用预定义的 Action例如 <code>actions/checkout@v2</code></td></tr><tr><td><code>run</code></td><td>运行命令行命令,如 <code>npm install</code></td></tr><tr><td><code>with</code></td><td>传递给 Action 的参数,如 <code>node-version: '14'</code></td></tr><tr><td><code>env</code></td><td>环境变量,可以在步骤级别定义。</td></tr><tr><td><code>if</code></td><td>条件表达式,控制步骤的执行,例如 <code>if: github.ref == 'refs/heads/main'</code></td></tr><tr><td><code>continue-on-error</code></td><td>如果设置为 <code>true</code>,即使步骤失败也会继续执行后续步骤。</td></tr><tr><td><code>timeout-minutes</code></td><td>设置步骤超时时间,以分钟为单位,防止步骤无限制地运行。</td></tr></tbody></table></div>
<h3 id="runner-和执行器runner">Runner 和执行器Runner<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#runner-和执行器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>Runner 是运行 GitHub Actions 作业的计算资源。GitHub 提供了托管 Runner也可以使用自托管 Runner。</p>
<ul>
<li><strong>托管 Runner</strong>:由 GitHub 提供和维护,目前支持 Linux、macOS 和 Windows。</li>
<li><strong>自托管 Runner</strong>:由用户提供和维护,适用于需要特殊软件或硬件环境的场景。</li>
</ul>
<h2 id="github-actions-中-yaml-的具体示例">GitHub Actions 中 YAML 的具体示例<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#github-actions-中-yaml-的具体示例" 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>
<p>在 GitHub Actions 中YAML 文件用于定义工作流的结构和行为。主要包括工作流名称、触发事件、作业和步骤等。</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">CI/CD Pipeline</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"># 定义触发事件:推送到 main 分支、拉取请求到 main 分支、定时触发、手动触发</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> push</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> branches</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;">main</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> pull_request</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> branches</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;">main</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> schedule</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;">cron</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'0 0 * * *'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> workflow_dispatch</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 使用 GitHub 托管的 Ubuntu 环境</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 矩阵策略:在多个 Node.js 版本上运行作业</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> strategy</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:#22863A;--shiki-dark:#85E89D;"> node-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">12</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">14</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">16</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</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;"> env</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> BUILD_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>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 使用 actions/checkout@v2 Action 检出代码</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Set up Node.js</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 使用 actions/setup-node@v2 Action 设置 Node.js 环境</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/setup-node@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> node-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ matrix.node-version }}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Install dependencies</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;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm install</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Build project</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;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm run build</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Upload build artifacts</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;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/upload-artifact@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</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;">build-artifacts</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> path</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>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> test</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></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;"> needs</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;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 矩阵策略:在多个 Node.js 版本上运行作业</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> strategy</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:#22863A;--shiki-dark:#85E89D;"> node-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">12</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">14</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">16</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;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Set up Node.js</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/setup-node@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> node-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ matrix.node-version }}</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Install dependencies</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm install</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Run tests</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;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm test</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;"> env</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> CI</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">true</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> deploy</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></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;"> needs</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;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 仅在推送到 main 分支时执行部署</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">github.ref == 'refs/heads/main'</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Set up Node.js</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/setup-node@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> node-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">14</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Install dependencies</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm install</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Build project</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm run build</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Deploy to server</span></span>
<span data-line><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 使用 GitHub Secrets 管理敏感信息</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> env</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> SSH_PRIVATE_KEY</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ secrets.SSH_PRIVATE_KEY }}</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> REMOTE_USER</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ secrets.REMOTE_USER }}</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> REMOTE_HOST</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ secrets.REMOTE_HOST }}</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> echo &quot;$SSH_PRIVATE_KEY&quot; | tr -d '\r' | ssh-add - > /dev/null</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ssh -o StrictHostKeyChecking=no $REMOTE_USER@$REMOTE_HOST &quot;mkdir -p ~/myapp&quot;</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> rsync -avz -e &quot;ssh -o StrictHostKeyChecking=no&quot; ./build/ $REMOTE_USER@$REMOTE_HOST:~/myapp/</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ssh -o StrictHostKeyChecking=no $REMOTE_USER@$REMOTE_HOST &quot;pm2 restart myapp&quot;</span></span></code></pre></figure>
<h2 id="github-actions-基础实践">GitHub Actions 基础实践<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#github-actions-基础实践" 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="创建第一个-github-actions-工作流">创建第一个 GitHub Actions 工作流<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#创建第一个-github-actions-工作流" 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>使用模板创建工作流是快速上手 GitHub Actions 的一种方法。GitHub 提供了一些预定义的模板,帮助用户快速生成工作流配置文件,适用于常见的 CI/CD 场景,如构建、测试和部署。</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>
<pre><code class="mermaid">graph LR
A[访问仓库的 Actions 页面] --> B[选择模板]
B --> C[修改模板]
C --> D[保存工作流文件]
</code></pre>
<ol>
<li><strong>访问仓库的 Actions 页面:</strong> 在 GitHub 仓库页面上,点击顶部菜单中的 “Actions” 标签,进入 Actions 页面。</li>
<li><strong>选择模板:</strong> GitHub 会根据仓库中的项目类型推荐一些工作流模板。例如,对于 Node.js 项目,会推荐 Node.js CI 模板。用户可以选择一个合适的模板进行创建。</li>
<li><strong>修改模板:</strong> 选择模板后GitHub 会展示模板的 YAML 配置文件。用户可以根据需要修改该文件,例如调整触发条件、添加或删除步骤。</li>
<li><strong>保存工作流文件:</strong> 修改完成后,点击 “Start commit” 按钮,将工作流文件保存到仓库的 <code>.github/workflows/</code> 目录下。</li>
</ol>
<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>
<pre><code class="mermaid">graph LR
A[创建工作流文件] --> B[编写 YAML 文件]
B --> C[定义工作流名称和触发条件]
C --> D[定义作业和步骤]
</code></pre>
<ol>
<li><strong>创建工作流文件:</strong> 在仓库的根目录下创建 <code>.github/workflows/</code> 目录。如果该目录不存在,需要手动创建。</li>
<li><strong>编写 YAML 文件:</strong><code>.github/workflows/</code> 目录下创建一个新的 YAML 文件,例如 <code>ci.yml</code>。在该文件中定义工作流的名称、触发条件、作业和步骤。</li>
<li><strong>定义工作流名称和触发条件:</strong> 使用 <code>name</code> 字段定义工作流的名称,使用 <code>on</code> 字段定义工作流的触发条件,如 <code>push</code><code>pull_request</code></li>
<li><strong>定义作业和步骤:</strong> 使用 <code>jobs</code> 字段定义一个或多个作业,每个作业包含多个步骤。步骤可以是运行命令或调用预定义的 action。</li>
</ol>
<h3 id="使用预定义的-actions">使用预定义的 Actions<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用预定义的-actions" 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>GitHub Actions 生态系统中包含大量的预定义 Actions分为官方 Actions 和社区 Actions。官方 Actions 由 GitHub 维护和提供,质量和可靠性有保障;社区 Actions 由开源社区开发和维护,种类丰富,覆盖了各种功能需求。可以在 <a href="https://github.com/marketplace?type=actions" class="external">GitHub Marketplace<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> 查找和使用各种 Actions查看其文档和示例。</p>
<h4 id="官方-actions-库">官方 Actions 库<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#官方-actions-库" 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>
<div class="table-container"><table><thead><tr><th>Action</th><th>功能说明</th><th>示例</th></tr></thead><tbody><tr><td><code>actions/checkout</code></td><td>检查出仓库代码</td><td><code>uses: actions/checkout@v2</code></td></tr><tr><td><code>actions/setup-node</code></td><td>设置 Node.js 环境</td><td><code>uses: actions/setup-node@v2</code></td></tr><tr><td><code>actions/upload-artifact</code></td><td>上传构建工件</td><td><code>uses: actions/upload-artifact@v2</code></td></tr><tr><td><code>actions/download-artifact</code></td><td>下载构建工件</td><td><code>uses: actions/download-artifact@v2</code></td></tr><tr><td><code>actions/setup-python</code></td><td>设置 Python 环境</td><td><code>uses: actions/setup-python@v2</code></td></tr><tr><td><code>actions/setup-java</code></td><td>设置 Java 环境</td><td><code>uses: actions/setup-java@v2</code></td></tr><tr><td><code>actions/setup-go</code></td><td>设置 Go 环境</td><td><code>uses: actions/setup-go@v3</code></td></tr><tr><td><code>actions/cache</code></td><td>缓存依赖和构建结果</td><td><code>uses: actions/cache@v2</code></td></tr><tr><td><code>actions/github-script</code></td><td>在 GitHub 上运行任意脚本</td><td><code>uses: actions/github-script@v5</code></td></tr><tr><td><code>actions/labeler</code></td><td>根据文件更改自动打标签</td><td><code>uses: actions/labeler@v2</code></td></tr><tr><td><code>actions/stale</code></td><td>自动标记和关闭过期的 issues 和 PRs</td><td><code>uses: actions/stale@v5</code></td></tr></tbody></table></div>
<h4 id="社区-actions-库">社区 Actions 库<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#社区-actions-库" 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>
<div class="table-container"><table><thead><tr><th>Action</th><th>功能说明</th><th>示例</th></tr></thead><tbody><tr><td><code>stefanzweifel/git-auto-commit-action</code></td><td>自动提交代码更改</td><td><code>uses: stefanzweifel/git-auto-commit-action@v4</code></td></tr><tr><td><code>docker/build-push-action</code></td><td>构建并推送 Docker 镜像</td><td><code>uses: docker/build-push-action@v2</code></td></tr><tr><td><code>slackapi/slack-github-action</code></td><td>发送通知到 Slack 频道</td><td><code>uses: slackapi/slack-github-action@v1</code></td></tr><tr><td><code>github/codeql-action</code></td><td>代码安全分析和质量检查</td><td><code>uses: github/codeql-action/analyze@v2</code></td></tr><tr><td><code>peter-evans/create-issue-from-file</code></td><td>从文件创建 issue</td><td><code>uses: peter-evans/create-issue-from-file@v2</code></td></tr><tr><td><code>softprops/action-gh-release</code></td><td>发布 GitHub Release</td><td><code>uses: softprops/action-gh-release@v1</code></td></tr><tr><td><code>peaceiris/actions-gh-pages</code></td><td>部署静态网站到 GitHub Pages</td><td><code>uses: peaceiris/actions-gh-pages@v3</code></td></tr><tr><td><code>JamesIves/github-pages-deploy-action</code></td><td>自动部署到 GitHub Pages</td><td><code>uses: JamesIves/github-pages-deploy-action@4.1.4</code></td></tr><tr><td><code>coverallsapp/github-action</code></td><td>测试覆盖率报告</td><td><code>uses: coverallsapp/github-action@v1</code></td></tr><tr><td><code>actions-rs/toolchain</code></td><td>设置 Rust 工具链</td><td><code>uses: actions-rs/toolchain@v1</code></td></tr><tr><td><code>microsoft/playwright-github-action</code></td><td>运行 Playwright 测试</td><td><code>uses: microsoft/playwright-github-action@v1</code></td></tr></tbody></table></div>
<p>使用预定义的 Actions 可以简化工作流配置,快速实现复杂功能。流行的预定义 Actions 通常经过广泛测试和使用,具有较高的可靠性和易用性。</p>
<p><strong>步骤:</strong></p>
<ol>
<li>
<p><strong>选择 Action</strong> 在 GitHub Marketplace 或官方文档中查找需要的 Action阅读其使用说明和示例。</p>
</li>
<li>
<p><strong>添加到工作流:</strong> 在工作流文件的 <code>steps</code> 部分,使用 <code>uses</code> 关键字引用选定的 Action并根据需求传递参数。</p>
</li>
<li>
<p><strong>具体示例:</strong> 使用 <code>actions/checkout</code><code>actions/setup-node</code> Actions 的工作流示例:</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span><span style="--shiki-light:#6A737D;--shiki-dark:#6A737D;"> # 该 Action 用于将仓库代码检出到 Runner 的工作目录。此步骤通常是工作流的第一步,用于获取最新代码。</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Set up Node.js</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/setup-node@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> node-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'14'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Install dependencies</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm install</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Run tests</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm test</span></span></code></pre></figure>
<p><strong>示例解析:</strong></p>
<ul>
<li><code>actions/checkout</code></li>
<li><code>actions/setup-node</code>:该 Action 用于设置 Node.js 环境。通过传递 <code>node-version</code> 参数,指定要使用的 Node.js 版本。</li>
<li><code>run</code>:使用 <code>run</code> 关键字直接运行命令行命令,如安装依赖和运行测试。</li>
</ul>
</li>
</ol>
<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 流程的重要组成部分。Maven 和 Gradle 是常见的 Java 项目构建工具,其他语言也有类似的工具,如 Node.js 的 npm 和 Python 的 pip。</p>
<p><strong>步骤:</strong></p>
<ol>
<li><strong>设置构建环境:</strong> 在工作流文件中,配置运行环境和必要的依赖工具。例如,在 Java 项目中,通常需要设置 JDK 环境。</li>
<li><strong>添加构建步骤:</strong><code>steps</code> 部分,使用 <code>run</code> 关键字执行构建工具的命令,如 <code>mvn install</code><code>gradle build</code></li>
</ol>
<p><strong>示例:</strong> 集成 Maven 的工作流文件示例:</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Set up JDK</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/setup-java@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> java-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'11'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Build with Maven</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mvn install</span></span></code></pre></figure>
<p>集成 Gradle 的工作流文件示例:</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Set up JDK</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/setup-java@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> java-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'11'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Build with Gradle</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">gradle 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>
<p><strong>步骤:</strong></p>
<ol>
<li><strong>配置测试环境:</strong> 确保工作流中包含必要的依赖工具和环境设置,以便能够运行测试。例如,在 Node.js 项目中,需要安装依赖包。</li>
<li><strong>添加测试步骤:</strong><code>steps</code> 部分,使用 <code>run</code> 关键字执行测试命令,如 <code>npm test</code><code>mvn test</code><code>gradle test</code></li>
</ol>
<p><strong>示例:</strong> 添加单元测试和集成测试的工作流文件示例Node.js 项目):</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Set up Node.js</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/setup-node@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> node-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'14'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Install dependencies</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm install</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Run unit tests</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm test</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Run integration tests</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm run integration-test</span></span></code></pre></figure>
<p>添加单元测试和集成测试的工作流文件示例Java 项目):</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Set up JDK</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/setup-java@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> java-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'11'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Build with Maven</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mvn install</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Run unit tests</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mvn test</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Run integration tests</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mvn verify</span></span></code></pre></figure>
<h2 id="github-actions-进阶操作">GitHub Actions 进阶操作<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#github-actions-进阶操作" 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="自定义-actions">自定义 Actions<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#自定义-actions" 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="创建和发布-javascript-actions">创建和发布 JavaScript Actions<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#创建和发布-javascript-actions" 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>JavaScript Actions 是基于 Node.js 环境执行的自定义 Actions适用于编写需要复杂逻辑或与 Node.js 生态系统集成的任务。它们可以复用现有的 npm 包,并且易于调试和测试。</p>
<ol>
<li>
<p><strong>初始化项目:</strong> 使用 <code>npm init</code> 初始化一个新的 Node.js 项目,并安装必要的依赖。</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;"> mkdir</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-action</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> cd</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> my-action</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> npm</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> init</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -y</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> npm</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> install</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> @actions/core</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> @actions/github</span></span></code></pre></figure>
</li>
<li>
<p><strong>编写代码:</strong> 创建 <code>index.js</code> 文件,编写 Action 的核心逻辑。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="javascript" data-theme="github-light github-dark"><code data-language="javascript" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> core</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> require</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'@actions/core'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> github</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> require</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'@actions/github'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">try</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> const</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> nameToGreet</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> core.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">getInput</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'who-to-greet'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> console.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">log</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">`Hello ${</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">nameToGreet</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">}!`</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> const</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> time</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> new</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Date</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">().</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">toTimeString</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> core.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">setOutput</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;time&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, time);</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">} </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">catch</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (error) {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> core.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">setFailed</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(error.message);</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
</li>
<li>
<p><strong>创建 Action 元数据文件:</strong> 在项目根目录下创建 <code>action.yml</code> 文件,定义 Action 的元数据。</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'Hello World'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">description</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'Greet someone and print the current time'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">inputs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> who-to-greet</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> description</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'The name of the person to greet'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> required</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">true</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> default</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'World'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">runs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> using</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'node12'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> main</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'index.js'</span></span></code></pre></figure>
</li>
<li>
<p><strong>发布 Action</strong> 将代码提交到 GitHub 仓库,并打标签发布。</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;"> git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> add</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> .</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> commit</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -m</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Initial commit&quot;</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tag</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -a</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -m</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Initial release&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> v1</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> push</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --follow-tags</span></span></code></pre></figure>
</li>
<li>
<p>**示例使用:**在工作流中使用自定义的 JavaScript Action</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> greet</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Greet</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">my-org/my-action@v1</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> who-to-greet</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'John Doe'</span></span></code></pre></figure>
</li>
</ol>
<h4 id="创建和发布-docker-actions">创建和发布 Docker Actions<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#创建和发布-docker-actions" 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 Actions 运行在 Docker 容器中,适用于需要特定环境或依赖的任务。通过 Docker可以确保 Action 在各种环境中具有一致的运行行为。</p>
<ol>
<li>
<p><strong>创建 Dockerfile</strong> 在项目根目录下创建 <code>Dockerfile</code>,定义容器的构建过程。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="dockerfile" data-theme="github-light github-dark"><code data-language="dockerfile" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">FROM</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> node:12-alpine</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">COPY</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> . /my-action</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">WORKDIR</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> /my-action</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">RUN</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> npm install</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">ENTRYPOINT</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;node&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;/my-action/index.js&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span></code></pre></figure>
</li>
<li>
<p><strong>编写代码:</strong> 创建 <code>index.js</code> 文件,编写 Action 的核心逻辑。</p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="javascript" data-theme="github-light github-dark"><code data-language="javascript" data-theme="github-light github-dark" style="display:grid;"><span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> core</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> require</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'@actions/core'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">const</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> github</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> require</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'@actions/github'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">try</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> {</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> const</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> nameToGreet</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> core.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">getInput</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'who-to-greet'</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> console.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">log</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">`Hello ${</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">nameToGreet</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">}!`</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">);</span></span>
<span data-line><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> const</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> time</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> =</span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;"> new</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;"> Date</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">().</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">toTimeString</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">();</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> core.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">setOutput</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">&quot;time&quot;</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, time);</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">} </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">catch</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> (error) {</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> core.</span><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">setFailed</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">(error.message);</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">}</span></span></code></pre></figure>
</li>
<li>
<p><strong>创建 Action 元数据文件:</strong> 在项目根目录下创建 <code>action.yml</code> 文件,定义 Action 的元数据。</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'Hello World'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">description</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'Greet someone and print the current time'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">inputs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> who-to-greet</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> description</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'The name of the person to greet'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> required</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">true</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> default</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'World'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">runs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> using</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'docker'</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;">'Dockerfile'</span></span></code></pre></figure>
</li>
<li>
<p><strong>发布 Action</strong> 将代码提交到 GitHub 仓库,并打标签发布。</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;"> git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> add</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> .</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> commit</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -m</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Initial commit&quot;</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tag</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -a</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -m</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Initial release&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> v1</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> push</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --follow-tags</span></span></code></pre></figure>
</li>
<li>
<p><strong>示例使用:</strong> 在工作流中使用自定义的 Docker Action</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> greet</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Greet</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">my-org/my-action@v1</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> who-to-greet</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'Jane Doe'</span></span></code></pre></figure>
</li>
</ol>
<h4 id="使用-composite-actions-复用步骤">使用 Composite Actions 复用步骤<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-composite-actions-复用步骤" 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>Composite Actions 允许将多个步骤组合成一个 Action以便在多个工作流中复用。适用于需要多个步骤的复杂任务。</p>
<p><strong>步骤:</strong></p>
<ol>
<li>
<p><strong>创建 Composite Action 文件:</strong> 在项目根目录下创建 <code>action.yml</code> 文件,定义 Composite Action。</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'Greet and Time'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">description</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'Greet someone and print the current time'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">inputs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> who-to-greet</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> description</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'The name of the person to greet'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> required</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">true</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> default</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'World'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">runs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> using</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'composite'</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;Hello ${{ inputs.who-to-greet }}!&quot;</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;The current time is ${{ steps.time.outputs.time }}&quot;</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> id</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">time</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> shell</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">bash</span></span></code></pre></figure>
</li>
<li>
<p><strong>发布 Composite Action</strong> 将代码提交到 GitHub 仓库,并打标签发布。</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;"> git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> add</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> .</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> commit</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -m</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Initial commit&quot;</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> tag</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -a</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> -m</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> &quot;Initial release&quot;</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> v1</span></span>
<span data-line><span style="--shiki-light:#6F42C1;--shiki-dark:#B392F0;">$</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> git</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> push</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> --follow-tags</span></span></code></pre></figure>
</li>
<li>
<p><strong>示例使用:</strong> 在工中使用自定义的 Composite Action</p>
</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> greet_and_time</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Greet and Time</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">my-org/greet-and-time@v1</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> who-to-greet</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'Alice'</span></span></code></pre></figure>
<h3 id="使用-github-secrets-进行安全管理">使用 GitHub Secrets 进行安全管理<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-github-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="添加和管理-secrets">添加和管理 Secrets<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#添加和管理-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></h4>
<p>Secrets 是 GitHub 提供的一种安全管理敏感信息的方法,用于存储诸如 API 密钥、凭据和其他机密数据。Secrets 可以在工作流中安全地引用,而不暴露实际值。</p>
<p><strong>步骤:</strong></p>
<ol>
<li><strong>添加 Secrets</strong> 在 GitHub 仓库页面,进入 “Settings” <span></span> “Secrets” <span></span> “Actions”点击 “New repository secret” 添加新的 Secret。
<ul>
<li><strong>Name</strong>:为 Secret 取一个名称,例如 <code>MY_SECRET</code>.</li>
<li><strong>Value</strong>:输入 Secret 的值,例如 <code>super-secret-value</code>.</li>
</ul>
</li>
<li><strong>管理 Secrets</strong> Secrets 可以通过 GitHub 界面进行添加、编辑和删除。注意Secret 值一旦保存,不能直接查看,只能通过工作流引用。</li>
</ol>
<h4 id="在工作流中安全地使用-secrets">在工作流中安全地使用 Secrets<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#在工作流中安全地使用-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></h4>
<p>在工作流中,可以通过 <code>${{ secrets.SECRET_NAME }}</code> 的语法安全地引用 Secrets。GitHub Actions 会在运行时将该表达式替换为实际的 Secret 值。</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Use Secret</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">echo &quot;The secret is ${{ secrets.MY_SECRET }}&quot;</span></span></code></pre></figure>
<p><strong>示例解析:</strong></p>
<ul>
<li><code>secrets.MY_SECRET</code>:引用名称为 <code>MY_SECRET</code> 的 Secret。GitHub Actions 会在运行时自动替换为 Secret 的实际值。</li>
</ul>
<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="使用-if-表达式设置条件化执行">使用 <code>if</code> 表达式设置条件化执行<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-if-表达式设置条件化执行" 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>if</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Run tests</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm test</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Deploy</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> if</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">github.ref == 'refs/heads/main' &amp;&amp; success()</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">./deploy.sh</span></span></code></pre></figure>
<p><strong>示例解析:</strong></p>
<ul>
<li><code>if: github.ref == 'refs/heads/main' &amp;&amp; success()</code>:仅在当前分支为 <code>main</code> 且前一步骤成功时执行 <code>deploy</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>默认情况下GitHub Actions 中的作业是并行运行的。通过配置 <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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Build project</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm build</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> test</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</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;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Run tests</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm test</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> deploy</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</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;">build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Deploy project</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">./deploy.sh</span></span></code></pre></figure>
<p><strong>示例解析:</strong></p>
<ul>
<li><code>needs: build</code>:表示 <code>test</code> 作业依赖 <code>build</code> 作业,<code>test</code> 作业将在 <code>build</code> 作业完成后执行。</li>
<li><code>needs: [build, test]</code>:表示 <code>deploy</code> 作业依赖 <code>build</code><code>test</code> 作业,<code>deploy</code> 作业将在 <code>build</code><code>test</code> 作业完成后执行。</li>
</ul>
<h3 id="集成第三方服务如-slackawsazure">集成第三方服务(如 Slack、AWS、Azure<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#集成第三方服务如-slackawsazure" 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="使用-webhooks-和自定义-notifications">使用 Webhooks 和自定义 Notifications<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-webhooks-和自定义-notifications" 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>Webhooks 允许工作流在特定事件发生时向外部服务发送 HTTP 请求,实现与外部服务的集成和通知。自定义 Notifications 可用于在工作流执行过程中发送通知,例如 Slack 通知。</p>
<p><strong>示例:</strong> 向 Slack 发送通知:</p>
<ol>
<li>
<p><strong>创建 Slack Webhook URL</strong> 在 Slack 工作区中,创建一个新的 Webhook URL。</p>
</li>
<li>
<p><strong>添加 Slack Webhook URL 到 Secrets</strong> 在 GitHub 仓库中添加一个新的 Secret例如 <code>SLACK_WEBHOOK_URL</code>,保存 Webhook URL。</p>
</li>
<li>
<p><strong>在工作流中使用 Webhook URL</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> notify</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Send notification to Slack</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> curl -X POST -H 'Content-type: application/json' \</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> --data '{&quot;text&quot;:&quot;Build completed successfully!&quot;}' \</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ${{ secrets.SLACK_WEBHOOK_URL }}</span></span></code></pre></figure>
</li>
</ol>
<p><strong>示例解析:</strong></p>
<ul>
<li><code>secrets.SLACK_WEBHOOK_URL</code>:引用名称为 <code>SLACK_WEBHOOK_URL</code> 的 Secret用于发送 HTTP 请求到 Slack Webhook URL。</li>
</ul>
<h4 id="部署到-awsazuregcp-等云服务">部署到 AWS、Azure、GCP 等云服务<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#部署到-awsazuregcp-等云服务" 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>GitHub Actions 可以与各大云服务平台(如 AWS、Azure、GCP集成实现自动化部署。通过使用官方或社区提供的 Actions可以简化部署流程。</p>
<p><strong>部署到 AWS S3</strong></p>
<ol>
<li>
<p><strong>添加 AWS 凭证到 Secrets</strong> 在 GitHub 仓库中添加 <code>AWS_ACCESS_KEY_ID</code><code>AWS_SECRET_ACCESS_KEY</code> Secrets。</p>
</li>
<li>
<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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> deploy</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Upload to S3</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">aws-actions/configure-aws-credentials@v1</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> aws-access-key-id</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ secrets.AWS_ACCESS_KEY_ID }}</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> aws-secret-access-key</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ secrets.AWS_SECRET_ACCESS_KEY }}</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> aws-region</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">us-west-2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Sync S3 bucket</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> aws s3 sync . s3://my-bucket --exclude &quot;.git/*&quot;</span></span></code></pre></figure>
</li>
</ol>
<p><strong>示例解析:</strong></p>
<ul>
<li><code>aws-actions/configure-aws-credentials@v1</code>:配置 AWS 凭证和区域。</li>
<li><code>aws s3 sync</code>:使用 AWS CLI 将代码同步到 S3 存储桶。</li>
</ul>
<h4 id="通知集成如-slackmicrosoft-teams">通知集成(如 Slack、Microsoft Teams<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#通知集成如-slackmicrosoft-teams" 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>除了 WebhooksGitHub Actions 还可以通过官方和社区 Actions 发送通知到各种消息平台,如 Slack 和 Microsoft Teams。这些通知可以用于报告工作流状态、构建结果等。</p>
<p><strong>发送通知到 Microsoft Teams</strong></p>
<ol>
<li>
<p><strong>创建 Microsoft Teams Webhook URL</strong> 在 Microsoft Teams 中,创建一个新的 Webhook URL。</p>
</li>
<li>
<p><strong>添加 Webhook URL 到 Secrets</strong> 在 GitHub 仓库中添加一个新的 Secret例如 <code>TEAMS_WEBHOOK_URL</code>,保存 Webhook URL。</p>
</li>
<li>
<p><strong>在工作流中使用 Webhook URL</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> notify</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Send notification to Microsoft Teams</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> curl -H 'Content-Type: application/json' \</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> -d '{&quot;title&quot;: &quot;Build Notification&quot;, &quot;text&quot;: &quot;The build has completed successfully!&quot;}' \</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ${{ secrets.TEAMS_WEBHOOK_URL }}</span></span></code></pre></figure>
</li>
</ol>
<p><strong>示例解析:</strong></p>
<ul>
<li><code>secrets.TEAMS_WEBHOOK_URL</code>:引用名称为 <code>TEAMS_WEBHOOK_URL</code> 的 Secret用于发送 HTTP 请求到 Microsoft Teams Webhook URL。</li>
</ul>
<h2 id="github-actions-高级实践">GitHub Actions 高级实践<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#github-actions-高级实践" 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="配置持续交付流水线">配置持续交付流水线<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>持续交付CD是软件工程中的一项实践旨在通过自动化部署过程使软件能够随时发布到任何环境。GitHub Actions 提供了强大的工具支持配置和管理 CD 流水线。</p>
<p><strong>步骤:</strong></p>
<ol>
<li>
<p><strong>创建工作流文件:</strong><code>.github/workflows/</code> 目录下创建一个新的工作流文件,例如 <code>cd.yml</code></p>
</li>
<li>
<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:#005CC5;--shiki-dark:#79B8FF;">on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> push</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> branches</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;">main</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> release</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> types</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">created</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span></code></pre></figure>
</li>
<li>
<p><strong>配置作业:</strong> 定义 CD 流水线的各个作业,包括构建、测试和部署。</p>
</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">CD Pipeline</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> push</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> branches</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;">main</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> release</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> types</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">created</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Build project</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm build</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> test</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</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;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Run tests</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm test</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> deploy</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</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;">build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">test</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Deploy to production</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">./deploy.sh</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>自动化版本发布和部署确保软件能够快速、安全地交付给用户减少人为操作和错误。GitHub Actions 可以自动执行版本号管理、构建工件发布和部署到目标环境。</p>
<p><strong>步骤:</strong></p>
<ol>
<li><strong>版本号管理:</strong> 在构建完成后,使用脚本自动更新版本号。</li>
<li><strong>发布构建工件:</strong> 使用 <code>actions/upload-artifact</code> 将构建工件上传到 GitHub或使用 <code>actions/create-release</code> 创建 GitHub Release。</li>
<li><strong>自动部署:</strong> 配置部署步骤,将构建工件部署到目标环境。</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Set up Node.js</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/setup-node@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> node-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'14'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Build project</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm build</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Upload build artifacts</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/upload-artifact@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</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;">build-artifacts</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> path</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>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> release</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</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;">build</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Create GitHub Release</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/create-release@v1</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> env</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> GITHUB_TOKEN</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ secrets.GITHUB_TOKEN }}</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> tag_name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">v1.0.0</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> release_name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Release v1.0.0</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> draft</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">false</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> prerelease</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">false</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Upload release assets</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/upload-release-asset@v1</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> upload_url</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ steps.create_release.outputs.upload_url }}</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> asset_path</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;"> asset_name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">build-artifacts.zip</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> asset_content_type</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">application/zip</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> deploy</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</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;">release</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Deploy to production</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">./deploy.sh</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>矩阵策略允许在多个环境和配置下并行运行作业,确保代码在不同平台、依赖版本和配置下的兼容性和稳定性。</p>
<p><strong>步骤:</strong></p>
<ol>
<li><strong>定义矩阵:</strong> 在工作流文件中使用 <code>matrix</code> 关键字定义多个变量组合。</li>
<li><strong>配置作业:</strong> 配置作业使用矩阵变量运行。</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> strategy</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:#22863A;--shiki-dark:#85E89D;"> node-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">12</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">14</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">16</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> os</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">windows-latest</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">macos-latest</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Setup Node.js</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/setup-node@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> node-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ matrix.node-version }}</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Install dependencies</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm install</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Run tests</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm 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>
<p><strong>示例: 在多个操作系统和 Node.js 版本上运行测试:</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> test</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ matrix.os }}</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> strategy</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:#22863A;--shiki-dark:#85E89D;"> node-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">12</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">14</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">16</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> os</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: [</span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">windows-latest</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">, </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">macos-latest</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">]</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Setup Node.js</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/setup-node@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> node-version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ matrix.node-version }}</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Install dependencies</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm install</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Run tests</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm test</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="使用-kubectl-和-kustomize-进行-kubernetes-部署">使用 <code>kubectl</code><code>kustomize</code> 进行 Kubernetes 部署<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#使用-kubectl-和-kustomize-进行-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><code>kubectl</code> 是 Kubernetes 的命令行工具,用于管理 Kubernetes 集群。<code>kustomize</code> 允许在 Kubernetes 原生清单上进行声明式配置管理。使用它们可以实现自动化 Kubernetes 部署。</p>
<p><strong>步骤:</strong></p>
<ol>
<li><strong>配置 Kubernetes 凭证:</strong> 在 GitHub Secrets 中添加 Kubernetes 配置,例如 <code>KUBE_CONFIG</code>.</li>
<li><strong>编写部署步骤:</strong> 在工作流中使用 <code>kubectl</code><code>kustomize</code> 部署应用。</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> deploy</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Set up kubectl</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">azure/setup-kubectl@v1</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'v1.18.0'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Deploy to Kubernetes</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> env</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> KUBE_CONFIG</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ secrets.KUBE_CONFIG }}</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> kubectl apply -k ./kustomize</span></span></code></pre></figure>
<h4 id="集成-helm-charts">集成 Helm Charts<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#集成-helm-charts" 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>Helm 是 Kubernetes 的包管理工具,使用 Charts 定义、安装和管理 Kubernetes 应用。通过 Helm 可以简化 Kubernetes 应用的部署和管理。</p>
<p><strong>步骤:</strong></p>
<ol>
<li><strong>添加 Helm 凭证:</strong> 在 GitHub Secrets 中添加 Helm 凭证,例如 <code>HELM_REPO_URL</code><code>HELM_REPO_AUTH</code>.</li>
<li><strong>编写部署步骤:</strong> 在工作流中使用 Helm 部署应用。</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> deploy</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Set up Helm</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">azure/setup-helm@v1</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Deploy with Helm</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> env</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> HELM_REPO_URL</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ secrets.HELM_REPO_URL }}</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> HELM_REPO_AUTH</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ secrets.HELM_REPO_AUTH }}</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> helm repo add my-repo $HELM_REPO_URL --username $HELM_REPO_AUTH</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> helm upgrade --install my-release my-repo/my-chart</span></span></code></pre></figure>
<h4 id="433-配置自动扩展和滚动更新">4.3.3 配置自动扩展和滚动更新<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#433-配置自动扩展和滚动更新" 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>步骤:</strong></p>
<ol>
<li><strong>编写 Kubernetes 配置文件:</strong> 使用 HPAHorizontal Pod Autoscaler和 Rolling Update 策略配置文件。</li>
<li><strong>在工作流中应用配置:</strong> 使用 <code>kubectl</code> 命令应用这些配置。</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> deploy</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Set up kubectl</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">azure/setup-kubectl@v1</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">'v1.18.0'</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Apply Kubernetes configuration</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> env</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> KUBE_CONFIG</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">${{ secrets.KUBE_CONFIG }}</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> kubectl apply -f k8s/deployment.yaml</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> kubectl apply -f k8s/hpa.yaml</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>优化工作流运行时间可以提高开发效率,减少等待时间。通过并行执行任务、缓存依赖和减少不必要的步骤可以实现优化。</p>
<p><strong>步骤:</strong></p>
<ol>
<li><strong>使用缓存:</strong> 使用 <code>actions/cache</code> 缓存依赖,减少重复下载和安装时间。</li>
<li><strong>并行执行任务:</strong> 将独立的任务配置为并行执行,减少总运行时间。</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Cache npm dependencies</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/cache@v2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> with</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> path</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">~/.npm</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;">${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> restore-keys</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ${{ runner.os }}-node-</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Install dependencies</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm install</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Build project</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm 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>通过分析日志可以排查工作流中的问题。GitHub Actions 提供详细的日志记录,可以在执行过程中查看每个步骤的输出。</p>
<p><strong>步骤:</strong></p>
<ol>
<li><strong>启用调试日志:</strong> 在工作流文件中设置 <code>ACTIONS_STEP_DEBUG</code> 环境变量为 <code>true</code></li>
<li><strong>查看运行日志:</strong> 在 GitHub Actions 界面查看每个作业和步骤的详细日志。</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> env</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> ACTIONS_STEP_DEBUG</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">true</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Checkout code</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> uses</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">actions/checkout@v2</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><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;">Run tests</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">npm 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>通过监控和告警可以及时发现和处理问题,确保工作流的稳定运行。可以使用外部服务(如 Prometheus 和 Grafana或集成通知如 Slack实现监控和告警。</p>
<p><strong>步骤:</strong></p>
<ol>
<li><strong>集成监控工具:</strong> 在工作流中集成 Prometheus、Grafana 等监控工具,收集和可视化指标数据。</li>
<li><strong>设置告警通知:</strong> 在工作流中配置告警通知,通过 Slack 或 Email 接收告警信息。</li>
</ol>
<p><strong>示例: 通过 Slack 发送告警通知:</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;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> notify</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> runs-on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">ubuntu-latest</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">name</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">Send notification to Slack</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#D73A49;--shiki-dark:#F97583;">|</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> curl -X POST -H 'Content-type: application/json' \</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> --data '{&quot;text&quot;:&quot;Build failed!&quot;}' \</span></span>
<span data-line><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;"> ${{ secrets.SLACK_WEBHOOK_URL }}</span></span></code></pre></figure>
<h2 id="github-actions-与其他-cicd-工具的对比">GitHub Actions 与其他 CI/CD 工具的对比<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#github-actions-与其他-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></h2>
<p>CI/CD 工具用于自动化软件构建、测试和部署过程,确保快速和可靠的发布周期。不同的工具有各自的优势和特点,适合不同规模和需求的项目。</p>
<h3 id="jenkins">Jenkins<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#jenkins" 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>Jenkins 是一个开源的自动化服务器广泛用于构建、部署和自动化任何项目。由于其强大的插件生态系统和可扩展性Jenkins 成为许多企业的首选 CI/CD 工具。</p>
<p><strong>优点:</strong></p>
<ul>
<li><strong>开源和可扩展性</strong>:庞大的插件库,可以满足几乎所有的 CI/CD 需求。</li>
<li><strong>广泛的社区支持</strong>:活跃的社区和丰富的资源(文档、教程、插件)。</li>
<li><strong>可自托管</strong>:完全控制构建环境和安全性,适用于企业内部部署。</li>
</ul>
<p><strong>缺点:</strong></p>
<ul>
<li><strong>复杂性</strong>:设置和维护可能需要大量时间和经验。</li>
<li><strong>用户界面</strong>:传统界面可能不够直观,需要学习曲线。</li>
<li><strong>资源消耗</strong>:自托管模式需要管理服务器资源。</li>
</ul>
<p><strong>适用场景:</strong></p>
<ul>
<li>需要高度定制化和控制的企业级项目。</li>
<li>复杂的构建和部署流水线。</li>
</ul>
<p><strong>示例:</strong></p>
<figure data-rehype-pretty-code-figure><pre tabindex="0" data-language="pipline" data-theme="github-light github-dark"><code data-language="pipline" data-theme="github-light github-dark" style="display:grid;"><span data-line><span>pipeline {</span></span>
<span data-line><span> agent any</span></span>
<span data-line><span> stages {</span></span>
<span data-line><span> stage('Build') {</span></span>
<span data-line><span> steps {</span></span>
<span data-line><span> sh 'mvn clean install'</span></span>
<span data-line><span> }</span></span>
<span data-line><span> }</span></span>
<span data-line><span> stage('Test') {</span></span>
<span data-line><span> steps {</span></span>
<span data-line><span> sh 'mvn test'</span></span>
<span data-line><span> }</span></span>
<span data-line><span> }</span></span>
<span data-line><span> stage('Deploy') {</span></span>
<span data-line><span> steps {</span></span>
<span data-line><span> sh './deploy.sh'</span></span>
<span data-line><span> }</span></span>
<span data-line><span> }</span></span>
<span data-line><span> }</span></span>
<span data-line><span>}</span></span></code></pre></figure>
<h3 id="travis-ci">Travis CI<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#travis-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>Travis CI 是一个基于云的 CI 服务,特别适合开源项目。它提供简单的配置和与 GitHub 的深度集成。</p>
<p><strong>优点:</strong></p>
<ul>
<li><strong>简单易用</strong>:配置文件简洁明了,适合快速上手。</li>
<li><strong>开源友好</strong>:免费提供给开源项目。</li>
<li><strong>GitHub 集成</strong>:无缝集成 GitHub 仓库。</li>
</ul>
<p><strong>缺点:</strong></p>
<ul>
<li><strong>有限的自定义</strong>:相比 JenkinsTravis CI 的自定义能力较弱。</li>
<li><strong>资源限制</strong>:免费版有并发构建和资源使用限制。</li>
<li><strong>速度和稳定性</strong>:在高峰期可能会出现构建排队和延迟。</li>
</ul>
<p><strong>适用场景:</strong></p>
<ul>
<li>开源项目。</li>
<li>需要快速上手和简便配置的小型项目。</li>
</ul>
<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;">language</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">java</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">jdk</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;">openjdk11</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;">mvn clean install</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mvn test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> provider</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">script</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 style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">./deploy.sh</span></span>
<span data-line><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;"> on</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> branch</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">main</span></span></code></pre></figure>
<h3 id="circleci">CircleCI<a role="anchor" aria-hidden="true" tabindex="-1" data-no-popover="true" href="#circleci" 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>CircleCI 是一个基于云的 CI/CD 工具,提供灵活的配置和强大的并行执行能力,支持多种编程语言和框架。</p>
<p><strong>优点:</strong></p>
<ul>
<li><strong>并行执行</strong>:支持并行化构建和测试,加快 CI/CD 流水线速度。</li>
<li><strong>灵活配置</strong>:通过 YAML 文件配置流水线,支持多种语言和环境。</li>
<li><strong>集成和扩展</strong>:与 GitHub、Bitbucket 和 Docker 集成良好。</li>
</ul>
<p><strong>缺点:</strong></p>
<ul>
<li><strong>成本</strong>:高级功能和更多资源需要付费。</li>
<li><strong>复杂配置</strong>:高级功能可能需要复杂的配置和调试。</li>
<li><strong>资源限制</strong>:免费版有并发和资源使用限制。</li>
</ul>
<p><strong>适用场景:</strong></p>
<ul>
<li>需要高并发和快速构建的大型项目。</li>
<li>希望在云端管理 CI/CD 流水线的项目。</li>
</ul>
<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;">version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">2.1</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">jobs</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> docker</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;">image</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">circleci/openjdk:11</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> steps</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;">checkout</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mvn clean install</span></span>
<span data-line><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;"> - </span><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">run</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#032F62;--shiki-dark:#9ECBFF;">mvn test</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">workflows</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> version</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">: </span><span style="--shiki-light:#005CC5;--shiki-dark:#79B8FF;">2</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> build-and-test</span><span style="--shiki-light:#24292E;--shiki-dark:#E1E4E8;">:</span></span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;"> jobs</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>
<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 是 GitLab 集成的 CI/CD 工具,提供完整的 DevOps 生命周期管理,适合从代码托管到部署的全流程管理。</p>
<p><strong>优点:</strong></p>
<ul>
<li><strong>集成性</strong>:与 GitLab 无缝集成,支持整个 DevOps 生命周期。</li>
<li><strong>自托管</strong>:提供自托管和云端版本,满足不同企业需求。</li>
<li><strong>安全性</strong>:内置的安全扫描和代码质量检查功能。</li>
</ul>
<p><strong>缺点:</strong></p>
<ul>
<li><strong>复杂性</strong>:全面的功能可能需要较长的学习曲线。</li>
<li><strong>资源管理</strong>:自托管版本需要额外的资源管理和维护。</li>
<li><strong>界面</strong>:某些高级功能的界面和配置可能不够直观。</li>
</ul>
<p><strong>适用场景:</strong></p>
<ul>
<li>需要一体化 DevOps 解决方案的企业。</li>
<li>希望在同一平台上管理代码、CI/CD 和部署的项目。</li>
</ul>
<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;">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</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;">mvn clean install</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">test</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;">mvn test</span></span>
<span data-line> </span>
<span data-line><span style="--shiki-light:#22863A;--shiki-dark:#85E89D;">deploy</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;">./deploy.sh</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;">main</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>
<div class="table-container"><table><thead><tr><th>特性</th><th>Jenkins</th><th>Travis CI</th><th>CircleCI</th><th>GitLab CI</th></tr></thead><tbody><tr><td><strong>开源/商业</strong></td><td>开源</td><td>商业,开源项目免费</td><td>商业,开源项目有限免费</td><td>开源,商业版</td></tr><tr><td><strong>托管方式</strong></td><td>自托管</td><td>云服务</td><td>云服务,自托管</td><td>云服务,自托管</td></tr><tr><td><strong>扩展性</strong></td><td></td><td></td><td></td><td></td></tr><tr><td><strong>配置复杂度</strong></td><td></td><td></td><td></td><td></td></tr><tr><td><strong>社区支持</strong></td><td></td><td></td><td></td><td></td></tr><tr><td><strong>适用项目类型</strong></td><td>大型企业项目,复杂流水线</td><td>开源项目,小型项目</td><td>需要高并发和快速构建的项目</td><td>希望全流程管理的企业项目</td></tr></tbody></table></div>
<p>不同的 CI/CD 工具各有优劣,选择合适的工具需要根据项目规模、复杂度和团队需求进行综合考虑。</p></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="#github-actions" data-for="github-actions">GitHub Actions</a></li><li class="depth-0"><a href="#github-actions-基本概念" data-for="github-actions-基本概念">GitHub Actions 基本概念</a></li><li class="depth-1"><a href="#工作流workflow" data-for="工作流workflow">工作流Workflow</a></li><li class="depth-1"><a href="#作业job" data-for="作业job">作业Job</a></li><li class="depth-1"><a href="#步骤step" data-for="步骤step">步骤Step</a></li><li class="depth-1"><a href="#runner-和执行器runner" data-for="runner-和执行器runner">Runner 和执行器Runner</a></li><li class="depth-0"><a href="#github-actions-中-yaml-的具体示例" data-for="github-actions-中-yaml-的具体示例">GitHub Actions 中 YAML 的具体示例</a></li><li class="depth-0"><a href="#github-actions-基础实践" data-for="github-actions-基础实践">GitHub Actions 基础实践</a></li><li class="depth-1"><a href="#创建第一个-github-actions-工作流" data-for="创建第一个-github-actions-工作流">创建第一个 GitHub Actions 工作流</a></li><li class="depth-1"><a href="#使用预定义的-actions" data-for="使用预定义的-actions">使用预定义的 Actions</a></li><li class="depth-1"><a href="#运行基本的-ci-流程" data-for="运行基本的-ci-流程">运行基本的 CI 流程</a></li><li class="depth-0"><a href="#github-actions-进阶操作" data-for="github-actions-进阶操作">GitHub Actions 进阶操作</a></li><li class="depth-1"><a href="#自定义-actions" data-for="自定义-actions">自定义 Actions</a></li><li class="depth-1"><a href="#使用-github-secrets-进行安全管理" data-for="使用-github-secrets-进行安全管理">使用 GitHub Secrets 进行安全管理</a></li><li class="depth-1"><a href="#设置条件化执行和并行作业" data-for="设置条件化执行和并行作业">设置条件化执行和并行作业</a></li><li class="depth-1"><a href="#集成第三方服务如-slackawsazure" data-for="集成第三方服务如-slackawsazure">集成第三方服务(如 Slack、AWS、Azure</a></li><li class="depth-0"><a href="#github-actions-高级实践" data-for="github-actions-高级实践">GitHub Actions 高级实践</a></li><li class="depth-1"><a href="#持续交付cd流程" data-for="持续交付cd流程">持续交付CD流程</a></li><li class="depth-1"><a href="#使用矩阵构建不同环境和配置" data-for="使用矩阵构建不同环境和配置">使用矩阵构建不同环境和配置</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><li class="depth-0"><a href="#github-actions-与其他-cicd-工具的对比" data-for="github-actions-与其他-cicd-工具的对比">GitHub Actions 与其他 CI/CD 工具的对比</a></li><li class="depth-1"><a href="#jenkins" data-for="jenkins">Jenkins</a></li><li class="depth-1"><a href="#travis-ci
</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>