[英]Environment variables in Google Cloud Build
We want to migrate from Bitbucket Pipelines to Google Cloud Build to test, build and push Docker images. 我们希望从Bitbucket Pipelines迁移到Google Cloud Build,以测试,构建和推送Docker镜像。
How can we use environment variables without a CryptoKey? 如何在没有CryptoKey的情况下使用环境变量? For example: 例如:
- printf "https://registry.npmjs.org/:_authToken=${NPM_TOKEN}\nregistry=https://registry.npmjs.org" > ~/.npmrc
To use environment variables in the args
portion of your build steps you need: 要在构建步骤的args
部分中使用环境变量,您需要:
See below the break for a more detailed explanation of these two points. 有关这两点的更详细说明,请参见下面的休息。
While the Using encrypted resources docs that David Bendory also linked to (and which you probably based your assumption on) show how to do this using an encrypted environment variable specified via secretEnv
, this is not a requirement and it works with normal environment variables too. 虽然David Bendory也链接到的使用加密资源文档(以及您可能基于您的假设)显示如何使用通过secretEnv
指定的加密环境变量来执行此操作,但这不是secretEnv
,它也适用于普通环境变量。
In your specific case you'll need to modify your build step to look something like this: 在您的特定情况下,您需要修改构建步骤,看起来像这样:
# you didn't show us which builder you're using - this is just one example of
# how you can get a shell using one of the supported builder images
- name: 'gcr.io/cloud-builders/docker'
entrypoint: 'bash'
args: ['-c', 'printf "https://registry.npmjs.org/:_authToken=%s\nregistry=https://registry.npmjs.org" $$NPM_TOKEN > ~/.npmrc']
Note the usage of %s
in the string to be formatted and how the environment variable is passed as an argument to printf
. 请注意要格式化的字符串中%s
的用法以及如何将环境变量作为参数传递给printf
。 I'm not aware of a way that you can include an environment variable value directly in the format string. 我不知道您可以直接在格式字符串中包含环境变量值的方法。
Alternatively you could use echo
as follows: 或者你可以使用echo
如下:
args: ['-c', 'echo "https://registry.npmjs.org/:_authToken=$${NPM_TOKEN}\\nregistry=https://registry.npmjs.org" > ~/.npmrc']
Detailed explanation: 详细说明:
My first point at the top can actually be split in two: 我在顶部的第一点实际上可以分成两部分:
$
character so that Cloud Build doesn't try to perform a substitution here 您需要转义$
字符,以便Cloud Build不会尝试在此处执行替换 If you don't do 2. your build will fail with an error like: Error merging substitutions and validating build: Error validating build: key in the template "NPM_TOKEN" is not a valid built-in substitution 如果你不这样做2.你的构建将失败并出现如下错误 : 错误合并替换和验证构建:错误验证构建:模板中的键“NPM_TOKEN”不是有效的内置替换
You should read through the Substituting variable values docs and make sure that you understand how that works. 您应该阅读替换变量值文档,并确保您了解其工作原理。 Then you need to realise that you are not performing a substitution here, at least not a Cloud Build substitution. 然后你需要意识到你没有在这里进行替换,至少不是云构建替换。 You're asking the shell to perform a substitution. 你要求shell执行替换。
In that context, 2. is actually the only useful piece of information that you'll get from the Substituting variable values docs (that $$
evaluates to the literal character $
). 在这种情况下,2.实际上是您从替换变量值 docs( $$
计算为文字字符$
)中获得的唯一有用信息。
My second point at the top may be obvious if you're used to working with the shell a lot. 如果你习惯使用shell,那么我在顶部的第二点可能是显而易见的。 The reason for needing to use single quotes is well explained by these two questions. 这 两个问题很好地解释了需要使用单引号的原因。 Basically: "You need to use single quotes to prevent interpolation happening in your calling shell." 基本上:“你需要使用单引号来防止在你的调用shell中发生插值。”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.