我需要从本地计算机上的azure管道下载工件。任何人都可以使用Powershell脚本来帮助完成此任务吗?
答案 0 :(得分:3)
上面提供的解决方案不起作用或不再起作用 - API 似乎已更改。如果您使用解决方案,您会收到要求您登录的 HTML 内容。
新 API 返回带有 downloadUrl 的 JSON:
{
"id": 105284,
"name": "Artifact",
"resource": {
...
"downloadUrl": "https://artprodsu6weu.artifacts.visualstudio.com/....."
}
}
所以这里有一些可以正常工作的代码:
$BuildId = "154782"
$ArtifactName = "Artifact"
$OutFile = "Artifact.zip"
$OrgName="myorg"
$ProjectName="myproject"
$PAT = "**************"
$url = "https://dev.azure.com/$($OrgName)/$($ProjectName)/_apis/build/builds/$($BuildID)/artifacts?artifactName=$($ArtifactName)&api-version=&format=zip"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($PAT)"))
# Get the download URL
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"}
$downloadUrl = $response.resource.downloadUrl
# Download the artifact
$response = Invoke-WebRequest -Uri $downloadUrl -Headers @{Authorization = "Basic $token"} -OutFile $OutFile
答案 1 :(得分:1)
您可以使用Artifacts Rest API:
$token = "Your PAT"
# Create Authorization header
$headers = @{"Authorization" = "Bearer $token"}
# Create Web client - used to downlaod files
$wc = New-Object System.Net.WebClient
$wc.Headers.Add("Authorization", $headers["Authorization"])
# Get Build artifact details
$buildId = "the artifats build id"
$artifactsUrl = "https://dev.azure.com/{organization}/{project}/_apis/build/builds/$buildId/artifacts?api-version=4.1"
$buildArtifacts = Invoke-RestMethod -Method Get -Headers $headers -Uri $artifactsUrl
foreach($buildArtifact in $buildArtifacts.value){
# Download build artifacts - ZIP files
$url = $buildArtifact.resource.downloadUrl
$output = Join-Path $artifactsDir "$($buildArtifact.name).zip"
$wc.DownloadFile($url, $output)
}
$wc.Dispose()
答案 2 :(得分:1)
$token = "xxx"
$url="https://dev.azure.com/{OrgName}/{ProjectName}/_apis/build/builds/{BuildID}/artifacts?artifactName={ArtifactName}&api-version=6.1-preview.5&%24format=zip"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$response = Invoke-RestMethod -Uri $url -Headers @{Authorization = "Basic $token"} -Method Get -ContentType application/zip -OutFile "{SomePath}\Response.zip"
注意:在网址之后添加&%24format=zip
并设置-ContentType application/zip -OutFile "{SomePath}\Response.zip"
您需要用自己的值替换token(PAT),OrgName,ProjectName,BuildID,ArtifactName
。然后选择一个现有路径来保存响应,例如C:\pub\Response.zip
。我已有路径C:\pub
,运行PS脚本后,我可以得到一个创建的Response.zip
,其中包含我需要的工件。
此外,您还可以尝试通过c#代码下载构建工件。有关详细信息,请参阅此ticket。
static readonly string TFUrl = "https://dev.azure.com/OrgName/";
static readonly string UserPAT = "PAT";
static void Main(string[] args)
{
try
{
int buildId = xx; // update to an existing build definition id
string artifactName = "drop"; //default artifact name
// string project = "projectName";
ConnectWithPAT(TFUrl, UserPAT);
Stream zipStream = BuildClient.GetArtifactContentZipAsync(buildId, artifactName).Result; //get content
using (FileStream zipFile = new FileStream(@"C:\MySite\test.zip", FileMode.Create))
zipStream.CopyTo(zipFile);
Console.WriteLine("Done");
}
catch (Exception ex)
{
Console.WriteLine("Exception: " + ex.Message);
if (ex.InnerException != null) Console.WriteLine("Detailed Info: " + ex.InnerException.Message);
Console.WriteLine("Stack:\n" + ex.StackTrace);
}
}