Trigger automated DockerHub builds
This change triggers automated DockerHub builds for successful official Ubuntu builds of rel.1.0.0. The DockerHub repo for this is https://hub.docker.com/r/microsoft/dotnet-preview/. The variables DOCKER_HUB_REPO and DOCKER_HUB_TRIGGER_TOKEN are set in the "DotNet-CLI-CI (Ubuntu) [rel.1.0.0]" VSTS build definition. This change also allows the Environment TargetConditionAttribute to be used to require that an environment variable is set, in addition to the existing functionality of being able to be used to require that an environment variable is equal to one of a set of specified values.
This commit is contained in:
parent
dfe985fa4c
commit
cd2b638b18
2 changed files with 62 additions and 22 deletions
|
@ -1,12 +1,9 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using Microsoft.DotNet.Cli.Build.Framework;
|
||||
using Microsoft.Extensions.PlatformAbstractions;
|
||||
using Microsoft.WindowsAzure;
|
||||
using Microsoft.WindowsAzure.Storage;
|
||||
using Microsoft.WindowsAzure.Storage.Auth;
|
||||
using Microsoft.WindowsAzure.Storage.Blob;
|
||||
|
||||
using static Microsoft.DotNet.Cli.Build.Framework.BuildHelpers;
|
||||
|
@ -36,7 +33,8 @@ namespace Microsoft.DotNet.Cli.Build
|
|||
|
||||
[Target(nameof(PrepareTargets.Init),
|
||||
nameof(PublishTargets.InitPublish),
|
||||
nameof(PublishTargets.PublishArtifacts))]
|
||||
nameof(PublishTargets.PublishArtifacts),
|
||||
nameof(PublishTargets.TriggerDockerHubBuilds))]
|
||||
[Environment("PUBLISH_TO_AZURE_BLOB", "1", "true")] // This is set by CI systems
|
||||
public static BuildTargetResult Publish(BuildTargetContext c)
|
||||
{
|
||||
|
@ -113,7 +111,7 @@ namespace Microsoft.DotNet.Cli.Build
|
|||
{
|
||||
var packageName = Monikers.GetDebianPackageName(c);
|
||||
var installerFile = c.BuildContext.Get<string>("SdkInstallerFile");
|
||||
var uploadUrl = $"https://dotnetcli.blob.core.windows.net/dotnet/{Channel}/Installers/{Version}/{Path.GetFileName(installerFile)}";
|
||||
var uploadUrl = $"https://dotnetcli.blob.core.windows.net/dotnet/{Channel}/Installers/{Version}/{Path.GetFileName(installerFile)}";
|
||||
var uploadJson = GenerateUploadJsonFile(packageName, Version, uploadUrl);
|
||||
|
||||
Cmd(Path.Combine(Dirs.RepoRoot, "scripts", "publish", "repoapi_client.sh"), "-addpkg", uploadJson)
|
||||
|
@ -123,6 +121,45 @@ namespace Microsoft.DotNet.Cli.Build
|
|||
return c.Success();
|
||||
}
|
||||
|
||||
[Target]
|
||||
[Environment("DOCKER_HUB_REPO")]
|
||||
[Environment("DOCKER_HUB_TRIGGER_TOKEN")]
|
||||
public static BuildTargetResult TriggerDockerHubBuilds(BuildTargetContext c)
|
||||
{
|
||||
string dockerHubRepo = Environment.GetEnvironmentVariable("DOCKER_HUB_REPO");
|
||||
string dockerHubTriggerToken = Environment.GetEnvironmentVariable("DOCKER_HUB_TRIGGER_TOKEN");
|
||||
|
||||
Uri baseDockerHubUri = new Uri("https://registry.hub.docker.com/u/");
|
||||
Uri dockerHubTriggerUri;
|
||||
if (!Uri.TryCreate(baseDockerHubUri, $"{dockerHubRepo}/trigger/{dockerHubTriggerToken}/", out dockerHubTriggerUri))
|
||||
{
|
||||
return c.Failed("Invalid DOCKER_HUB_REPO and/or DOCKER_HUB_TRIGGER_TOKEN");
|
||||
}
|
||||
|
||||
c.Info($"Triggering automated DockerHub builds for {dockerHubRepo}");
|
||||
using (HttpClient client = new HttpClient())
|
||||
{
|
||||
StringContent requestContent = new StringContent("{\"build\": true}", Encoding.UTF8, "application/json");
|
||||
try
|
||||
{
|
||||
HttpResponseMessage response = client.PostAsync(dockerHubTriggerUri, requestContent).Result;
|
||||
if (!response.IsSuccessStatusCode)
|
||||
{
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.AppendLine($"HTTP request to {dockerHubTriggerUri.ToString()} was unsuccessful.");
|
||||
sb.AppendLine($"Response status code: {response.StatusCode}. Reason phrase: {response.ReasonPhrase}.");
|
||||
sb.Append($"Respone content: {response.Content.ReadAsStringAsync().Result}");
|
||||
return c.Failed(sb.ToString());
|
||||
}
|
||||
}
|
||||
catch (AggregateException e)
|
||||
{
|
||||
return c.Failed($"HTTP request to {dockerHubTriggerUri.ToString()} failed. {e.ToString()}");
|
||||
}
|
||||
}
|
||||
return c.Success();
|
||||
}
|
||||
|
||||
private static string GenerateUploadJsonFile(string packageName, string version, string uploadUrl)
|
||||
{
|
||||
var repoID = Environment.GetEnvironmentVariable("REPO_ID");
|
||||
|
@ -133,12 +170,12 @@ namespace Microsoft.DotNet.Cli.Build
|
|||
{
|
||||
using (StreamWriter sw = new StreamWriter(fileStream))
|
||||
{
|
||||
sw.WriteLine("{");
|
||||
sw.WriteLine($" \"name\":\"{packageName}\",");
|
||||
sw.WriteLine($" \"version\":\"{version}\",");
|
||||
sw.WriteLine($" \"repositoryId\":\"{repoID}\",");
|
||||
sw.WriteLine($" \"sourceUrl\":\"{uploadUrl}\"");
|
||||
sw.WriteLine("}");
|
||||
sw.WriteLine("{");
|
||||
sw.WriteLine($" \"name\":\"{packageName}\",");
|
||||
sw.WriteLine($" \"version\":\"{version}\",");
|
||||
sw.WriteLine($" \"repositoryId\":\"{repoID}\",");
|
||||
sw.WriteLine($" \"sourceUrl\":\"{uploadUrl}\"");
|
||||
sw.WriteLine("}");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue