cloud-build-helper
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCloud Build Helper
Cloud Build 助手
Quick Start
快速开始
Configure Cloud Build pipelines with proper caching and optimization for fast, efficient builds.
配置带有适当缓存和优化的Cloud Build流水线,以实现快速、高效的构建。
Instructions
操作步骤
Step 1: Create cloudbuild.yaml
步骤1:创建cloudbuild.yaml文件
yaml
steps:
# Build step
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/myapp:$COMMIT_SHA', '.']
# Push to registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/myapp:$COMMIT_SHA']
# Deploy
- name: 'gcr.io/cloud-builders/gcloud'
args:
- 'run'
- 'deploy'
- 'myapp'
- '--image=gcr.io/$PROJECT_ID/myapp:$COMMIT_SHA'
- '--region=us-central1'
images:
- 'gcr.io/$PROJECT_ID/myapp:$COMMIT_SHA'
options:
machineType: 'N1_HIGHCPU_8'
logging: CLOUD_LOGGING_ONLYyaml
steps:
# Build step
- name: 'gcr.io/cloud-builders/docker'
args: ['build', '-t', 'gcr.io/$PROJECT_ID/myapp:$COMMIT_SHA', '.']
# Push to registry
- name: 'gcr.io/cloud-builders/docker'
args: ['push', 'gcr.io/$PROJECT_ID/myapp:$COMMIT_SHA']
# Deploy
- name: 'gcr.io/cloud-builders/gcloud'
args:
- 'run'
- 'deploy'
- 'myapp'
- '--image=gcr.io/$PROJECT_ID/myapp:$COMMIT_SHA'
- '--region=us-central1'
images:
- 'gcr.io/$PROJECT_ID/myapp:$COMMIT_SHA'
options:
machineType: 'N1_HIGHCPU_8'
logging: CLOUD_LOGGING_ONLYStep 2: Configure caching
步骤2:配置缓存
yaml
steps:
- name: 'gcr.io/cloud-builders/docker'
entrypoint: 'bash'
args:
- '-c'
- |
docker pull gcr.io/$PROJECT_ID/myapp:latest || exit 0
docker build \
--cache-from gcr.io/$PROJECT_ID/myapp:latest \
-t gcr.io/$PROJECT_ID/myapp:$COMMIT_SHA \
-t gcr.io/$PROJECT_ID/myapp:latest \
.
docker push gcr.io/$PROJECT_ID/myapp:$COMMIT_SHA
docker push gcr.io/$PROJECT_ID/myapp:latestyaml
steps:
- name: 'gcr.io/cloud-builders/docker'
entrypoint: 'bash'
args:
- '-c'
- |
docker pull gcr.io/$PROJECT_ID/myapp:latest || exit 0
docker build \
--cache-from gcr.io/$PROJECT_ID/myapp:latest \
-t gcr.io/$PROJECT_ID/myapp:$COMMIT_SHA \
-t gcr.io/$PROJECT_ID/myapp:latest \
.
docker push gcr.io/$PROJECT_ID/myapp:$COMMIT_SHA
docker push gcr.io/$PROJECT_ID/myapp:latestStep 3: Set up build triggers
步骤3:设置构建触发器
bash
undefinedbash
undefinedCreate trigger from GitHub
Create trigger from GitHub
gcloud builds triggers create github
--repo-name=my-repo
--repo-owner=my-org
--branch-pattern="^main$"
--build-config=cloudbuild.yaml
--repo-name=my-repo
--repo-owner=my-org
--branch-pattern="^main$"
--build-config=cloudbuild.yaml
gcloud builds triggers create github
--repo-name=my-repo
--repo-owner=my-org
--branch-pattern="^main$"
--build-config=cloudbuild.yaml
--repo-name=my-repo
--repo-owner=my-org
--branch-pattern="^main$"
--build-config=cloudbuild.yaml
Create trigger with substitutions
Create trigger with substitutions
gcloud builds triggers create github
--repo-name=my-repo
--repo-owner=my-org
--branch-pattern="^main$"
--build-config=cloudbuild.yaml
--substitutions=_ENV=production,_REGION=us-central1
--repo-name=my-repo
--repo-owner=my-org
--branch-pattern="^main$"
--build-config=cloudbuild.yaml
--substitutions=_ENV=production,_REGION=us-central1
undefinedgcloud builds triggers create github
--repo-name=my-repo
--repo-owner=my-org
--branch-pattern="^main$"
--build-config=cloudbuild.yaml
--substitutions=_ENV=production,_REGION=us-central1
--repo-name=my-repo
--repo-owner=my-org
--branch-pattern="^main$"
--build-config=cloudbuild.yaml
--substitutions=_ENV=production,_REGION=us-central1
undefinedStep 4: Optimize build performance
步骤4:优化构建性能
Use parallel steps:
yaml
steps:
# These run in parallel
- name: 'gcr.io/cloud-builders/npm'
id: 'install-frontend'
args: ['install']
dir: 'frontend'
waitFor: ['-']
- name: 'gcr.io/cloud-builders/npm'
id: 'install-backend'
args: ['install']
dir: 'backend'
waitFor: ['-']
# This waits for both
- name: 'gcr.io/cloud-builders/npm'
args: ['run', 'build']
waitFor: ['install-frontend', 'install-backend']使用并行步骤:
yaml
steps:
# These run in parallel
- name: 'gcr.io/cloud-builders/npm'
id: 'install-frontend'
args: ['install']
dir: 'frontend'
waitFor: ['-']
- name: 'gcr.io/cloud-builders/npm'
id: 'install-backend'
args: ['install']
dir: 'backend'
waitFor: ['-']
# This waits for both
- name: 'gcr.io/cloud-builders/npm'
args: ['run', 'build']
waitFor: ['install-frontend', 'install-backend']Best Practices
最佳实践
- Use caching to speed up builds
- Implement parallel build steps
- Use appropriate machine types
- Store artifacts in Cloud Storage
- Use substitution variables
- Implement proper error handling
- Monitor build performance
- Use Cloud Build's built-in builders when possible
- 使用缓存加速构建
- 实现并行构建步骤
- 使用合适的机器类型
- 在Cloud Storage中存储构件
- 使用替换变量
- 实现适当的错误处理
- 监控构建性能
- 尽可能使用Cloud Build的内置构建器
Advanced
进阶内容
For detailed information, see:
- Build Steps - Build step configuration and patterns
- Caching - Caching strategies for faster builds
- Triggers - Build trigger configuration
有关详细信息,请参阅:
- 构建步骤 - 构建步骤配置和模式
- 缓存 - 加速构建的缓存策略
- 触发器 - 构建触发器配置