Azure Devops - 如何动态传递数据库连接字符串作为环境变量(azure cli)

问题描述 投票:0回答:1

目前我正在使用 azure devops 将应用程序容器部署到 azure。

我已经创建了发布管道,并在变量组中添加了每个环境变量。

为了动态调用环境变量,请在 Azure CLI 任务中使用以下 CLI 命令

#!/bin/bash

# Initialize variable
ENV_VARS=""

# Loop through all environment variables
while IFS= read -r var
do
  # Check if the variable contains __ and starts with specific prefixes
  if [[ "$var" == APPSETTINGS__* || "$var" == AWSS3CONFIG__* || "$var" == CONNECTIONSTRINGS__* || "$var" == SMTPSETTINGS__* ]]; then
    # Extract the variable name and value
    name=$(echo "$var" | cut -d '=' -f 1 | sed 's/^//')
    value=$(echo "$var" | cut -d '=' -f 2-)

    if [[ "$value" == *" "* ]]; then
      value="\"$value\""
    fi

    # Append the variable and its value to the result
    ENV_VARS+="$name=$value "
  fi
done <<< "$(env)"

echo "Constructed envVars: $ENV_VARS"

# Example command using constructed envVars
az containerapp update \
            --name api-dev \
            --resource-group resource-group \
            --image dev.azurecr.io/api:$(Build.BuildId) \
            --set-env-vars "$ENV_VARS"

但是,数据库连接字符串设置不正确,并且由于空格,它被视为新键。例如用户 ID 和保留安全信息。

enter image description here

如何摆脱空白?请帮忙!

我尝试将存储在变量组中的变量的值设置为双引号,也直接在脚本中设置,但它们都不起作用

bash azure-pipelines azure-cli azure-pipelines-release-pipeline
1个回答
0
投票

您可以通过 3 种方式传递环境变量..

  • 只需设置它们

    导出COOKIE=草莓

  • 将它们放在您正在执行的程序前面

    COOKIE1=巧克力 COOKIE2=花生 bash -c 'set | grep COOKIE'

  • 或者,如果您像您一样创建一组字符串,请使用“eval”

    eval '导出 COOKIE1=椰子;导出 COOKIE2=燕麦片' 集| grep Cookie

© www.soinside.com 2019 - 2024. All rights reserved.