From 05a014958fe0c2871e15bdf4f012d5d166b0fb14 Mon Sep 17 00:00:00 2001 From: Dean Herbert Date: Mon, 13 Feb 2017 16:48:53 +0900 Subject: [PATCH] Add complete publish automation. Uses AppSettings to allow for secret storage. --- osu.Desktop.Deploy/App.config | 20 +- osu.Desktop.Deploy/GitHubRelease.cs | 25 ++ osu.Desktop.Deploy/Program.cs | 279 ++++++++++++++----- osu.Desktop.Deploy/osu.Desktop.Deploy.csproj | 2 + 4 files changed, 254 insertions(+), 72 deletions(-) create mode 100644 osu.Desktop.Deploy/GitHubRelease.cs diff --git a/osu.Desktop.Deploy/App.config b/osu.Desktop.Deploy/App.config index 836f2237d6..6272e396fb 100644 --- a/osu.Desktop.Deploy/App.config +++ b/osu.Desktop.Deploy/App.config @@ -1,8 +1,22 @@  - - - + + + + + + + + + + + + + + + + + diff --git a/osu.Desktop.Deploy/GitHubRelease.cs b/osu.Desktop.Deploy/GitHubRelease.cs new file mode 100644 index 0000000000..7e7b04fe58 --- /dev/null +++ b/osu.Desktop.Deploy/GitHubRelease.cs @@ -0,0 +1,25 @@ +using Newtonsoft.Json; + +namespace osu.Desktop.Deploy +{ + internal class GitHubRelease + { + [JsonProperty(@"id")] + public int Id; + + [JsonProperty(@"tag_name")] + public string TagName => $"v{Name}"; + + [JsonProperty(@"name")] + public string Name; + + [JsonProperty(@"draft")] + public bool Draft; + + [JsonProperty(@"prerelease")] + public bool PreRelease; + + [JsonProperty(@"upload_url")] + public string UploadUrl; + } +} \ No newline at end of file diff --git a/osu.Desktop.Deploy/Program.cs b/osu.Desktop.Deploy/Program.cs index 96d041a4c9..a0e7ff796a 100644 --- a/osu.Desktop.Deploy/Program.cs +++ b/osu.Desktop.Deploy/Program.cs @@ -1,12 +1,17 @@ // Copyright (c) 2007-2017 ppy Pty Ltd . -// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu-framework/master/LICENCE +// Licensed under the MIT Licence - https://raw.GitHubusercontent.com/ppy/osu-framework/master/LICENCE using System; using System.Collections.Generic; +using System.Configuration; using System.Diagnostics; using System.IO; using System.Linq; +using System.Net; +using Newtonsoft.Json; using osu.Framework.IO.Network; +using FileWebRequest = osu.Framework.IO.Network.FileWebRequest; +using WebRequest = osu.Framework.IO.Network.WebRequest; namespace osu.Desktop.Deploy { @@ -16,24 +21,37 @@ namespace osu.Desktop.Deploy private const string squirrel_path = @"packages\squirrel.windows.1.5.2\tools\Squirrel.exe"; private const string msbuild_path = @"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe"; - public static string StagingFolder = "Staging"; - public static string ReleasesFolder = "Releases"; + public static string StagingFolder = ConfigurationManager.AppSettings["StagingFolder"]; + public static string ReleasesFolder = ConfigurationManager.AppSettings["ReleasesFolder"]; + public static string GitHubAccessToken = ConfigurationManager.AppSettings["GitHubAccessToken"]; + public static string GitHubUsername = ConfigurationManager.AppSettings["GitHubUsername"]; + public static string GitHubRepoName = ConfigurationManager.AppSettings["GitHubRepoName"]; + public static string SolutionName = ConfigurationManager.AppSettings["SolutionName"]; + public static string ProjectName = ConfigurationManager.AppSettings["ProjectName"]; + public static string NuSpecName = ConfigurationManager.AppSettings["NuSpecName"]; + public static string TargetName = ConfigurationManager.AppSettings["TargetName"]; + public static string PackageName = ConfigurationManager.AppSettings["PackageName"]; + public static string IconName = ConfigurationManager.AppSettings["IconName"]; + public static string CodeSigningCertificate = ConfigurationManager.AppSettings["CodeSigningCertificate"]; - public static string ProjectName = "osu.Desktop"; - public static string CodeSigningCert => Path.Combine(homeDir, "deanherbert.pfx"); + public static string GitHubApiEndpoint => $"https://api.github.com/repos/{GitHubUsername}/{GitHubRepoName}/releases"; + public static string GitHubReleasePage => $"https://github.com/{GitHubUsername}/{GitHubRepoName}/releases"; + /// + /// How many previous build deltas we want to keep when publishing. + /// const int keep_delta_count = 3; - private static string codeSigningCmd => string.IsNullOrEmpty(codeSigningPassword) ? "" : $"-n \"/a /f {CodeSigningCert} /p {codeSigningPassword} /t http://timestamp.comodoca.com/authenticode\""; + private static string codeSigningCmd => string.IsNullOrEmpty(codeSigningPassword) ? "" : $"-n \"/a /f {codeSigningCertPath} /p {codeSigningPassword} /t http://timestamp.comodoca.com/authenticode\""; private static string homeDir => Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); - + private static string codeSigningCertPath => Path.Combine(homeDir, CodeSigningCertificate); private static string solutionPath => Environment.CurrentDirectory; private static string stagingPath => Path.Combine(solutionPath, StagingFolder); - private static string iconPath => Path.Combine(solutionPath, ProjectName, "lazer.ico"); + private static string iconPath => Path.Combine(solutionPath, ProjectName, IconName); - private static string nupkgFilename(string ver) => $"osulazer.{ver}.nupkg"; - private static string nupkgDistroFilename(string ver) => $"osulazer-{ver}-full.nupkg"; + private static string nupkgFilename(string ver) => $"{PackageName}.{ver}.nupkg"; + private static string nupkgDistroFilename(string ver) => $"{PackageName}-{ver}-full.nupkg"; private static string codeSigningPassword; @@ -47,54 +65,29 @@ namespace osu.Desktop.Deploy Directory.CreateDirectory(ReleasesFolder); } - Console.WriteLine("Checking github releases..."); + checkGitHubReleases(); - var req = new JsonWebRequest>("https://api.github.com/repos/ppy/osu/releases"); - req.BlockingPerform(); - - if (req.ResponseObject.Count > 0) - { - var release = req.ResponseObject[0]; - Console.WriteLine($"Last version pushed was {release.Name}"); - - if (!File.Exists(Path.Combine(ReleasesFolder, nupkgDistroFilename(release.Name)))) - { - Console.WriteLine("Not found locally; let's pull down last release data."); - - req = new JsonWebRequest>($"https://api.github.com/repos/ppy/osu/releases/{release.Id}/assets"); - req.BlockingPerform(); - - foreach (var asset in req.ResponseObject) - { - Console.WriteLine($"Downloading {asset.Name}..."); - var dl = new FileWebRequest(Path.Combine(ReleasesFolder, asset.Name), $"https://api.github.com/repos/ppy/osu/releases/assets/{asset.Id}"); - dl.BlockingPerform(); - } - } - } - - if (Directory.Exists(StagingFolder)) - Directory.Delete(StagingFolder, true); - Directory.CreateDirectory(StagingFolder); - - string verBase = DateTime.Now.ToString("yyyy.Md."); - int increment = 0; + refreshDirectory(StagingFolder); //increment build number until we have a unique one. + string verBase = DateTime.Now.ToString("yyyy.Md."); + int increment = 0; while (Directory.GetFiles(ReleasesFolder, $"*{verBase}{increment}*").Any()) increment++; string version = $"{verBase}{increment}"; Console.Write($"Ready to deploy {version}: "); - version += Console.ReadLine(); - - Console.Write("Enter code signing password: "); - var fg = Console.ForegroundColor; - Console.ForegroundColor = Console.BackgroundColor; - codeSigningPassword = Console.ReadLine(); - Console.ForegroundColor = fg; + Console.ReadLine(); + if (!string.IsNullOrEmpty(CodeSigningCertificate)) + { + Console.Write("Enter code signing password: "); + var fg = Console.ForegroundColor; + Console.ForegroundColor = Console.BackgroundColor; + codeSigningPassword = Console.ReadLine(); + Console.ForegroundColor = fg; + } Console.WriteLine("Restoring NuGet packages..."); runCommand(nuget_path, "restore " + solutionPath); @@ -103,15 +96,53 @@ namespace osu.Desktop.Deploy updateAssemblyInfo(version); Console.WriteLine("Running build process..."); - runCommand(msbuild_path, $"/v:quiet /m /t:Client\\{ProjectName.Replace('.', '_')} /p:OutputPath={stagingPath};Configuration=Release osu.sln"); + runCommand(msbuild_path, $"/v:quiet /m /t:{TargetName.Replace('.', '_')} /p:OutputPath={stagingPath};Configuration=Release {SolutionName}.sln"); Console.WriteLine("Creating NuGet deployment package..."); - runCommand(nuget_path, $"pack osu.Desktop\\osu.nuspec -Version {version} -Properties Configuration=Deploy -OutputDirectory {stagingPath} -BasePath {stagingPath}"); + runCommand(nuget_path, $"pack {NuSpecName} -Version {version} -Properties Configuration=Deploy -OutputDirectory {stagingPath} -BasePath {stagingPath}"); + //prune once before checking for files so we can avoid erroring on files which aren't even needed for this build. + pruneReleases(); + + checkReleaseFiles(); + + Console.WriteLine("Releasifying package..."); + runCommand(squirrel_path, $"--releasify {stagingPath}\\{nupkgFilename(version)} --setupIcon {iconPath} --icon {iconPath} {codeSigningCmd} --no-msi"); + + //prune again to clean up before upload. + pruneReleases(); + + //rename setup to install. + File.Copy(Path.Combine(ReleasesFolder, "Setup.exe"), Path.Combine(ReleasesFolder, "install.exe"), true); + File.Delete(Path.Combine(ReleasesFolder, "Setup.exe")); + + uploadBuild(version); + + Console.WriteLine("Done!"); + Console.ReadLine(); + } + + /// + /// Ensure we have all the files in the release directory which are expected to be there. + /// This should have been accounted for in earlier steps, and just serves as a verification step. + /// + private static void checkReleaseFiles() + { + var releaseLines = getReleaseLines(); + + //ensure we have all files necessary + foreach (var l in releaseLines) + if (!File.Exists(Path.Combine(ReleasesFolder, l.Filename))) + error($"Local file missing {l.Filename}"); + } + + private static IEnumerable getReleaseLines() => File.ReadAllLines(Path.Combine(ReleasesFolder, "RELEASES")).Select(l => new ReleaseLine(l)); + + private static void pruneReleases() + { Console.WriteLine("Pruning RELEASES..."); - var releaseLines = new List(); - foreach (var l in File.ReadAllLines(Path.Combine(ReleasesFolder, "RELEASES"))) releaseLines.Add(new ReleaseLine(l)); + var releaseLines = getReleaseLines().ToList(); var fulls = releaseLines.Where(l => l.Filename.Contains("-full")).Reverse().Skip(1); @@ -135,25 +166,115 @@ namespace osu.Desktop.Deploy } } - //ensure we have all files necessary - foreach (var l in releaseLines) - if (!File.Exists(Path.Combine(ReleasesFolder, l.Filename))) - error($"Local file missing {l.Filename}"); - - List lines = new List(); - foreach (var l in releaseLines) - lines.Add(l.ToString()); + var lines = new List(); + releaseLines.ForEach(l => lines.Add(l.ToString())); File.WriteAllLines(Path.Combine(ReleasesFolder, "RELEASES"), lines); + } - Console.WriteLine("Releasifying package..."); - runCommand(squirrel_path, $"--releasify {stagingPath}\\{nupkgFilename(version)} --setupIcon {iconPath} --icon {iconPath} {codeSigningCmd} --no-msi"); + private static void uploadBuild(string version) + { + if (string.IsNullOrEmpty(GitHubAccessToken) || string.IsNullOrEmpty(codeSigningCertPath)) + return; - //rename setup to install. - File.Copy(Path.Combine(ReleasesFolder, "Setup.exe"), Path.Combine(ReleasesFolder, "install.exe"), true); - File.Delete(Path.Combine(ReleasesFolder, "Setup.exe")); + Console.WriteLine("Publishing to GitHub..."); - Console.WriteLine("Done!"); - Console.ReadLine(); + Console.WriteLine($"- Creating release {version}..."); + var req = new JsonWebRequest($"{GitHubApiEndpoint}") + { + Method = HttpMethod.POST + }; + req.AddRaw(JsonConvert.SerializeObject(new GitHubRelease + { + Name = version, + Draft = true, + PreRelease = true + })); + req.AuthenticatedBlockingPerform(); + + var assetUploadUrl = req.ResponseObject.UploadUrl.Replace("{?name,label}", "?name={0}"); + foreach (var a in Directory.GetFiles(ReleasesFolder)) + { + Console.WriteLine($"- Adding asset {a}..."); + var upload = new WebRequest(assetUploadUrl, Path.GetFileName(a)) + { + Method = HttpMethod.POST, + ContentType = "application/octet-stream", + }; + + upload.AddRaw(File.ReadAllBytes(a)); + upload.AuthenticatedBlockingPerform(); + } + + openGitHubReleasePage(); + } + + private static void openGitHubReleasePage() => Process.Start(GitHubReleasePage); + + private static void checkGitHubReleases() + { + Console.WriteLine("Checking GitHub releases..."); + var req = new JsonWebRequest>($"{GitHubApiEndpoint}"); + req.AuthenticatedBlockingPerform(); + + var lastRelease = req.ResponseObject.FirstOrDefault(); + + if (lastRelease == null) + return; + + if (lastRelease.Draft) + { + openGitHubReleasePage(); + error("There's a pending draft release! You probably don't want to push a build with this present."); + } + + //there's a previous release for this project. + var assetReq = new JsonWebRequest>($"{GitHubApiEndpoint}/{lastRelease.Id}/assets"); + assetReq.AuthenticatedBlockingPerform(); + var assets = assetReq.ResponseObject; + + //make sure our RELEASES file is the same as the last build on the server. + var releaseAsset = assets.FirstOrDefault(a => a.Name == "RELEASES"); + + //if we don't have a RELEASES asset then the previous release likely wasn't a Squirrel one. + if (releaseAsset == null) return; + + Console.WriteLine($"Last GitHub release was {lastRelease.Name}."); + + bool requireDownload = false; + + if (!File.Exists(Path.Combine(ReleasesFolder, nupkgDistroFilename(lastRelease.Name)))) + { + Console.WriteLine($"! Last verion's package not found locally."); + requireDownload = true; + } + else + { + var lastReleases = new RawFileWebRequest($"{GitHubApiEndpoint}/assets/{releaseAsset.Id}"); + lastReleases.AuthenticatedBlockingPerform(); + if (File.ReadAllText(Path.Combine(ReleasesFolder, "RELEASES")) != lastReleases.ResponseString) + { + Console.WriteLine("! Server's RELEASES differed from ours."); + requireDownload = true; + } + } + + if (!requireDownload) return; + + Console.WriteLine("Refreshing local releases directory..."); + refreshDirectory(ReleasesFolder); + + foreach (var a in assets) + { + Console.WriteLine($"- Downloading {a.Name}..."); + new FileWebRequest(Path.Combine(ReleasesFolder, a.Name), $"{GitHubApiEndpoint}/assets/{a.Id}").AuthenticatedBlockingPerform(); + } + } + + private static void refreshDirectory(string directory) + { + if (Directory.Exists(directory)) + Directory.Delete(directory, true); + Directory.CreateDirectory(directory); } private static void updateAssemblyInfo(string version) @@ -176,7 +297,7 @@ namespace osu.Desktop.Deploy } /// - /// Find the base path of the osu! solution (git checkout location) + /// Find the base path of the active solution (git checkout location) /// private static void findSolutionPath() { @@ -185,7 +306,7 @@ namespace osu.Desktop.Deploy if (string.IsNullOrEmpty(path)) path = Environment.CurrentDirectory; - while (!File.Exists(path + "\\osu.sln")) + while (!File.Exists(Path.Combine(path, $"{SolutionName}.sln"))) path = path.Remove(path.LastIndexOf('\\')); path += "\\"; @@ -220,6 +341,26 @@ namespace osu.Desktop.Deploy Console.ReadLine(); Environment.Exit(-1); } + + public static void AuthenticatedBlockingPerform(this WebRequest r) + { + r.AddHeader("Authorization", $"token {GitHubAccessToken}"); + r.BlockingPerform(); + } + } + + internal class RawFileWebRequest : WebRequest + { + public RawFileWebRequest(string url) : base(url) + { + } + + protected override HttpWebRequest CreateWebRequest(string requestString = null) + { + var req = base.CreateWebRequest(requestString); + req.Accept = "application/octet-stream"; + return req; + } } internal class ReleaseLine diff --git a/osu.Desktop.Deploy/osu.Desktop.Deploy.csproj b/osu.Desktop.Deploy/osu.Desktop.Deploy.csproj index 2660473ef8..122c2ec0d6 100644 --- a/osu.Desktop.Deploy/osu.Desktop.Deploy.csproj +++ b/osu.Desktop.Deploy/osu.Desktop.Deploy.csproj @@ -85,6 +85,7 @@ True + @@ -95,6 +96,7 @@ +