我正在尝试在容器中运行管道,我正在使用 azure 管道提供的内置支持通过 azure 代理在容器中运行作业。
这就是我的 YAML 的样子:
resources:
containers:
- container: myContainerResource
image: myregistry/image
endpoint: 'azuredevops-service-connection'
volumes:
- 'C:\X:C:\X'
- 'C:\Y:C:\Y'
stages:
- stage:
jobs:
- job:
pool:
name: myPool
container: myContainerResource
steps:
- powershell: | # First Powershell task
whoami # This is printed as 'user manager\containeradministrator'
net use \\share-path\folder1$ /user:$env:ACCOUNT $env:PASSWORD
if (Test-Path -ErrorAction SilentlyContinue \\share-path\folder1$)
{
Write-Host "Mapping \\mr-fs01\midas$ finished successfully" # This is executed
}
else
{
Write-Host "Mapping failed"
exit 1
}
gc \\share-path\folder1$\myFile.txt # The content of the file is accessible and printed
env:
ACCOUNT: $(USER_SECRET_FROM_VAR_GROUP_KEY_VAULT)
PASSWORD: $(PWD_SECRET_FROM_VAR_GROUP_KEY_VAULT)
- powershell: | # Second Powershell task
whoami
gc \\share-path\folder1$\myFile.txt # HERE THE SAME PATH IS NOT ACCESSIBLE, TROWS EXCEPTION
“net use”命令在第一个 PowerShell 任务中正确执行,并且文件也可以访问。当我尝试从第二个 PowerShell 任务访问共享或文件时,它会抛出异常:
user manager\containeradministrator
gc : Access is denied
At C:\__w\_temp\0sdsd3-wewe-4d92234-2-xcxc3232.ps1:5 char:1
+ gc \\share-path\folder1$\myFile.txt
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : PermissionDenied: (\\share-path\folder1$\myFile.txt:Stri
ng) [Get-Content], UnauthorizedAccessException
+ FullyQualifiedErrorId : ItemExistsUnauthorizedAccessError,Microsoft.Powe
rShell.Commands.GetContentCommand
是的,我知道可以将共享映射为卷,它可以与我的容器一起使用。但是,我的测试对网络共享路径进行了硬编码。因此,在这种情况下我无法使用 Docker 卷方法。 如何让第二个任务访问第一个任务中完美映射的网络共享?
根据进一步的讨论,实际要求是在容器代理环境中运行
VSTest
,其测试的编写方式是从硬编码的网络共享路径访问一些依赖项。
据我测试,
VSTest
管道任务本身不支持在执行测试之前验证对网络共享路径的访问的这种场景。
但是,由于我们可以使用
PowerShell
脚本来验证对该共享路径的访问,因此您可以考虑在 samePowerShell 会话中运行
vstest
。
stages:
- stage:
jobs:
- job:
pool:
name: myPool
container: myContainerResource
steps:
- powershell: | # First Powershell task
whoami # This is printed as 'user manager\containeradministrator'
net use \\share-path\folder1$ /user:$env:ACCOUNT $env:PASSWORD
if (Test-Path -ErrorAction SilentlyContinue \\share-path\folder1$)
{
Write-Host "Mapping \\mr-fs01\midas$ finished successfully" # This is executed
}
else
{
Write-Host "Mapping failed"
exit 1
}
gc \\share-path\folder1$\myFile.txt # The content of the file is accessible and printed
cd "C:\Program Files\Microsoft Visual Studio\2022\Enterprise\Common7\IDE\Extensions\TestPlatform\" # The location in the container where `vstest.console.exe` tool is installed
.\vstest.console.exe "$(Build.SourceDirectory)\DotNetCore\DotNetCoreTests\bin\Debug\net7.0\DotNetCoreTests.dll" # Use vstest.console.exe to test the assemblies (dotnet tests in my case)
env:
ACCOUNT: $(USER_SECRET_FROM_VAR_GROUP_KEY_VAULT)
PASSWORD: $(PWD_SECRET_FROM_VAR_GROUP_KEY_VAULT)