Azure CLI 任务不适用于 Windows 生成代理

Azure CLI task doesn't work on Windows build agents

我已经创建了一个 Azure DevOps 任务组来在 Azure(Azure 容器实例)上创建一个 selenium 容器,如下所示:

脚本:

ipAddress="$(az container create  \
-g $(resourceGroup) \
--name temp-$(ContainerName) \
--image myregistry.azurecr.io/selenium/standalone-chrome \
--cpu 2 \
--memory 4 \
--os-type Linux \
--ports 4444 \
--vnet $(VNet)
--subnet $(Subnet)
--registry-username $(registryUsername) \
--registry-password $(registryPassword) \
--environment-variables \
NODE_MAX_SESSION=10 \
Browser=$(Browser) \
--query 'ipAddress.ip' -o tsv)"

echo "##vso[task.setvariable variable=$(SeleniumHubVariable);]http://$ipAddress:4444/wd/hub/"

在 Linux 构建代理上 运行ning 时,此任务成功执行。

当我尝试在 Windows 构建代理上 运行 它时,任务不会失败,但不会创建容器。任务输出如下:

F:\Agent03\w5\s>ipAddress="$(az container create \ 'ipAddress' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>-g myresourcegroup \ '-g' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>--name temp-1807-build-385769 \ '--name' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>--image mycontainerregistry.azurecr.io/selenium/standalone-chrome \ '--image' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>--cpu 2 \ '--cpu' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>--memory 4 \ '--memory' is not recognized as an internal or external command, operable program or batch file. '--os-type' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>--os-type Linux \

F:\Agent03\w5\s>--ports 4444 \ '--ports' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>--vnet ..... \ '--vnet' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>--subnet .... \ '--subnet' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>--registry-username myregistryusername \ '--registry-username' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>--registry-password *** \ '--registry-password' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>--environment-variables \ '--environment-variables' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>NODE_MAX_SESSION=10 \ 'NODE_MAX_SESSION' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>Browser=$(Browser) \ 'Browser' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w5\s>--query 'ipAddress.ip' -o tsv)" '--query' is not recognized as an internal or external command, operable program or batch file.

我错过了什么?我怎样才能让它同时适用于 Windows 和 Linux?

我现在可以使用 2 个任务使其与 Windows 代理一起工作。

任务 1 (Azure CLI) - 创建容器实例并将命令(IP 地址)输出到文件

脚本:

az container create ^
-g $(resourceGroup) ^
--name temp-$(ContainerName) ^
--image myregistry.azurecr.io/selenium/standalone-chrome ^
--cpu 2 ^
--memory 4 ^
--os-type Linux ^
--ports 4444 ^
--vnet $(VNet)
--subnet $(Subnet)
--registry-username $(registryUsername) ^
--registry-password $(registryPassword) ^
--environment-variables ^
NODE_MAX_SESSION=10 ^
Browser=$(Browser) ^
--query "ipAddress.ip" -o tsv>tmpFile-$(ContainerName).txt

一些注意事项:

  • \ 个字符被替换为 ^
  • 单引号在 Linux (--query 'ipAddress.ip') 上工作正常,但在 Windows 上我不得不使用双引号 (--query "ipAddress.ip")

任务 2 (Powershell) - 从文件中读取 IP 地址并设置环境变量

$ipAddress = [IO.File]::ReadAllText("tmpFile-$(ContainerName).txt")

Write-Host "Selenium hub URL is http://$($ipAddress.Trim()):4444/wd/hub/"
Write-Host "##vso[task.setvariable variable=$(SeleniumHubVariable);]http://$($ipAddress.Trim()):4444/wd/hub/"

为任务设置自定义条件

不幸的是,我不得不为每个 OS(Windows 或 Linux)创建不同版本的任务:

为了 运行 这些任务取决于构建代理的 OS 您可以设置自定义条件:

因此,对于 运行 Windows 构建代理上的任务,您可以设置此自定义条件:

and(succeeded(), eq(variables['Agent.OS'], 'Windows_NT'))

在 Linux 构建代理上 运行 它:

and(succeeded(), eq(variables['Agent.OS'], 'Linux'))

PS:Agent.OS可以在build agent capabilities上找到