将小 bash 脚本添加到 cloudbuild.yaml
add small bash script to cloudbuild.yaml
我有一个 google 云 (GCP) 的 cloudbuild.yaml
文件。我想使用简单的 bash 脚本 $(node -p -e "require('./package.json').version")
(或任何其他方式)从 package.json
获取 version
。如何将其添加到我的 cloudbuild.yaml
文件中?
我尝试将脚本放入 substitution
,但没有成功。
# gcloud submit --substitutions=_VERSION="1.1.0"
steps:
# build the container image
- name: "gcr.io/cloud-builders/docker"
args: ["build", "-t", "gcr.io/${_PROJECT_ID}/${_IMAGE}:${_VERSION}", "."]
# push the container image to Container Registry
- name: "gcr.io/cloud-builders/docker"
args: ["push", "gcr.io/${_PROJECT_ID}/${_IMAGE}:${_VERSION}"]
# build the container image
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args:
[
"run",
"deploy",
"${_SERVICE_NAME}",
"--project",
"${_PROJECT_ID}",
"--image",
"gcr.io/${_PROJECT_ID}/${_IMAGE}:${_VERSION}",
"--platform",
"managed",
"--allow-unauthenticated",
"--region",
"${_REGION}",
"--set-env-vars",
"${_ENV_VARS}",
"--ingress",
"internal-and-cloud-load-balancing",
"--quiet",
]
images:
- gcr.io/${_PROJECT_ID}/${_IMAGE}
substitutions:
_REGION: us-east1
_PROJECT_ID: my-dev
_SERVICE_NAME: my-client
_IMAGE: my-client
_VERSION: $(node -p -e "require('./package.json').version")
_ENV_VARS: "APP_ENV=dev"
这是 Cloud Build 的缺点之一。您不能将变量从一个步骤传递到另一个步骤。步骤之间只保留/workspace
。并且替换变量只是静态的(预定义或在管道运行时设置)。
这里的解决方案并不那么容易。
- 您需要添加一个获取版本的步骤并将其写入文件
- name: 'node'
entrypoint: bash
args:
- -c
- node -p -e "require('./package.json').version" > /workspace/node_version
- 然后在你的步骤中使用它,就像那样
- name: "gcr.io/cloud-builders/docker"
entrypoint: bash
args:
- -c
- |
VERSION=$${cat /workspace/node_version}
docker build -t gcr.io/${_PROJECT_ID}/${_IMAGE}:$${VERSION} .
双美元 $$ 表示这是一个 linux 命令而不是 Cloud Build 变量
根据 Guillaume 的回答,您可以使用包含两个 $$
而不是 1 个 $
的 bash 脚本,就像这样 $$(node -p -e "require('./package.json').version")
。 但是,如果您尝试使用的命令不可用(node
将不可用),最好从您可以创建的文件中提取它上面的步骤就像纪尧姆的回答:
- name: "gcr.io/cloud-builders/docker"
entrypoint: bash
args:
- -c
- docker build -t gcr.io/${_PROJECT_ID}/${_IMAGE}:$$(cat ./package_version) .
我有一个 google 云 (GCP) 的 cloudbuild.yaml
文件。我想使用简单的 bash 脚本 $(node -p -e "require('./package.json').version")
(或任何其他方式)从 package.json
获取 version
。如何将其添加到我的 cloudbuild.yaml
文件中?
我尝试将脚本放入 substitution
,但没有成功。
# gcloud submit --substitutions=_VERSION="1.1.0"
steps:
# build the container image
- name: "gcr.io/cloud-builders/docker"
args: ["build", "-t", "gcr.io/${_PROJECT_ID}/${_IMAGE}:${_VERSION}", "."]
# push the container image to Container Registry
- name: "gcr.io/cloud-builders/docker"
args: ["push", "gcr.io/${_PROJECT_ID}/${_IMAGE}:${_VERSION}"]
# build the container image
- name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
entrypoint: gcloud
args:
[
"run",
"deploy",
"${_SERVICE_NAME}",
"--project",
"${_PROJECT_ID}",
"--image",
"gcr.io/${_PROJECT_ID}/${_IMAGE}:${_VERSION}",
"--platform",
"managed",
"--allow-unauthenticated",
"--region",
"${_REGION}",
"--set-env-vars",
"${_ENV_VARS}",
"--ingress",
"internal-and-cloud-load-balancing",
"--quiet",
]
images:
- gcr.io/${_PROJECT_ID}/${_IMAGE}
substitutions:
_REGION: us-east1
_PROJECT_ID: my-dev
_SERVICE_NAME: my-client
_IMAGE: my-client
_VERSION: $(node -p -e "require('./package.json').version")
_ENV_VARS: "APP_ENV=dev"
这是 Cloud Build 的缺点之一。您不能将变量从一个步骤传递到另一个步骤。步骤之间只保留/workspace
。并且替换变量只是静态的(预定义或在管道运行时设置)。
这里的解决方案并不那么容易。
- 您需要添加一个获取版本的步骤并将其写入文件
- name: 'node'
entrypoint: bash
args:
- -c
- node -p -e "require('./package.json').version" > /workspace/node_version
- 然后在你的步骤中使用它,就像那样
- name: "gcr.io/cloud-builders/docker"
entrypoint: bash
args:
- -c
- |
VERSION=$${cat /workspace/node_version}
docker build -t gcr.io/${_PROJECT_ID}/${_IMAGE}:$${VERSION} .
双美元 $$ 表示这是一个 linux 命令而不是 Cloud Build 变量
根据 Guillaume 的回答,您可以使用包含两个 $$
而不是 1 个 $
的 bash 脚本,就像这样 $$(node -p -e "require('./package.json').version")
。 但是,如果您尝试使用的命令不可用(node
将不可用),最好从您可以创建的文件中提取它上面的步骤就像纪尧姆的回答:
- name: "gcr.io/cloud-builders/docker"
entrypoint: bash
args:
- -c
- docker build -t gcr.io/${_PROJECT_ID}/${_IMAGE}:$$(cat ./package_version) .