260 lines
11 KiB
HTML
260 lines
11 KiB
HTML
|
|
<!DOCTYPE html>
|
|||
|
|
<html lang="zh-CN" data-theme="light">
|
|||
|
|
<head>
|
|||
|
|
<meta charset="utf-8">
|
|||
|
|
<meta name="viewport" content="width=device-width, initial-scale=1">
|
|||
|
|
<title>一份完整的生产级流水线 · CI/CD 流水线 · SRE 从零到成熟</title>
|
|||
|
|
<meta name="description" content="CI/CD 流水线 第 8 章:一份完整的生产级流水线。本章 0 个小节。">
|
|||
|
|
<link rel="stylesheet" href="../../assets/doc.css">
|
|||
|
|
</head>
|
|||
|
|
<body>
|
|||
|
|
|
|||
|
|
<header class="topbar">
|
|||
|
|
<div class="tb-in">
|
|||
|
|
<a class="brand" href="../../index.html"><span class="logo">SRE</span><div>从零到成熟<small>模块详解</small></div></a>
|
|||
|
|
<div class="crumbs">
|
|||
|
|
<a href="../../index.html">学习路径</a><span class="sep">/</span>
|
|||
|
|
<a href="../../index.html#s1">阶段 1 · 服务上线</a><span class="sep">/</span>
|
|||
|
|
<a href="index.html">CI/CD 流水线</a><span class="sep">/</span><b>第 8 章</b>
|
|||
|
|
</div>
|
|||
|
|
<div class="spacer"></div>
|
|||
|
|
<button class="btn" id="tocToggle">☰ 导航</button>
|
|||
|
|
<button class="btn" id="themeBtn" title="切换到深色主题">深色</button>
|
|||
|
|
</div>
|
|||
|
|
</header>
|
|||
|
|
|
|||
|
|
<div class="layout" id="layout">
|
|||
|
|
<aside class="sidebar">
|
|||
|
|
<h2>章节目录</h2>
|
|||
|
|
<nav id="chapters"><a class="toc-a" href="01-why.html"><i class="cn">1</i>为什么 SRE 要管 CI/CD</a><a class="toc-a" href="02-stages.html"><i class="cn">2</i>流水线的六个阶段</a><a class="toc-a" href="03-platform.html"><i class="cn">3</i>平台选型</a><div class="subnav"><a class="toc-a lv3" href="03-platform.html#sec-3-1">概念对照表</a></div><a class="toc-a" href="04-artifact.html"><i class="cn">4</i>制品与版本:可追溯的基石</a><div class="subnav"><a class="toc-a lv3" href="04-artifact.html#sec-4-1">一次构建,多处部署</a><a class="toc-a lv3" href="04-artifact.html#sec-4-2">版本号与标签策略</a><a class="toc-a lv3" href="04-artifact.html#sec-4-3">构建可复现</a></div><a class="toc-a" href="05-env.html"><i class="cn">5</i>环境隔离与门禁</a><div class="subnav"><a class="toc-a lv3" href="05-env.html#sec-5-1">门禁怎么设才不惹人烦</a></div><a class="toc-a" href="06-security.html"><i class="cn">6</i>流水线安全</a><div class="subnav"><a class="toc-a lv3" href="06-security.html#sec-6-1">凭据管理:从最差到最好</a><a class="toc-a lv3" href="06-security.html#sec-6-2">第三方 Action / 插件的风险</a><a class="toc-a lv3" href="06-security.html#sec-6-3">密钥泄漏检测</a></div><a class="toc-a" href="07-speed.html"><i class="cn">7</i>让它快起来</a><span class="toc-a active" aria-current="page"><i class="cn">8</i>一份完整的生产级流水线</span><a class="toc-a" href="09-debug.html"><i class="cn">9</i>流水线故障排查</a><div class="subnav"><a class="toc-a lv3" href="09-debug.html#sec-9-1">构建失败</a><a class="toc-a lv3" href="09-debug.html#sec-9-2">部署失败</a></div><a class="toc-a" href="10-cheat.html"><i class="cn">10</i>速查表与高频坑</a><div class="subnav"><a class="toc-a lv3" href="10-cheat.html#sec-10-1">速查</a><a class="toc-a lv3" href="10-cheat.html#sec-10-2">十个高频坑</a></div><a class="toc-a" href="11-checklist.html"><i class="cn">11</i>完成标准与自测</a><div class="subnav"><a class="toc-a lv3" href="11-checklist.html#sec-x1">自测题</a></div></nav>
|
|||
|
|
<div class="side-foot">
|
|||
|
|
<a class="btn" href="index.html">← 模块目录</a>
|
|||
|
|
<a class="btn" href="../../index.html">学习路径</a>
|
|||
|
|
</div>
|
|||
|
|
</aside>
|
|||
|
|
<div class="mask" id="mask"></div>
|
|||
|
|
|
|||
|
|
<main class="main-col">
|
|||
|
|
<article>
|
|||
|
|
<p class="chapnav">
|
|||
|
|
<a href="index.html">CI/CD 流水线</a>
|
|||
|
|
<span>·</span>
|
|||
|
|
<span>第 <b class="pos">8</b> / 11 章</span>
|
|||
|
|
</p>
|
|||
|
|
|
|||
|
|
<h2 id="example"><span class="n">8</span>一份完整的生产级流水线</h2>
|
|||
|
|
|
|||
|
|
<pre data-lang="yaml" data-name=".github/workflows/release.yml">name: release
|
|||
|
|
|
|||
|
|
on:
|
|||
|
|
push:
|
|||
|
|
branches: [main]
|
|||
|
|
paths-ignore: # 文档改动不触发完整流程
|
|||
|
|
- '**.md'
|
|||
|
|
- 'docs/**'
|
|||
|
|
workflow_dispatch:
|
|||
|
|
inputs:
|
|||
|
|
image_tag:
|
|||
|
|
description: '要部署的镜像 tag(留空则部署最新构建)'
|
|||
|
|
required: false
|
|||
|
|
|
|||
|
|
# 同一时间只允许一个发布,新的排队不取消(发布的取消很危险)
|
|||
|
|
concurrency:
|
|||
|
|
group: release-${{ github.ref }}
|
|||
|
|
cancel-in-progress: false
|
|||
|
|
|
|||
|
|
env:
|
|||
|
|
REGISTRY: registry.example.com
|
|||
|
|
IMAGE: registry.example.com/sre/orders-api
|
|||
|
|
SERVICE: orders-api
|
|||
|
|
|
|||
|
|
permissions:
|
|||
|
|
contents: read
|
|||
|
|
id-token: write # OIDC
|
|||
|
|
packages: write
|
|||
|
|
|
|||
|
|
jobs:
|
|||
|
|
# ---------------- ① 静态检查 ----------------
|
|||
|
|
lint:
|
|||
|
|
runs-on: ubuntu-latest
|
|||
|
|
steps:
|
|||
|
|
- uses: actions/checkout@v4
|
|||
|
|
- uses: actions/setup-go@v5
|
|||
|
|
with: { go-version: '1.22', cache: true }
|
|||
|
|
- run: go vet ./...
|
|||
|
|
- run: golangci-lint run --timeout 5m ./...
|
|||
|
|
- name: 检查是否有密钥被提交
|
|||
|
|
uses: gitleaks/gitleaks-action@v2
|
|||
|
|
|
|||
|
|
# ---------------- ② 测试 ----------------
|
|||
|
|
test:
|
|||
|
|
runs-on: ubuntu-latest
|
|||
|
|
services:
|
|||
|
|
postgres:
|
|||
|
|
image: postgres:16
|
|||
|
|
env:
|
|||
|
|
POSTGRES_PASSWORD: test
|
|||
|
|
ports: ['5432:5432']
|
|||
|
|
options: >-
|
|||
|
|
--health-cmd pg_isready --health-interval 5s
|
|||
|
|
--health-timeout 3s --health-retries 10
|
|||
|
|
steps:
|
|||
|
|
- uses: actions/checkout@v4
|
|||
|
|
- uses: actions/setup-go@v5
|
|||
|
|
with: { go-version: '1.22', cache: true }
|
|||
|
|
- run: go test -race -coverprofile=cover.out ./...
|
|||
|
|
- run: go tool cover -func=cover.out | tail -1
|
|||
|
|
- uses: actions/upload-artifact@v4
|
|||
|
|
if: always()
|
|||
|
|
with: { name: coverage, path: cover.out }
|
|||
|
|
|
|||
|
|
# ---------------- ③ 构建镜像 ----------------
|
|||
|
|
build:
|
|||
|
|
needs: [lint, test]
|
|||
|
|
runs-on: ubuntu-latest
|
|||
|
|
outputs:
|
|||
|
|
digest: ${{ steps.push.outputs.digest }}
|
|||
|
|
tag: ${{ steps.meta.outputs.tag }}
|
|||
|
|
steps:
|
|||
|
|
- uses: actions/checkout@v4
|
|||
|
|
|
|||
|
|
- name: 计算镜像标签
|
|||
|
|
id: meta
|
|||
|
|
run: |
|
|||
|
|
TAG="$(date -u +%Y%m%d-%H%M)-${GITHUB_SHA::7}"
|
|||
|
|
echo "tag=$TAG" >> "$GITHUB_OUTPUT"
|
|||
|
|
echo "构建版本:$TAG"
|
|||
|
|
|
|||
|
|
- uses: docker/setup-buildx-action@v3
|
|||
|
|
- uses: docker/login-action@v3
|
|||
|
|
with:
|
|||
|
|
registry: ${{ env.REGISTRY }}
|
|||
|
|
username: ${{ secrets.REGISTRY_USER }}
|
|||
|
|
password: ${{ secrets.REGISTRY_TOKEN }}
|
|||
|
|
|
|||
|
|
- name: 构建并推送
|
|||
|
|
id: push
|
|||
|
|
uses: docker/build-push-action@v5
|
|||
|
|
with:
|
|||
|
|
context: .
|
|||
|
|
push: true
|
|||
|
|
tags: ${{ env.IMAGE }}:${{ steps.meta.outputs.tag }}
|
|||
|
|
build-args: |
|
|||
|
|
VERSION=${{ steps.meta.outputs.tag }}
|
|||
|
|
COMMIT=${{ github.sha }}
|
|||
|
|
cache-from: type=gha
|
|||
|
|
cache-to: type=gha,mode=max
|
|||
|
|
provenance: true # 生成构建来源证明(供应链安全)
|
|||
|
|
sbom: true # 生成 SBOM
|
|||
|
|
|
|||
|
|
# ---------------- ④ 扫描 ----------------
|
|||
|
|
scan:
|
|||
|
|
needs: build
|
|||
|
|
runs-on: ubuntu-latest
|
|||
|
|
steps:
|
|||
|
|
- name: 镜像漏洞扫描
|
|||
|
|
run: |
|
|||
|
|
curl -sfL https://raw.githubusercontent.com/aquasecurity/trivy/main/contrib/install.sh \
|
|||
|
|
| sh -s -- -b /usr/local/bin
|
|||
|
|
trivy image --exit-code 1 --severity CRITICAL --ignore-unfixed \
|
|||
|
|
${{ env.IMAGE }}@${{ needs.build.outputs.digest }}
|
|||
|
|
|
|||
|
|
# ---------------- ⑤ 部署 staging ----------------
|
|||
|
|
deploy-staging:
|
|||
|
|
needs: [build, scan]
|
|||
|
|
runs-on: ubuntu-latest
|
|||
|
|
environment:
|
|||
|
|
name: staging
|
|||
|
|
url: https://staging.example.com
|
|||
|
|
steps:
|
|||
|
|
- uses: actions/checkout@v4
|
|||
|
|
- name: 部署到 staging
|
|||
|
|
run: |
|
|||
|
|
./scripts/deploy.sh \
|
|||
|
|
--env staging \
|
|||
|
|
--image "${{ env.IMAGE }}@${{ needs.build.outputs.digest }}"
|
|||
|
|
- name: 冒烟测试
|
|||
|
|
run: ./scripts/smoke-test.sh https://staging.example.com
|
|||
|
|
|
|||
|
|
# ---------------- ⑥ 部署生产(需审批)----------------
|
|||
|
|
deploy-prod:
|
|||
|
|
needs: deploy-staging
|
|||
|
|
runs-on: ubuntu-latest
|
|||
|
|
environment:
|
|||
|
|
name: production # ← 这个 environment 上配置了 Required reviewers
|
|||
|
|
url: https://api.example.com
|
|||
|
|
steps:
|
|||
|
|
- uses: actions/checkout@v4
|
|||
|
|
|
|||
|
|
- name: 发布前记录
|
|||
|
|
run: |
|
|||
|
|
echo "发布人:${{ github.actor }}"
|
|||
|
|
echo "制品:${{ env.IMAGE }}@${{ needs.build.outputs.digest }}"
|
|||
|
|
echo "提交:${{ github.sha }}"
|
|||
|
|
|
|||
|
|
- name: 灰度发布 5%
|
|||
|
|
id: canary
|
|||
|
|
run: |
|
|||
|
|
./scripts/deploy.sh --env prod --weight 5 \
|
|||
|
|
--image "${{ env.IMAGE }}@${{ needs.build.outputs.digest }}"
|
|||
|
|
|
|||
|
|
- name: 观察 5 分钟
|
|||
|
|
run: ./scripts/check-metrics.sh --window 5m --error-rate-max 0.01 --p99-max 500ms
|
|||
|
|
|
|||
|
|
- name: 全量发布
|
|||
|
|
run: |
|
|||
|
|
./scripts/deploy.sh --env prod --weight 100 \
|
|||
|
|
--image "${{ env.IMAGE }}@${{ needs.build.outputs.digest }}"
|
|||
|
|
|
|||
|
|
- name: 发布后验证
|
|||
|
|
run: ./scripts/smoke-test.sh https://api.example.com
|
|||
|
|
|
|||
|
|
- name: 失败则回滚
|
|||
|
|
if: failure()
|
|||
|
|
run: ./scripts/deploy.sh --env prod --rollback
|
|||
|
|
|
|||
|
|
- name: 记录发布
|
|||
|
|
if: always()
|
|||
|
|
run: |
|
|||
|
|
./scripts/record-release.sh \
|
|||
|
|
--service "${{ env.SERVICE }}" \
|
|||
|
|
--digest "${{ needs.build.outputs.digest }}" \
|
|||
|
|
--commit "${{ github.sha }}" \
|
|||
|
|
--status "${{ job.status }}" \
|
|||
|
|
--pipeline "${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}"</pre>
|
|||
|
|
|
|||
|
|
<div class="box tip">
|
|||
|
|
<span class="bt">这份配置里六个值得学的设计</span>
|
|||
|
|
<ol>
|
|||
|
|
<li><code>concurrency</code> 且 <code>cancel-in-progress: false</code>:发布的取消比排队危险得多,
|
|||
|
|
半完成的状态最难处理。</li>
|
|||
|
|
<li><code>paths-ignore</code>:文档改动不跑完整流程,省下大量等待。</li>
|
|||
|
|
<li><code>build.outputs.digest</code>:后续所有环节都用同一个 digest,
|
|||
|
|
<b>保证「验证的制品」和「部署的制品」严格相同</b>。</li>
|
|||
|
|
<li><code>environment: production</code>:审批、密钥、OIDC 信任策略三者绑定在一起,
|
|||
|
|
审批记录自动留痕。</li>
|
|||
|
|
<li><b>灰度 + 观察 + 回滚</b>写在流水线里,而不是写在一份文档里等人执行。</li>
|
|||
|
|
<li><code>record-release.sh</code> 用 <code>if: always()</code>:无论成功失败都记录,
|
|||
|
|
失败记录同样重要(用于统计变更失败率)。</li>
|
|||
|
|
</ol>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
</article>
|
|||
|
|
|
|||
|
|
<nav class="pager">
|
|||
|
|
<a class="prev" href="07-speed.html"><span class="k">← 上一章</span><span class="v">第 7 章 · 让它快起来</span></a>
|
|||
|
|
<a class="next" href="09-debug.html"><span class="k">下一章 →</span><span class="v">第 9 章 · 流水线故障排查</span></a>
|
|||
|
|
</nav>
|
|||
|
|
|
|||
|
|
<p class="foot">
|
|||
|
|
CI/CD 流水线 · 第 8 章 一份完整的生产级流水线<br>
|
|||
|
|
<a href="index.html">模块目录</a> · <a href="../../index.html">返回学习路径</a>
|
|||
|
|
</p>
|
|||
|
|
</main>
|
|||
|
|
</div>
|
|||
|
|
|
|||
|
|
<button id="toTop" title="回到顶部">↑</button>
|
|||
|
|
<script src="../../assets/doc.js"></script>
|
|||
|
|
</body>
|
|||
|
|
</html>
|