forked from Inedo/inedox-teamcity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetArtifactAction.cs
115 lines (100 loc) · 4.53 KB
/
GetArtifactAction.cs
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.IO;
using Inedo.BuildMaster;
using Inedo.BuildMaster.Extensibility.Actions;
using Inedo.BuildMaster.Extensibility.Agents;
using Inedo.BuildMaster.Web;
namespace Inedo.BuildMasterExtensions.TeamCity
{
/// <summary>
/// Gets an artifact from a TeamCity server.
/// </summary>
[ActionProperties(
"Get TeamCity Artifact",
"Gets an artifact from a TeamCity server.",
DefaultToLocalServer = true)]
[CustomEditor(typeof(GetArtifactActionEditor))]
[Tag(Tags.ContinuousIntegration)]
public sealed class GetArtifactAction : TeamCityActionBase
{
public GetArtifactAction()
{
this.ExtractFilesToTargetDirectory = true;
}
[Persistent]
public string ArtifactName { get; set; }
[Persistent]
public string BuildConfigurationId { get; set; }
[Persistent]
public string BuildNumber { get; set; }
[Persistent]
public string BranchName { get; set; }
[Persistent]
public bool ExtractFilesToTargetDirectory { get; set; }
public override ActionDescription GetActionDescription()
{
return new ActionDescription(
new ShortActionDescription("Get TeamCity ", new Hilite(this.ArtifactName), " Artifact "),
new LongActionDescription("of build ",
InedoLib.Util.Int.ParseN(this.BuildNumber) != null ? "#" : "",
new Hilite(this.BuildNumber),
!string.IsNullOrEmpty(this.BranchName) ? " on branch " + this.BranchName : "",
" of the configuration \"",
this.BuildConfigurationId,
"\" and ",
this.ExtractFilesToTargetDirectory ? "deploy its contents" : "copy the artifact",
" to ",
new DirectoryHilite(this.OverriddenTargetDirectory)
)
);
}
protected override void Execute()
{
var configurer = this.GetExtensionConfigurer();
string relativeUrl = string.Format("repository/download/{0}/{1}/{2}", this.BuildConfigurationId, this.BuildNumber, this.ArtifactName);
string branchName = this.GetBranchName(configurer);
if (branchName != null)
{
this.LogDebug("Getting artifact using branch: " + branchName);
relativeUrl += "?branch=" + Uri.EscapeDataString(branchName);
}
this.LogDebug("Downloading TeamCity artifact \"{0}\" from {1} to {2}", this.ArtifactName, configurer.BaseUrl + relativeUrl, this.Context.TargetDirectory);
var fileOps = this.Context.Agent.GetService<IFileOperationsExecuter>();
var remoteZip = this.Context.Agent.GetService<IRemoteZip>();
string tempFile;
using (var client = new TeamCityWebClient(configurer))
{
tempFile = Path.GetTempFileName();
client.DownloadFile(relativeUrl, tempFile);
}
if (this.ExtractFilesToTargetDirectory)
{
this.LogDebug("Transferring artifact to {0} before extracting...", this.Context.TempDirectory);
string remoteTempPath = fileOps.CombinePath(this.Context.TempDirectory, this.ArtifactName);
fileOps.WriteFileBytes(
remoteTempPath,
File.ReadAllBytes(tempFile)
);
this.LogDebug("Extracting TeamCity artifact to {0}...", this.Context.TargetDirectory);
remoteZip.ExtractZipFile(remoteTempPath, this.Context.TargetDirectory, true);
}
else
{
this.LogDebug("Transferring artifact to {0}...", this.Context.TargetDirectory);
fileOps.WriteFileBytes(
fileOps.CombinePath(this.Context.TargetDirectory, this.ArtifactName),
File.ReadAllBytes(tempFile)
);
}
this.LogInformation("Artifact retrieved successfully.");
}
private string GetBranchName(TeamCityConfigurer configurer)
{
if (!string.IsNullOrEmpty(this.BranchName))
return this.BranchName;
if (!string.IsNullOrEmpty(configurer.DefaultBranchName))
return configurer.DefaultBranchName;
return null;
}
}
}