Skip to content

构建流水线

设计 efficient 构建流水线使用缓存, parallelism, and artifact management.

你将学到

  • stages 的 well-设计构建流水线
  • 缓存 dependencies and 构建输出
  • 运行作业 in parallel for 速度
  • Managing 构建 artifacts and 发布

概念讲解

流水线设计

A well-designed build pipeline is the backbone of CI/CD. It starts with a trigger — a code push, a pull request, a schedule, or a manual invocation. The pipeline then flows through stages: lint checks code style, build compiles the application, test runs automated tests, package creates a deployable artifact, and deploy ships it. Each stage should be fast, independent, and produce clear output so failures are easy to diagnose.

Caching

Re-downloading dependencies and rebuilding unchanged code on every run wastes time. Caching stores dependencies, build outputs, and tool installations between pipeline runs. In GitHub Actions, use 操作/缓存@v4 to save and restore directories. In Docker, layer caching avoids rebuilding unchanged layers. A well-cached pipeline can cut build times from 10 minutes to 2 minutes, giving the team faster feedback.

Parallelism

Many pipeline stages can run in parallel. If you have unit tests, integration tests, and lint checks, they can all run simultaneously instead of sequentially. In GitHub Actions, define multiple jobs that run concurrently. In test suites, use parallel test runners. Parallelism reduces the total wall-clock time of the pipeline, but be mindful of resource limits and test isolation — parallel tests must not interfere with each other.

Artifacts and Releases

A build artifact is the output of a build — a compiled binary, a Docker image, a ZIP file. Artifacts should be stored in a registry or artifact store so they can be deployed later without rebuilding. This ensures that what you tested is exactly what you deploy. Use semantic versioning to tag releases. Store artifacts with retention policies so old builds are cleaned up automatically but you can still roll back if needed.

Pipeline as Code

Defining your pipeline in code — YAML, TypeScript, or Python — is a best practice. It makes the pipeline version-controlled, reviewable, and reproducible. When a pipeline change is needed, you create a pull request just like for application code. This ensures that pipeline changes are reviewed and tested, preventing a bad pipeline change from breaking all deployments. Treat your pipeline configuration with the same care as your application code: test it, review it, and document it.

示例代码

              name: Build Pipeline
on: [push]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci && npm run lint

  test:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        node-version: [18, 20, 22]
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-node@v4
        with:
          node-version: ${{ matrix.node-version }}
      - run: npm ci && npm test

  build:
    needs: [lint, test]
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - run: npm ci && npm run build
      - uses: actions/upload-artifact@v4
        with:
          name: dist
          path: dist/
            

Lint and 测试运行 in parallel. 测试运行 on 多个节点.js versions 通过矩阵策略. 构建 waits for 两者都到 通过, 然后 uploads artifact.

动手试一试

常见错误

错误写法

运行每个 stage sequentially

正确做法

Identify independent stages and 运行它们 in parallel. Lint, 单元测试, and 类型检查能 通常运行 simultaneously, cutting 总计流水线时间 significantly.

错误写法

Rebuilding 相同 artifact for 每个环境

正确做法

构建一次 and promote 相同 artifact 通过 staging and production. 这个 guarantees tested artifact 是什么获取 deployed, eliminating 环境-特定构建 differences.

查阅不认识的术语

新手友好的术语表用大白话解释编程术语——变量、函数、DOM、Promise 等等。

查看术语表

动手做项目

通过引导式项目应用所学,含起始代码与解决方案。

查看项目

解读错误信息

用大白话解释常见错误——什么意思、为什么发生、如何修复。

查看错误