Azure CLI 任务不适用于 Windows 构建代理

标签 azure docker azure-devops azure-cli azure-container-instances

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

Azure container task group

脚本:

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 构建代理上运行时,此任务成功执行。

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

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

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

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

F:\Agent03\w\125\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\w\125\s>--cpu 2 \ '--cpu' is not recognized as an internal or external command, operable program or batch file.

F:\Agent03\w\125\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\w\125\s>--os-type Linux \

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

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

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

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

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

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

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

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

F:\Agent03\w\125\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 地址)输出到文件

Create container instance (Windows)

脚本:

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/"

为任务设置自定义条件

不幸的是,我必须为每个操作系统(Windows 或 Linux)创建不同版本的任务:

Task list

要根据构建代理的操作系统运行这些任务,您可以设置自定义条件:

Custom conditions

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

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

要在 Linux 构建代理上运行它:

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

PS:Agent.OS可以在构建代理功能上找到

关于Azure CLI 任务不适用于 Windows 构建代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56750104/

相关文章:

docker - 在 dockerising nest.js 应用程序上出错

linux - 无法导入 docker 或 docker-py - 没有名为 docker 的模块

azure-devops - Azure DevOps - AWS CloudFormation 和参数

xml - 使用自定义策略在Azure AD B2C中添加用户

t-sql - 如何从 Azure SQL 数据库获取表描述

docker - 如何在 "docker build"期间更新 Docker 镜像中的/etc/hosts 文件

azure - 如何在azure devops中转换dev、uat、prod环境的.xml文件值?

azure - 相当于 Azure 的 Travis CI 中的allow_failures

Azure SDK 和 Azure 存储停用日期和含义

用于访问 Microsoft 图形 API 的 Azure ADAL 库