如何使用 powershell 将管道工件从 azure pipeline 下载到本地?
How to download pipeline artifacts from azure pipeline to local using powershell?
我需要从本地计算机上的 Azure 管道下载工件。任何人都可以使用 powershell 脚本帮助完成这项工作吗?
您可以使用 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()
$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"
注:在url后加上&%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#代码下载构建神器。详情请参考这个.
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);
}
}
上面提供的解决方案不起作用或不再起作用 - API 似乎已经改变。如果您使用解决方案,您会收到 HTML 要求您登录的内容。
新的 API returns JSON with a downloadUrl:
{
"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
我需要从本地计算机上的 Azure 管道下载工件。任何人都可以使用 powershell 脚本帮助完成这项工作吗?
您可以使用 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()
$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"
注:在url后加上&%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#代码下载构建神器。详情请参考这个
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);
}
}
上面提供的解决方案不起作用或不再起作用 - API 似乎已经改变。如果您使用解决方案,您会收到 HTML 要求您登录的内容。
新的 API returns JSON with a downloadUrl:
{
"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