harness-ci

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Harness CI Skill

Harness CI 技能

Container-native CI builds with test intelligence, caching, parallelization, and infrastructure management.
具备测试智能、缓存、并行执行和基础设施管理能力的容器原生CI构建工具。

Build Infrastructure

构建基础设施

  • Cloud (Recommended): Zero-config hosted, auto-scaling, pre-installed tools
    yaml
    infrastructure:
      type: Cloud
      spec:
        os: Linux  # Linux, MacOS, Windows
  • Kubernetes: Self-hosted via k8s clusters
    yaml
    infrastructure:
      type: KubernetesDirect
      spec:
        connectorRef: k8s_connector
        namespace: harness-builds
        os: Linux
  • VMs: AWS, Azure, GCP pool-based scaling
  • 云(推荐): 零配置托管、自动扩缩容、预安装工具
    yaml
    infrastructure:
      type: Cloud
      spec:
        os: Linux  # Linux, MacOS, Windows
  • Kubernetes: 通过k8s集群自托管
    yaml
    infrastructure:
      type: KubernetesDirect
      spec:
        connectorRef: k8s_connector
        namespace: harness-builds
        os: Linux
  • 虚拟机: 基于AWS、Azure、GCP资源池的扩缩容

Basic Pipeline Structure

基础流水线结构

yaml
pipeline:
  name: Build Pipeline
  identifier: build_pipeline
  properties:
    ci:
      codebase:
        connectorRef: harness_code
        repoName: my-service
        build: <+input>
  stages:
    - stage:
        name: Build and Test
        type: CI
        spec:
          cloneCodebase: true
          infrastructure:
            type: Cloud
            spec:
              os: Linux
          execution:
            steps:
              - step:
                  name: Install
                  type: Run
                  spec:
                    shell: Sh
                    command: npm ci
              - step:
                  name: Test
                  type: Run
                  spec:
                    command: npm test -- --coverage
              - step:
                  name: Build
                  type: Run
                  spec:
                    command: npm run build
yaml
pipeline:
  name: Build Pipeline
  identifier: build_pipeline
  properties:
    ci:
      codebase:
        connectorRef: harness_code
        repoName: my-service
        build: <+input>
  stages:
    - stage:
        name: Build and Test
        type: CI
        spec:
          cloneCodebase: true
          infrastructure:
            type: Cloud
            spec:
              os: Linux
          execution:
            steps:
              - step:
                  name: Install
                  type: Run
                  spec:
                    shell: Sh
                    command: npm ci
              - step:
                  name: Test
                  type: Run
                  spec:
                    command: npm test -- --coverage
              - step:
                  name: Build
                  type: Run
                  spec:
                    command: npm run build

Step Types

步骤类型

Run: Execute shell commands
yaml
- step:
    name: Build
    type: Run
    spec:
      shell: Sh
      command: npm run build
      envVariables:
        NODE_ENV: production
      resources:
        limits:
          memory: 2Gi
          cpu: "1"
RunTests (Test Intelligence): Language/framework-aware test execution
yaml
- step:
    type: RunTests
    spec:
      language: Java  # Java, Kotlin, Scala, C#, Python, Ruby
      buildTool: Maven  # Maven, Gradle, Bazel, etc.
      runOnlySelectedTests: true  # Enable TI
      enableTestSplitting: true   # Parallel execution
      testAnnotations: org.junit.Test
      packages: com.myapp
Docker Registry Build/Push
yaml
- step:
    name: Build and Push
    type: BuildAndPushDockerRegistry
    spec:
      connectorRef: docker_connector
      repo: myorg/myapp
      tags: [<+pipeline.sequenceId>, <+codebase.shortCommitSha>, latest]
      dockerfile: Dockerfile
      caching: true
      buildArgs:
        VERSION: <+pipeline.sequenceId>
ECR/GCR/ACR: Replace
BuildAndPushDockerRegistry
with
BuildAndPushECR
,
BuildAndPushGCR
, or
BuildAndPushACR
with appropriate connector refs.
Run: 执行shell命令
yaml
- step:
    name: Build
    type: Run
    spec:
      shell: Sh
      command: npm run build
      envVariables:
        NODE_ENV: production
      resources:
        limits:
          memory: 2Gi
          cpu: "1"
RunTests(测试智能): 支持语言/框架感知的测试执行
yaml
- step:
    type: RunTests
    spec:
      language: Java  # Java, Kotlin, Scala, C#, Python, Ruby
      buildTool: Maven  # Maven, Gradle, Bazel, etc.
      runOnlySelectedTests: true  # Enable TI
      enableTestSplitting: true   # Parallel execution
      testAnnotations: org.junit.Test
      packages: com.myapp
Docker 镜像仓库构建/推送
yaml
- step:
    name: Build and Push
    type: BuildAndPushDockerRegistry
    spec:
      connectorRef: docker_connector
      repo: myorg/myapp
      tags: [<+pipeline.sequenceId>, <+codebase.shortCommitSha>, latest]
      dockerfile: Dockerfile
      caching: true
      buildArgs:
        VERSION: <+pipeline.sequenceId>
ECR/GCR/ACR:
BuildAndPushDockerRegistry
替换为
BuildAndPushECR
BuildAndPushGCR
BuildAndPushACR
,并配置对应的连接器引用即可。

Caching

缓存

S3 Cache:
yaml
- step:
    name: Save Cache
    type: SaveCacheS3
    spec:
      connectorRef: aws_connector
      bucket: harness-cache
      key: npm-{{ checksum "package-lock.json" }}
      sourcePaths: [node_modules]
- step:
    name: Restore Cache
    type: RestoreCacheS3
    spec:
      connectorRef: aws_connector
      bucket: harness-cache
      key: npm-{{ checksum "package-lock.json" }}
      failIfKeyNotFound: false
GCS Cache: Replace S3 steps with
SaveCacheGCS
/
RestoreCacheGCS
.
S3缓存:
yaml
- step:
    name: Save Cache
    type: SaveCacheS3
    spec:
      connectorRef: aws_connector
      bucket: harness-cache
      key: npm-{{ checksum "package-lock.json" }}
      sourcePaths: [node_modules]
- step:
    name: Restore Cache
    type: RestoreCacheS3
    spec:
      connectorRef: aws_connector
      bucket: harness-cache
      key: npm-{{ checksum "package-lock.json" }}
      failIfKeyNotFound: false
GCS缓存: 将S3步骤替换为
SaveCacheGCS
/
RestoreCacheGCS
即可。

Parallelism

并行执行

Matrix Strategy: Run steps with multiple configurations
yaml
- step:
    name: Test Matrix
    type: Run
    spec:
      command: npm test
      envVariables:
        NODE_VERSION: <+matrix.nodeVersion>
        DB_TYPE: <+matrix.database>
    strategy:
      matrix:
        nodeVersion: ["16", "18", "20"]
        database: [postgres, mysql]
      maxConcurrency: 4
Parallelism: Run same step multiple times
yaml
- step:
    name: Parallel Tests
    type: Run
    spec:
      command: npm test -- --shard=$HARNESS_STAGE_INDEX/$HARNESS_STAGE_TOTAL
    strategy:
      parallelism: 4
Parallel Step Groups:
yaml
- stepGroup:
    name: Parallel Build
    steps:
      - parallel:
          - step:
              name: Build Frontend
              type: Run
              spec:
                command: npm run build:frontend
          - step:
              name: Build Backend
              type: Run
              spec:
                command: npm run build:backend
矩阵策略: 使用多配置运行步骤
yaml
- step:
    name: Test Matrix
    type: Run
    spec:
      command: npm test
      envVariables:
        NODE_VERSION: <+matrix.nodeVersion>
        DB_TYPE: <+matrix.database>
    strategy:
      matrix:
        nodeVersion: ["16", "18", "20"]
        database: [postgres, mysql]
      maxConcurrency: 4
并行: 多次运行同一个步骤
yaml
- step:
    name: Parallel Tests
    type: Run
    spec:
      command: npm test -- --shard=$HARNESS_STAGE_INDEX/$HARNESS_STAGE_TOTAL
    strategy:
      parallelism: 4
并行步骤组:
yaml
- stepGroup:
    name: Parallel Build
    steps:
      - parallel:
          - step:
              name: Build Frontend
              type: Run
              spec:
                command: npm run build:frontend
          - step:
              name: Build Backend
              type: Run
              spec:
                command: npm run build:backend

Background Services

后台服务

Start services (databases, caches) for integration tests:
yaml
- step:
    name: PostgreSQL
    type: Background
    spec:
      image: postgres:14
      envVariables:
        POSTGRES_USER: test
        POSTGRES_PASSWORD: test
        POSTGRES_DB: testdb
      portBindings:
        "5432": "5432"
      resources:
        limits:
          memory: 1Gi

- step:
    name: Wait for DB
    type: Run
    spec:
      command: until pg_isready -h localhost -p 5432; do sleep 1; done
启动服务(数据库、缓存)用于集成测试:
yaml
- step:
    name: PostgreSQL
    type: Background
    spec:
      image: postgres:14
      envVariables:
        POSTGRES_USER: test
        POSTGRES_PASSWORD: test
        POSTGRES_DB: testdb
      portBindings:
        "5432": "5432"
      resources:
        limits:
          memory: 1Gi

- step:
    name: Wait for DB
    type: Run
    spec:
      command: until pg_isready -h localhost -p 5432; do sleep 1; done

Plugins & Actions

插件与Action

Slack Notification:
yaml
- step:
    name: Notify Slack
    type: Plugin
    spec:
      image: plugins/slack
      settings:
        webhook: <+secrets.getValue("slack_webhook")>
        channel: builds
        template: "Build {{#success build.status}}succeeded{{else}}failed{{/success}}"
S3 Upload:
yaml
- step:
    name: Upload Artifacts
    type: Plugin
    spec:
      image: plugins/s3
      settings:
        bucket: build-artifacts
        source: dist/**/*
        target: builds/<+pipeline.sequenceId>
GitHub Actions:
yaml
- step:
    name: Setup Node
    type: Action
    spec:
      uses: actions/setup-node@v3
      with:
        node-version: "18"
        cache: npm
Slack通知:
yaml
- step:
    name: Notify Slack
    type: Plugin
    spec:
      image: plugins/slack
      settings:
        webhook: <+secrets.getValue("slack_webhook")>
        channel: builds
        template: "Build {{#success build.status}}succeeded{{else}}failed{{/success}}"
S3上传:
yaml
- step:
    name: Upload Artifacts
    type: Plugin
    spec:
      image: plugins/s3
      settings:
        bucket: build-artifacts
        source: dist/**/*
        target: builds/<+pipeline.sequenceId>
GitHub Actions:
yaml
- step:
    name: Setup Node
    type: Action
    spec:
      uses: actions/setup-node@v3
      with:
        node-version: "18"
        cache: npm

Artifact Management

制品管理

Upload build outputs to cloud storage:
  • S3: Type
    S3Upload
    , spec:
    bucket
    ,
    sourcePath
    ,
    target
  • GCS: Type
    GCSUpload
    , spec:
    bucket
    ,
    sourcePath
    ,
    target
将构建产物上传到云存储:
  • S3: 类型为
    S3Upload
    ,配置项:
    bucket
    sourcePath
    target
  • GCS: 类型为
    GCSUpload
    ,配置项:
    bucket
    sourcePath
    target

CI Expressions

CI表达式

ExpressionDescription
<+codebase.branch>
Git branch name
<+codebase.commitSha>
Full commit SHA
<+codebase.shortCommitSha>
Short SHA (7 chars)
<+codebase.commitMessage>
Commit message
<+pipeline.sequenceId>
Build number
<+pipeline.executionId>
Execution UUID
<+secrets.getValue("key")>
Secret value
表达式描述
<+codebase.branch>
Git分支名称
<+codebase.commitSha>
完整commit SHA
<+codebase.shortCommitSha>
短SHA(7位字符)
<+codebase.commitMessage>
Commit信息
<+pipeline.sequenceId>
构建编号
<+pipeline.executionId>
执行UUID
<+secrets.getValue("key")>
密钥值

Triggers

触发器

Push Trigger:
yaml
trigger:
  name: Build on Push
  pipelineIdentifier: build_pipeline
  source:
    type: Webhook
    spec:
      type: Push
      connectorRef: harness_code
      repoName: my-service
      payloadConditions:
        - key: targetBranch
          operator: In
          value: [main, develop]
Pull Request & Tag: Use
type: PullRequest
or
type: Tag
with
actions
or
tagCondition
.
推送触发器:
yaml
trigger:
  name: Build on Push
  pipelineIdentifier: build_pipeline
  source:
    type: Webhook
    spec:
      type: Push
      connectorRef: harness_code
      repoName: my-service
      payloadConditions:
        - key: targetBranch
          operator: In
          value: [main, develop]
Pull Request与标签触发器: 使用
type: PullRequest
type: Tag
,并配置
actions
tagCondition
即可。

Troubleshooting

问题排查

IssueSolution
Build timeoutIncrease timeout, optimize steps
Cache missVerify checksum file path
Image pull failedCheck connector credentials
TI not workingVerify language/buildTool config
Out of memoryIncrease step memory limits
Debug:
yaml
- step:
    name: Debug
    type: Run
    spec:
      command: |
        echo "Branch: <+codebase.branch>"
        echo "Build: <+pipeline.sequenceId>"
        env | sort
        df -h
问题解决方案
构建超时调大超时时间,优化步骤
缓存未命中校验校验和文件路径
镜像拉取失败检查连接器凭证
测试智能不生效校验语言/构建工具配置
内存不足调大步骤内存限制
调试:
yaml
- step:
    name: Debug
    type: Run
    spec:
      command: |
        echo "Branch: <+codebase.branch>"
        echo "Build: <+pipeline.sequenceId>"
        env | sort
        df -h

Related Documentation

相关文档