/// <summary> /// Download assets from a previous release into the releases folder. /// </summary> /// <param name="release"></param> private static void GetAssetsFromRelease(GitHubRelease release) { if (!CanGitHub) { return; } //there's a previous release for this project. var assetReq = new JsonWebRequest <List <GitHubObject> >($"{GitHubApiEndpoint}releases/{release.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; } bool requireDownload = false; if (!File.Exists(Path.Combine(releases_folder, $"{PackageName}-{release.Name}-full.nupkg"))) { Write("Last version's package not found locally.", ConsoleColor.Red); requireDownload = true; } else { var lastReleases = new RawFileWebRequest($"{GitHubApiEndpoint}releases/assets/{releaseAsset.Id}"); lastReleases.AuthenticatedBlockingPerform(); if (File.ReadAllText(Path.Combine(releases_folder, "RELEASES")) != lastReleases.ResponseString) { Write("Server's RELEASES differed from ours.", ConsoleColor.Red); requireDownload = true; } } if (!requireDownload) { return; } Write("Refreshing local releases directory..."); RefreshDirectory(releases_folder); foreach (var a in assets) { if (a.Name.EndsWith(".exe") || a.Name.EndsWith(".app.zip")) { continue; } Write($"- Downloading {a.Name}...", ConsoleColor.Yellow); new FileWebRequest(Path.Combine(releases_folder, a.Name), $"{GitHubApiEndpoint}releases/assets/{a.Id}").AuthenticatedBlockingPerform(); } }
public static int Main(string[] args) { interactive = args[0] == "1"; FindSolutionPath(); if (!Directory.Exists(releases_folder)) { Write("WARNING: No release directory found. Make sure you want this!", ConsoleColor.Yellow); Directory.CreateDirectory(releases_folder); } GitHubRelease lastRelease = null; if (CanGitHub) { Write("Checking GitHub releases..."); lastRelease = GetLastGithubRelease(); if (lastRelease == null) { Write("This is the first GitHub release"); } else { Write($"Last GitHub release was {lastRelease.Name}."); if (lastRelease.Draft) { Write("WARNING: This is a pending draft release! You might not want to push a build with this present.", ConsoleColor.Red); } } } string version; if (args.Length > 1 && !string.IsNullOrEmpty(args[1])) { version = args[1]; } else { Write("Unable to find version number"); return(-1); } Console.ResetColor(); Console.WriteLine($"Increment Version: {IncrementVersion}"); Console.WriteLine($"Signing Certificate: {CodeSigningCertificate}"); Console.WriteLine($"Upload to GitHub: {GitHubUpload}"); Console.WriteLine($"Upload to Nuget: {NuGetUpload}"); Console.WriteLine($"Announce to Discord: {AnnounceDiscord}"); Console.WriteLine(); Console.Write($"Ready to deploy {version}!"); PauseIfInteractive(); stopwatch.Start(); RefreshDirectory(staging_folder); Write("Running build process..."); switch (RuntimeInfo.OS) { case RuntimeInfo.Platform.Windows: if (lastRelease != null) { GetAssetsFromRelease(lastRelease); } RunCommand("dotnet", $"build {ProjectName} -o {StagingPath} --configuration Release /p:Version={version}"); string nupkgFilename = $"{PackageName}.{version}.nupkg"; // upload to NuGet if (NuGetUpload) { Write("Uploading to NuGet..."); Write($"Running dotnet nuget push {nupkgFilename} -k {NugetPublishToken} -s https://api.nuget.org/v3/index.json..."); var psi = new ProcessStartInfo("dotnet", $"nuget push {nupkgFilename} -k {NugetPublishToken} -s https://api.nuget.org/v3/index.json") { WorkingDirectory = Path.Combine(Directory.GetCurrentDirectory(), @"staging"), CreateNoWindow = true, RedirectStandardOutput = true, RedirectStandardError = true, UseShellExecute = false, WindowStyle = ProcessWindowStyle.Hidden }; Process p = Process.Start(psi); if (p == null) { throw new NullReferenceException(); } string output = p.StandardOutput.ReadToEnd(); output += p.StandardError.ReadToEnd(); p.WaitForExit(); if (p.ExitCode != 0) { Write(output); Error($"Command dotnet nuget push {nupkgFilename} -k {NugetPublishToken} -s https://api.nuget.org/v3/index.json failed!"); } } break; } if (GitHubUpload) { UploadBuild(version); } AnnounceToDiscord(version); Write("Done!"); PauseIfInteractive(); return(0); }
private static void UploadBuild(string version) { if (!CanGitHub) { return; } Write("Publishing to GitHub..."); var req = new JsonWebRequest <GitHubRelease>($"{GitHubApiEndpoint}releases") { Method = HttpMethod.Post, }; GitHubRelease targetRelease = GetLastGithubRelease(); if (targetRelease == null || targetRelease.TagName != version) { Write($"- Creating release {version}...", ConsoleColor.Yellow); // Get all of the previous PR's name string body = $"Version {version} for {GitHubRepoName} has been released! this now fixes and adds:\r\n"; var requests = GetPullRequests(); // Adds every pull requests after the last release requests.ForEach(pr => { body += $"- {pr.Title} | [{pr.User.Name}]({pr.User.Link})\r\n"; }); req.AddRaw(JsonConvert.SerializeObject(new GitHubRelease { Name = $"{GitHubRepoName} v{version}", TagName = version, Description = body, Draft = true, PublishedAt = DateTime.Now })); req.AuthenticatedBlockingPerform(); targetRelease = req.ResponseObject; } else { Write($"- Adding to existing release {version}...", ConsoleColor.Yellow); } var assetUploadUrl = targetRelease.UploadUrl.Replace("{?name,label}", "?name={0}"); foreach (var a in Directory.GetFiles(staging_folder).Reverse()) { if (Path.GetFileName(a).StartsWith('.')) { continue; } if (!Path.GetFileName(a).EndsWith(".nupkg")) { continue; } var upload = new WebRequest(assetUploadUrl, Path.GetFileName(a)) { Method = HttpMethod.Post, Timeout = 240000, ContentType = "application/octet-stream", }; upload.AddRaw(File.ReadAllBytes(a)); upload.AuthenticatedBlockingPerform(); } OpenGitHubReleasePage(); }