首页 > 解决方案 > 在 Azure Devops 中,如何在运行 shell 脚本的“Azure CLI task v.2”中使用管道变量?

问题描述

我正在尝试在 Azure Devops 中使用 Azure CLI 和bash执行任务。我正在使用Azure CLI 任务 v.2并选择shell作为脚本类型,如下所示。 在此处输入图像描述 我想在 bash 脚本中使用管道变量。我在脚本中运行以下命令:

#!/bin/bash
az role assignment create --role "Lab Admin" --assignee $(ownerEmail) -g $(rgName)

我收到以下错误:

 line 2: ownerEmail: command not found
 line 2: rgName: command not found

我不明白。通常,我应该能够在 bash 脚本中使用 azure cli。有人有想法吗?

标签: bashazureazure-devopsazure-pipelinesazure-cli

解决方案


有几种方法可以读取 bash 脚本中的管道变量:

  1. (建议)将它们作为脚本中的环境变量直接阅读。Azure DevOps 管道变量作为环境变量添加,您的 bash 脚本可以访问这些变量。因此,只需定义/设置管道变量,您就可以从 bash 脚本访问它们。即在您的脚本中将它们引用为$OWNERNAME 和$RGNAME。
    请参阅 > https://docs.microsoft.com/en-us/azure/devops/pipelines/process/variables#environment-variables

  2. 引用 bash 脚本中的直接参数。Bash 参数是编号的,可以这样引用。例如,对于您的参数,$1 是字符串“-labOwner” $2 是包含在 ownerEmail 管道变量中的值。
    请参阅 > https://tecadmin.net/tutorial/bash-scripting/bash-command-arguments/


推荐阅读