harness-ci
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseHarness 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 toolsyaml
infrastructure: type: Cloud spec: os: Linux # Linux, MacOS, Windows -
Kubernetes: Self-hosted via k8s clustersyaml
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 buildyaml
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 buildStep 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.myappDocker 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 with , , or with appropriate connector refs.
BuildAndPushDockerRegistryBuildAndPushECRBuildAndPushGCRBuildAndPushACRRun: 执行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.myappDocker 镜像仓库构建/推送
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: 将替换为、或,并配置对应的连接器引用即可。
BuildAndPushDockerRegistryBuildAndPushECRBuildAndPushGCRBuildAndPushACRCaching
缓存
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: falseGCS Cache: Replace S3 steps with /.
SaveCacheGCSRestoreCacheGCSS3缓存:
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: falseGCS缓存: 将S3步骤替换为/即可。
SaveCacheGCSRestoreCacheGCSParallelism
并行执行
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: 4Parallelism: 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: 4Parallel 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:backendBackground 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; donePlugins & 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: npmSlack通知:
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: npmArtifact Management
制品管理
Upload build outputs to cloud storage:
- S3: Type , spec:
S3Upload,bucket,sourcePathtarget - GCS: Type , spec:
GCSUpload,bucket,sourcePathtarget
将构建产物上传到云存储:
- S3: 类型为,配置项:
S3Upload、bucket、sourcePathtarget - GCS: 类型为,配置项:
GCSUpload、bucket、sourcePathtarget
CI Expressions
CI表达式
| Expression | Description |
|---|---|
| Git branch name |
| Full commit SHA |
| Short SHA (7 chars) |
| Commit message |
| Build number |
| Execution UUID |
| Secret value |
| 表达式 | 描述 |
|---|---|
| Git分支名称 |
| 完整commit SHA |
| 短SHA(7位字符) |
| Commit信息 |
| 构建编号 |
| 执行UUID |
| 密钥值 |
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 or with or .
type: PullRequesttype: TagactionstagCondition推送触发器:
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: PullRequesttype: TagactionstagConditionTroubleshooting
问题排查
| Issue | Solution |
|---|---|
| Build timeout | Increase timeout, optimize steps |
| Cache miss | Verify checksum file path |
| Image pull failed | Check connector credentials |
| TI not working | Verify language/buildTool config |
| Out of memory | Increase 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