简体   繁体   English

Google Cloud Build中的环境变量

[英]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部分中使用环境变量,您需要:

  • "a shell to resolve environment variables with $$" (as mentioned in the example code here ) “一个壳,以解决$$环境变量”(如示例代码中提到这里
  • and you also need to be careful with your usage of quotes (use single quotes) 并且您还需要小心使用引号(使用单引号)

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: 我在顶部的第一点实际上可以分成两部分:

  1. you need a shell to resolve environment variables, and 你需要一个shell来解决环境变量,和
  2. you need to escape the $ 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.

相关问题 在 Google Cloud Build 中使用 Google Cloud Secret 作为环境变量 - Using Google Cloud Secret as environment variables in Google Cloud Build 使用文件将环境变量加载到Google Cloud Build中 - Loading in environment variables to Google Cloud Build using a file 如何在 Google App Engine 标准环境中使用 Google Cloud Build 或其他方法设置环境变量? - How to set environment variables using Google Cloud Build or other method in Google App Engine Standard Environment? 谷歌云 sdk 环境变量 - Google cloud sdk environment variables Google Cloud Functions环境变量 - Google Cloud Functions Environment Variables 将秘密存储为云构建环境变量 - Storing Secrets as cloud build environment variables 如何在cloudbuild.yaml中使用带有KMS的Google Cloud Build将多个环境变量作为秘密传递? - How to pass multiple environment variables as secrets using Google Cloud Build with KMS in cloudbuild.yaml? Google Cloud Compute,使用环境变量 - Google Cloud Compute, using environment variables 在 Google Cloud Console UI 中应用环境变量 - Applying environment variables in Google Cloud Console UI 为 Flask 设置 Google Cloud 环境变量 - Setting Google Cloud Environment Variables for Flask
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM