|
| 1 | +using System; |
| 2 | +using Cake.Common.Diagnostics; |
| 3 | +using Cake.Common.IO; |
| 4 | +using Cake.Core; |
| 5 | +using Cake.Core.IO; |
| 6 | +using Cake.Frosting; |
| 7 | +using Cake.Git; |
| 8 | +using Cake.Kudu; |
| 9 | +using Cake.Kudu.KuduSync; |
| 10 | +using Common.Utilities; |
| 11 | + |
| 12 | +namespace Docs.Tasks |
| 13 | +{ |
| 14 | + [TaskName(nameof(PublishDocs))] |
| 15 | + [TaskDescription("Published the docs changes to docs specific branch")] |
| 16 | + [IsDependentOn(typeof(BuildDocs))] |
| 17 | + public sealed class PublishDocs : FrostingTask<BuildContext> |
| 18 | + { |
| 19 | + public override bool ShouldRun(BuildContext context) |
| 20 | + { |
| 21 | + var shouldRun = true; |
| 22 | + shouldRun &= context.ShouldRun(context.DirectoryExists(Paths.Docs), "Wyam documentation directory is missing"); |
| 23 | + |
| 24 | + return shouldRun; |
| 25 | + } |
| 26 | + |
| 27 | + public override void Run(BuildContext context) |
| 28 | + { |
| 29 | + PublishDocumentation(context); |
| 30 | + } |
| 31 | + |
| 32 | + private static void PublishDocumentation(BuildContext context) |
| 33 | + { |
| 34 | + const string publishBranchName = "gh-pages"; |
| 35 | + var sourceCommit = context.GitLogTip("./"); |
| 36 | + |
| 37 | + var publishFolder = context.MakeAbsolute(Paths.ArtifactsDocs.Combine("_published").Combine(DateTime.Now.ToString("yyyyMMdd_HHmmss"))); |
| 38 | + context.Information("Publishing Folder: {0}", publishFolder); |
| 39 | + context.Information("Getting publish branch..."); |
| 40 | + context.GitClone("https://github.com/gittools/GitVersion", publishFolder, new GitCloneSettings |
| 41 | + { |
| 42 | + BranchName = publishBranchName |
| 43 | + }); |
| 44 | + |
| 45 | + context.Information("Sync output files..."); |
| 46 | + context.Kudu().Sync(context.MakeAbsolute(Paths.ArtifactsDocs.Combine("preview")), publishFolder, new KuduSyncSettings |
| 47 | + { |
| 48 | + ArgumentCustomization = args => args.Append("--ignore").AppendQuoted(".git;CNAME;_git2") |
| 49 | + }); |
| 50 | + |
| 51 | + if (context.GitHasUncommitedChanges(publishFolder)) |
| 52 | + { |
| 53 | + context.Information("Stage all changes..."); |
| 54 | + context.GitAddAll(publishFolder); |
| 55 | + |
| 56 | + if (context.GitHasStagedChanges(publishFolder)) |
| 57 | + { |
| 58 | + context.Information("Commit all changes..."); |
| 59 | + context.GitCommit( |
| 60 | + publishFolder, |
| 61 | + sourceCommit.Committer.Name, |
| 62 | + sourceCommit.Committer.Email, |
| 63 | + $"Continuous Integration Publish: {sourceCommit.Sha}\r\n{sourceCommit.Message}" |
| 64 | + ); |
| 65 | + |
| 66 | + PublishToGitHub(context, publishFolder, publishBranchName); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + private static void PublishToGitHub(BuildContext context, DirectoryPath publishFolder, string publishBranchName) |
| 72 | + { |
| 73 | + var username = context.Credentials?.GitHub?.UserName; |
| 74 | + if (string.IsNullOrEmpty(username)) |
| 75 | + { |
| 76 | + throw new InvalidOperationException("Could not resolve Github username."); |
| 77 | + } |
| 78 | + |
| 79 | + var token = context.Credentials?.GitHub?.Token; |
| 80 | + if (string.IsNullOrEmpty(token)) |
| 81 | + { |
| 82 | + throw new InvalidOperationException("Could not resolve Github token."); |
| 83 | + } |
| 84 | + |
| 85 | + context.Information("Pushing all changes..."); |
| 86 | + context.GitPush(publishFolder, username, token, publishBranchName); |
| 87 | + } |
| 88 | + } |
| 89 | +} |
0 commit comments