Skip to content

Commit

Permalink
ci: fix nightly-build version
Browse files Browse the repository at this point in the history
  • Loading branch information
moomiji committed Oct 10, 2024
1 parent 11db8b8 commit 7ef425f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ jobs:
dotnet tool update -g docfx
- id: build
env:
GH_TOKEN: ${{ github.token }}
run: |
dotnet script ./tools/Builder/Build.csx
./docs/build.ps1
Expand Down
21 changes: 15 additions & 6 deletions tools/Builder/Build.csx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#r "nuget: NuGet.Versioning, 6.11.0"

using System.Diagnostics;
using System.Xml.Linq;
using NuGet.Versioning;

var GITHUB_OUTPUT = Environment.GetEnvironmentVariable("GITHUB_OUTPUT")
Expand All @@ -11,10 +12,7 @@ var GITHUB_WORKFLOW = Environment.GetEnvironmentVariable("GITHUB_WORKFLOW")
?? throw new InvalidOperationException("GITHUB_WORKFLOW is null.");
var GITHUB_REF = Environment.GetEnvironmentVariable("GITHUB_REF")
?? string.Empty;
var d = DateTimeOffset.Parse(StartProcess($"gh run view {GITHUB_RUN_ID} --json createdAt --jq .createdAt")).ToOffset(TimeSpan.FromHours(8));

var dateTime = ((d.Year - 2000) * 1000 + d.Month * 50 + d.Day).ToString("D5");
var todayBuildTimes = StartProcess($"gh run list --workflow {GITHUB_WORKFLOW} --created {d.ToString("yyyy-MM-ddT00:00:00+08:00")}..{d.ToString("yyyy-MM-ddT23:59:59+08:00")} --limit 99 --json createdAt --jq length");
var defaultBranch = StartProcess("gh repo view --json defaultBranchRef --jq .defaultBranchRef.name");
var branch = StartProcess("git rev-parse --abbrev-ref HEAD");
var commit = StartProcess("git rev-parse HEAD");
Expand All @@ -32,7 +30,16 @@ var version = tags.Count switch

if (tags.Count is 3 or 4) // 非最新版本号
{
version = new NuGetVersion(version.Major, version.Minor, version.Patch,
var d = DateTimeOffset.Parse(StartProcess($"gh run view {GITHUB_RUN_ID} --json createdAt --jq .createdAt")).ToOffset(TimeSpan.FromHours(8));
var dateTime = ((d.Year - 2000) * 1000 + d.Month * 50 + d.Day).ToString("D5");
var todayBuildTimes = StartProcess($"gh run list --workflow {GITHUB_WORKFLOW} --created {d.ToString("yyyy-MM-ddT00:00:00+08:00")}..{d.ToString("yyyy-MM-ddT23:59:59+08:00")} --limit 99 --json createdAt --jq length");
var runtimes = NuGetVersion.Parse(
XDocument.Load("Directory.Packages.props")
.Descendants("PackageVersion")
.FirstOrDefault(e => e.Attribute("Include")?.Value == "Maa.Framework.Runtimes")
!.Attribute("Version")
!.Value);
version = new NuGetVersion(runtimes.Major, runtimes.Minor, runtimes.Patch,
["preview", dateTime, todayBuildTimes],
tag);
}
Expand All @@ -54,10 +61,12 @@ MoveNupkgFiles("nupkgs");
#region Methods
void TeeToGithubOutput(params string[] outputs)
{
Console.ForegroundColor = ConsoleColor.Green;
foreach (var output in outputs)
{
Console.WriteLine(output);
Console.WriteLine("|> {0}", output);
}
Console.ResetColor();
File.AppendAllLines(GITHUB_OUTPUT, outputs);
}
string StartProcess(string cmd, bool redirectStandardOutputToReturn = true)
Expand All @@ -74,7 +83,7 @@ string StartProcess(string cmd, bool redirectStandardOutputToReturn = true)
if (p.ExitCode != 0)
throw new InvalidOperationException($"ExitCode is {p.ExitCode} from {cmd}.");
var output = redirectStandardOutputToReturn ? p.StandardOutput.ReadToEnd().Trim() : string.Empty;
Console.WriteLine(output);
Console.WriteLine(" {0}", output);
return output;
}
void MoveNupkgFiles(string destDir)
Expand Down
10 changes: 10 additions & 0 deletions tools/Builder/test.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
cd ../../

$env:GITHUB_RUN_ID = "11165266892"
$env:GITHUB_WORKFLOW = "ci"
$env:GITHUB_OUTPUT = New-TemporaryFile
dotnet script ./tools/Builder/Build.csx

ls nupkgs
rm $env:GITHUB_OUTPUT
rmdir nupkgs

0 comments on commit 7ef425f

Please sign in to comment.