【发布时间】:2020-05-08 08:48:33
【问题描述】:
我有以下 Google Cloud Build 管道:
# gcloud builds submit --config cloud-build/cloudbuild.yaml --substitutions=_GIT_USER="<your user>",_GIT_PASS="<your password here>,_GIT_TAG="<tag name>"
steps:
# Git
- name: 'gcr.io/cloud-builders/git'
args: ['clone', 'https://${_GIT_USER}:${_GIT_PASS}@bitbucket.my-company.com/scm/my-project/my-app.git',
'--branch', '${_GIT_TAG}', '--single-branch']
# Build
- name: 'gcr.io/cloud-builders/mvn'
args: ['package', '-DskipTests=true']
dir: my-app/backend
- name: 'gcr.io/cloud-builders/docker'
args: ['build',
'--no-cache',
'-t', 'gcr.io/$PROJECT_ID/my-app-test:latest',
'-f', './cloud-build/Dockerfile-backend',
'--build-arg', 'JAR_FILE=./my-app/backend/target/my-app-0.0.1-SNAPSHOT.jar',
'.']
- name: "gcr.io/cloud-builders/docker"
args: ["push", "gcr.io/$PROJECT_ID/my-app-test:latest"]
# Deploy
# The Deploy step requires the role 'Kubernetes Engine Developer' for the service account `<project_number>@cloudbuild.gserviceaccount.com`
- name: 'gcr.io/cloud-builders/kubectl'
id: Deploy
args:
- 'apply'
- '-f'
- 'cloud-build/deployment-backend.yaml'
env:
- 'CLOUDSDK_COMPUTE_ZONE=${_K8S_COMPUTE_ZONE}'
- 'CLOUDSDK_CONTAINER_CLUSTER=${_K8S_CLUSTER}'
substitutions:
_K8S_COMPUTE_ZONE: us-central1-a
_K8S_CLUSTER: my-cluster-1
_GIT_USER: my-git-user
_GIT_PASS: replace-me-in-cloudbuild-file # default value
部署后端.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
name: my-backend
spec:
replicas: 1
selector:
matchLabels:
app: my-backend
template:
metadata:
labels:
app: my-backend
spec:
containers:
- name: my-backend
image: gcr.io/<my-project>/my-app-test:latest
...
问题在于,在第 3 步中,我必须将图像构建为 my-app-test:latest,这样我才能再次使用 deployment.yaml (image: gcr.io/<my-project>/my-app-test:latest) 中的最新图像
我希望能够像这样使用图像标签的标签名称:
第 3 步:
- name: 'gcr.io/cloud-builders/docker'
args: ['build',
'--no-cache',
'-t', 'gcr.io/$PROJECT_ID/my-app-test:${_GIT_TAG}',
'-f', './cloud-build/Dockerfile-backend',
'--build-arg', 'JAR_FILE=./my-app/backend/target/my-app-0.0.1-SNAPSHOT.jar',
'.']
但在这种情况下,告诉部署步骤使用以所用标签命名的图像的最佳方法是什么?
我发现 Kustomize 是“参数化”kubernetes 的惯用方式,但我仍然需要预先知道图像名称并将其存储在文件中。
用sed 替换图像标签可能会起作用,但似乎不是一个好的解决方案。
【问题讨论】:
标签: kubernetes google-cloud-platform google-cloud-build