阶段2(下):运维专题 Docker/Nginx/Linux/CI-CD/Git 的课程、企业场景、面试题和可运行示例
This commit is contained in:
1 parent
5eb67c5d08
commit
b8010a74f3
42 files changed
+3533
-55
No files matched your search
@@ -0,0 +1,116 @@
|
||||
# GitHub Actions 工作流示例(放到 .github/workflows/ci.yml)。
|
||||
# Gitea Actions 的语法与之基本兼容;GitLab CI 语法不同,思路一样。具体字段以所用平台的官方文档为准。
|
||||
# 仓库结构假设:api/(Go)、ai/(Python)、web/(前端)
|
||||
name: ci
|
||||
|
||||
on:
|
||||
push:
|
||||
branches: [main]
|
||||
pull_request:
|
||||
|
||||
concurrency: # 同一分支新的提交会取消旧的运行
|
||||
group: ci-${{ github.ref }}
|
||||
cancel-in-progress: true
|
||||
|
||||
jobs:
|
||||
changes: # 只构建改动过的部分
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
api: ${{ steps.filter.outputs.api }}
|
||||
ai: ${{ steps.filter.outputs.ai }}
|
||||
web: ${{ steps.filter.outputs.web }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: dorny/paths-filter@v3
|
||||
id: filter
|
||||
with:
|
||||
filters: |
|
||||
api: ['api/**']
|
||||
ai: ['ai/**']
|
||||
web: ['web/**']
|
||||
|
||||
api:
|
||||
needs: changes
|
||||
if: needs.changes.outputs.api == 'true'
|
||||
runs-on: ubuntu-latest
|
||||
services:
|
||||
postgres: # 集成测试用的数据库
|
||||
image: postgres:17
|
||||
env:
|
||||
POSTGRES_USER: test
|
||||
POSTGRES_PASSWORD: test
|
||||
POSTGRES_DB: test
|
||||
ports: ['5432:5432']
|
||||
options: >-
|
||||
--health-cmd "pg_isready -U test" --health-interval 5s --health-retries 10
|
||||
defaults:
|
||||
run:
|
||||
working-directory: api
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version-file: api/go.mod
|
||||
- run: go vet ./...
|
||||
- run: go test -race -count=1 ./...
|
||||
env:
|
||||
TEST_DATABASE_URL: postgres://test:test@localhost:5432/test?sslmode=disable
|
||||
|
||||
ai:
|
||||
needs: changes
|
||||
if: needs.changes.outputs.ai == 'true'
|
||||
runs-on: ubuntu-latest
|
||||
defaults:
|
||||
run:
|
||||
working-directory: ai
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-python@v5
|
||||
with:
|
||||
python-version: '3.12'
|
||||
cache: pip
|
||||
- run: pip install -r requirements.txt -r requirements-dev.txt
|
||||
- run: ruff check .
|
||||
- run: pytest -q # 单元测试里用假的模型客户端,不调用真实模型 API
|
||||
|
||||
web:
|
||||
needs: changes
|
||||
if: needs.changes.outputs.web == 'true'
|
||||
runs-on: ubuntu-latest
|
||||
defaults:
|
||||
run:
|
||||
working-directory: web
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: 22
|
||||
cache: npm
|
||||
cache-dependency-path: web/package-lock.json
|
||||
- run: npm ci
|
||||
- run: npm run lint
|
||||
- run: npm run test -- --run
|
||||
- run: npm run build
|
||||
|
||||
image: # 只在 main 分支、测试全部通过后构建并推送镜像
|
||||
needs: [api, ai, web]
|
||||
# 依赖的任务被跳过时(没改动),默认会连带跳过本任务,所以要显式写 always() 再判断结果
|
||||
if: >-
|
||||
always() && github.ref == 'refs/heads/main' &&
|
||||
!contains(needs.*.result, 'failure') && !contains(needs.*.result, 'cancelled')
|
||||
runs-on: ubuntu-latest
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: docker/login-action@v3
|
||||
with:
|
||||
registry: ghcr.io
|
||||
username: ${{ github.actor }}
|
||||
password: ${{ secrets.GITHUB_TOKEN }} # 密钥来自平台,不写进仓库
|
||||
- name: build and push
|
||||
run: |
|
||||
tag=${GITHUB_SHA::12} # 用提交哈希做标签,可追溯、可回滚
|
||||
docker build -t ghcr.io/${{ github.repository }}/api:$tag api
|
||||
docker push ghcr.io/${{ github.repository }}/api:$tag
|
||||
@@ -0,0 +1,41 @@
|
||||
#!/usr/bin/env bash
|
||||
# 不自动运行:单机 Docker Compose 部署脚本示例——拉取指定版本、健康检查、失败自动回滚
|
||||
# 用法:IMAGE_TAG=<提交哈希> ./deploy.sh
|
||||
# 约定:compose.yaml 里 api 服务的镜像写成 image: registry.example.com/app/api:${IMAGE_TAG}
|
||||
set -euo pipefail
|
||||
|
||||
: "${IMAGE_TAG:?需要指定 IMAGE_TAG}"
|
||||
HEALTH_URL="${HEALTH_URL:-http://127.0.0.1:8080/healthz}"
|
||||
STATE_FILE="${STATE_FILE:-.deployed_tag}"
|
||||
previous="$(cat "$STATE_FILE" 2>/dev/null || true)"
|
||||
|
||||
deploy() {
|
||||
local tag="$1"
|
||||
echo "部署版本 $tag"
|
||||
IMAGE_TAG="$tag" docker compose pull api
|
||||
IMAGE_TAG="$tag" docker compose up -d --no-deps api
|
||||
}
|
||||
|
||||
healthy() {
|
||||
for _ in $(seq 1 30); do
|
||||
if curl -fsS --max-time 2 "$HEALTH_URL" >/dev/null; then
|
||||
return 0
|
||||
fi
|
||||
sleep 2
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
deploy "$IMAGE_TAG"
|
||||
if healthy; then
|
||||
echo "$IMAGE_TAG" > "$STATE_FILE"
|
||||
echo "部署成功:$IMAGE_TAG"
|
||||
else
|
||||
echo "健康检查失败" >&2
|
||||
if [[ -n "$previous" ]]; then
|
||||
echo "回滚到 $previous" >&2
|
||||
deploy "$previous"
|
||||
healthy && echo "已回滚到 $previous" >&2
|
||||
fi
|
||||
exit 1
|
||||
fi
|
||||
@@ -0,0 +1,44 @@
|
||||
# requires: yaml
|
||||
"""检查 ci.yml 的结构是否符合约定(YAML 能解析、关键任务存在、镜像只在 main 构建、没有明文密钥)。
|
||||
|
||||
运行:python -m pytest -q test_ci_config.py
|
||||
这不能代替在真实 CI 平台上运行,只用来防止明显的配置错误。
|
||||
"""
|
||||
import re
|
||||
from pathlib import Path
|
||||
|
||||
import yaml
|
||||
|
||||
HERE = Path(__file__).resolve().parent
|
||||
TEXT = (HERE / "ci.yml").read_text(encoding="utf-8")
|
||||
CI = yaml.safe_load(TEXT)
|
||||
|
||||
|
||||
def test_jobs_exist():
|
||||
assert {"changes", "api", "ai", "web", "image"} <= set(CI["jobs"])
|
||||
|
||||
|
||||
def test_trigger():
|
||||
# 注意:PyYAML 按 YAML 1.1 把键 on 解析成 True
|
||||
triggers = CI.get("on", CI.get(True))
|
||||
assert "pull_request" in triggers and triggers["push"]["branches"] == ["main"]
|
||||
|
||||
|
||||
def test_image_only_on_main_and_after_tests():
|
||||
image = CI["jobs"]["image"]
|
||||
assert set(image["needs"]) == {"api", "ai", "web"}
|
||||
assert "refs/heads/main" in image["if"]
|
||||
|
||||
|
||||
def test_tests_run():
|
||||
api_steps = " ".join(s.get("run", "") for s in CI["jobs"]["api"]["steps"])
|
||||
assert "go test" in api_steps and "go vet" in api_steps
|
||||
web_steps = " ".join(s.get("run", "") for s in CI["jobs"]["web"]["steps"])
|
||||
assert "npm ci" in web_steps and "npm run build" in web_steps
|
||||
|
||||
|
||||
def test_no_plaintext_secrets():
|
||||
for line in TEXT.splitlines():
|
||||
if re.search(r"(?i)(password|token|secret)\s*:", line) and "secrets." not in line:
|
||||
# 只有 CI 服务容器里的测试数据库密码允许明文
|
||||
assert "POSTGRES_PASSWORD: test" in line, line
|
||||
Reference in new issue
Block a user