简体   繁体   English

从 Azure Devops 获取所有秘密到 Node.JS 进程

[英]get all secrets from Azure Devops into Node.JS process

I'm trying to pass all secrets from Azure Devops pipeline to Node.js process.我正在尝试将 Azure Devops 管道中的所有秘密传递给 Node.js 进程。 Right now, to do this, I need to pass secrets explicitly as parameters to Node.JS process:现在,要做到这一点,我需要将秘密作为参数显式传递给 Node.JS 进程:

在此处输入图像描述

Additionally, I know, that I can receive a list of all Azure variables from process.env inside Node.JS:此外,我知道,我可以从 Node.JS 中的 process.env 接收所有 Azure 变量的列表:

console.log(process.env);

Is there any way, to pass all secrets and it's values to Node.JS without explicitly pointing it's as parameters to Node.JS but listing it as process.env?有什么方法可以将所有秘密及其值传递给 Node.JS,而无需明确将其作为参数指向 Node.JS,而是将其列为 process.env?

There is a way to pass the secrets to Node.JS without explicitly pointing to it as parameters.有一种方法可以将秘密传递给 Node.JS,而无需明确将其作为参数指向它。

As below it is described in the document Set secret variables .如下所述,它在文档Set secret variables中进行了描述。

Unlike a normal variable, they are not automatically decrypted into environment variables for scripts.与普通变量不同,它们不会自动解密为脚本的环境变量。 You need to explicitly map secret variables.您需要明确 map 秘密变量。

Each task that needs to use the secret as an environment variable does remapping.每个需要使用密钥作为环境变量的任务都会重新映射。 If you want to use a secret variable called mySecret from a script, use the Environment section of the scripting task's input variables.如果要使用脚本中名为 mySecret 的秘密变量,请使用脚本任务输入变量的 Environment 部分。 Set the environment variable name to MYSECRET, and set the value to $(mySecret).将环境变量名称设置为 MYSECRET,并将值设置为 $(mySecret)。

So you need to use the Environment Variables section to map the secrets in side the task.所以你需要使用环境变量部分来 map 任务中的秘密。 Check below screenshot.检查下面的屏幕截图。

在此处输入图像描述

After the secrets are mapped, you donot need to pass the secrets explicitly in the script like what you are currently doing.映射秘密后,您无需像当前所做的那样在脚本中显式传递秘密。 You can use process.env.API_KEY directly to get the secrets inside Node.JS.您可以直接使用process.env.API_KEY来获取 Node.JS 中的秘密。

Hope above helps!以上希望有帮助!

I tried first az cli我首先尝试az cli

az pipelines variable list --org "https://dev.azure.com/organization-name" --project "project-name" --pipeline-name "pipeline-name"

But I got:但我得到了:

az : This command group is in preview. It may be changed/removed in a future release.
At line:1 char:1
+ az pipelines variable list --org "https://dev.azure.com/organization-name...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (This command gr...future release.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

I am able to get variables using:我可以使用以下方法获取变量:

  - task: Bash@3
    inputs:
      targetType: 'inline'
      script: 'env | sort'

But you will get also bunch of prebuild variables.但是你也会得到一堆预编译变量。

My defined variables:我定义的变量:

在此处输入图像描述

and here is the log output:这是日志 output:

ImageVersion=20200330.1
INPUT_ARGUMENTS=
ISPROD=true
JAVA_HOME_11_X64=/usr/lib/jvm/zulu-11-azure-amd64
JAVA_HOME_12_X64=/usr/lib/jvm/zulu-12-azure-amd64

You can also try to call API .您也可以尝试致电API I haven't tried this aproach.我没有尝试过这种方法。 But it should be doable.但这应该是可行的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM