Skip to content

Commit a258f36

Browse files
committed
(build) added PublishDocs task
1 parent dc28554 commit a258f36

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

build/.run/Publish Docs.run.xml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<component name="ProjectRunConfigurationManager">
2+
<configuration default="false" name="Publish Docs" type="DotNetProject" factoryName=".NET Project" folderName="Docs">
3+
<option name="EXE_PATH" value="$PROJECT_DIR$/../run/docs.exe" />
4+
<option name="PROGRAM_PARAMETERS" value="--target=PublishDocs" />
5+
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/.." />
6+
<option name="PASS_PARENT_ENVS" value="1" />
7+
<option name="USE_EXTERNAL_CONSOLE" value="0" />
8+
<option name="USE_MONO" value="0" />
9+
<option name="RUNTIME_ARGUMENTS" value="" />
10+
<option name="PROJECT_PATH" value="$PROJECT_DIR$/docs/docs.csproj" />
11+
<option name="PROJECT_EXE_PATH_TRACKING" value="1" />
12+
<option name="PROJECT_ARGUMENTS_TRACKING" value="1" />
13+
<option name="PROJECT_WORKING_DIRECTORY_TRACKING" value="0" />
14+
<option name="PROJECT_KIND" value="DotNetCore" />
15+
<option name="PROJECT_TFM" value="net5.0" />
16+
<method v="2">
17+
<option name="Build" />
18+
</method>
19+
</configuration>
20+
</component>

build/docs/Tasks/PublishDocs.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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

Comments
 (0)