-
Notifications
You must be signed in to change notification settings - Fork 231
/
buildArtifact.ts
85 lines (74 loc) · 3.76 KB
/
buildArtifact.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// A sample showing how to list VSTS build artifacts, and how to download a zip of a VSTS build artifact.
import * as cm from "./common";
import * as vm from "azure-devops-node-api";
import * as fs from "fs";
import * as ba from "azure-devops-node-api/BuildApi";
import * as bi from "azure-devops-node-api/interfaces/BuildInterfaces";
export async function run() {
try
{
const vsts: vm.WebApi = await cm.getWebApi();
const vstsBuild: ba.IBuildApi = await vsts.getBuildApi();
cm.banner("Build Artifact Samples");
const project = cm.getProject();
console.log("project", project);
// get the latest successful build.
cm.heading(`Get latest successful build for ${project} project`);
const builds: bi.Build[] = await vstsBuild.getBuilds(
project,
null, // definitions: number[]
null, // queues: number[]
null, // buildNumber
null, //new Date(2016, 1, 1), // minFinishTime
null, // maxFinishTime
null, // requestedFor: string
bi.BuildReason.All, // reason
bi.BuildStatus.Completed,
bi.BuildResult.Succeeded,
null, // tagFilters: string[]
null, // properties: string[]
//bi.DefinitionType.Build,
1 // top: number
);
if (builds.length > 0) {
const latestBuild: bi.Build = builds[0];
console.log(`build ${latestBuild.id}`);
// Retrieve the list of artifacts for the latest build.
cm.heading(`Artifacts for build ${latestBuild.id}, ${project} project`);
const artifacts: bi.BuildArtifact[] = await vstsBuild.getArtifacts(project, latestBuild.id);
let downloadableArtifact;
for (const artifact of artifacts) {
let additionalInfo = "";
if (artifact.resource.type === "FilePath") {
additionalInfo = `UNC Path: ${artifact.resource.data}`;
}
else if (artifact.resource.type === "Container") {
// As of June 2018, only `Container` artifacts can be downloaded as a zip.
additionalInfo = `Downloadable: true.`;
downloadableArtifact = artifact;
}
console.log(`Artifact: '${artifact.name}'. Type: ${artifact.resource.type}. Id: ${artifact.id}. ${additionalInfo}`);
}
// Download an artifact.
if (downloadableArtifact) {
cm.heading(`Download zip of artifact '${downloadableArtifact.name}' for build ${latestBuild.id}, ${project} project`);
const artifactStream: NodeJS.ReadableStream = await vstsBuild.getArtifactContentZip(project, latestBuild.id, downloadableArtifact.name);
const path = `${downloadableArtifact.name}.zip`;
const fileStream = fs.createWriteStream(path);
artifactStream.pipe(fileStream);
fileStream.on("close", () => {
console.log(`Artifact '${downloadableArtifact.name}' downloaded to ${path}`);
});
}
else {
console.log("No downloadable artifact found.");
}
}
else {
console.log("No successful builds found.");
}
}
catch (err) {
console.error(`Error: ${err.stack}`);
}
}