Skip to content

Commit f39c918

Browse files
authored
refactor build script with C# script (#26)
* update example cover basic crud * build: refine build with dotnet-exec build * update build script * fix build script * update version * update version prefix * update build script * update build script
1 parent 938a2a9 commit f39c918

File tree

5 files changed

+141
-19
lines changed

5 files changed

+141
-19
lines changed

.github/workflows/build.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ jobs:
2525
fail-fast: false
2626
matrix:
2727
os: [ubuntu-latest, windows-latest, macos-latest]
28-
config: [Release]
2928
test_tfm: [net8.0, net9.0]
3029

3130
steps:
@@ -38,11 +37,11 @@ jobs:
3837
path: ~/.nuget/packages
3938
key: ${{ runner.os }}-nuget-${{ hashFiles('**/Directory.Build.targets') }}
4039
restore-keys: |
41-
${{ runner.os }}-nuget-
40+
${{ runner.os }}-nuget
4241
4342
- name: Setup .NET Core SDK
4443
uses: actions/[email protected]
4544

4645
- name: Build
47-
run: dotnet build -c ${{ matrix.config }}
46+
run: bash ./build.sh --target=build
4847
shell: bash

.github/workflows/release.yml

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,14 @@ jobs:
2121
8.0.x
2222
9.0.x
2323
24-
- name: Build project
25-
run: dotnet build -c Release
26-
2724
- name: Set Package
2825
if: startsWith(github.ref, 'refs/tags/')
2926
run: |
30-
VERSION_PREFIX=${GITHUB_REF_NAME#v}
31-
echo "VERSION_PREFIX=$VERSION_PREFIX"
32-
dotnet pack ./src/GaussDB/GaussDB.csproj -c Release -o ./artifacts/nupkgs -p VersionPrefix=$VERSION_PREFIX
33-
34-
- name: Set Preview Package
35-
if: github.event_name == 'workflow_dispatch'
36-
run: |
37-
VERSION_SUFFIX=preview-$(date -u +"%Y%m%d%H%M")
38-
echo $VERSION_SUFFIX
39-
dotnet pack ./src/GaussDB/GaussDB.csproj -c Release -o ./artifacts/nupkgs -p VersionSuffix=$VERSION_SUFFIX
40-
41-
- name: Push to nuget.org
27+
VERSION=${GITHUB_REF_NAME#v}
28+
echo "VERSION=$VERSION" >> $GITHUB_ENV
29+
30+
- name: Pack nuget package
4231
run: |
43-
dotnet nuget push "./artifacts/nupkgs/*.nupkg" -s https://api.nuget.org/v3/index.json -k ${{ secrets.NUGET_API_KEY }}
32+
bash ./build.sh
33+
env:
34+
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}

build.cs

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
var target = CommandLineParser.Val(args, "target", "Default");
2+
var apiKey = CommandLineParser.Val(args, "apiKey");
3+
var noPush = CommandLineParser.BooleanVal(args, "noPush");
4+
var version = Environment.GetEnvironmentVariable("VERSION");
5+
var stable = CommandLineParser.BooleanVal(args, "stable") || !string.IsNullOrEmpty(version);
6+
var runningOnGithubActions = Environment.GetEnvironmentVariable("GITHUB_ACTIONS") == "true";
7+
8+
Console.WriteLine($$"""
9+
Arguments:
10+
11+
target: {{target}}
12+
stable: {{stable}}
13+
noPush: {{noPush}}
14+
args:
15+
{{args.StringJoin("\n")}}
16+
17+
""");
18+
19+
var solutionPath = "./GaussDB.slnx";
20+
string[] srcProjects = [
21+
"./src/GaussDB/GaussDB.csproj"
22+
];
23+
string[] testProjects = [
24+
"./test/GaussDB.Tests/GaussDB.Tests.csproj",
25+
"./test/GaussDB.GaussDB.DependencyInjection.Tests/GaussDB.GaussDB.DependencyInjection.Tests.csproj"
26+
];
27+
28+
await new BuildProcessBuilder()
29+
.WithSetup(() =>
30+
{
31+
// cleanup previous artifacts
32+
if (Directory.Exists("./artifacts/packages"))
33+
Directory.Delete("./artifacts/packages", true);
34+
})
35+
.WithTaskExecuting(task => Console.WriteLine($@"===== Task [{task.Name}] {task.Description} executing ======"))
36+
.WithTaskExecuted(task => Console.WriteLine($@"===== Task [{task.Name}] {task.Description} executed ======"))
37+
.WithTask("build", b =>
38+
{
39+
b.WithDescription("build")
40+
.WithExecution(cancellationToken => ExecuteCommandAsync($"dotnet build {solutionPath}", cancellationToken))
41+
;
42+
})
43+
.WithTask("test", b =>
44+
{
45+
b.WithDescription("dotnet test")
46+
.WithDependency("build")
47+
.WithExecution(async cancellationToken =>
48+
{
49+
foreach (var project in testProjects)
50+
{
51+
var loggerOptions = runningOnGithubActions
52+
? "--logger GitHubActions"
53+
: "--logger \"console;verbosity=d\"";
54+
var command = $"dotnet test --blame --collect:\"XPlat Code Coverage;Format=cobertura,opencover;ExcludeByAttribute=ExcludeFromCodeCoverage,Obsolete,GeneratedCode,CompilerGenerated\" {loggerOptions} -v=d {project}";
55+
await ExecuteCommandAsync(command, cancellationToken);
56+
}
57+
})
58+
;
59+
})
60+
.WithTask("pack", b => b
61+
.WithDescription("dotnet pack")
62+
.WithDependency("build")
63+
.WithExecution(async cancellationToken =>
64+
{
65+
var packOptions = " -o ./artifacts/packages";
66+
if (stable)
67+
{
68+
if (!string.IsNullOrEmpty(version))
69+
{
70+
packOptions += $" -p VersionPrefix={version}";
71+
}
72+
}
73+
else
74+
{
75+
var suffix = $"preview-{DateTime.UtcNow:yyyyMMdd-HHmmss}";
76+
packOptions += $" --version-suffix {suffix}";
77+
}
78+
79+
foreach (var project in srcProjects)
80+
{
81+
await ExecuteCommandAsync($"dotnet pack {project} {packOptions}", cancellationToken);
82+
}
83+
84+
if (noPush)
85+
{
86+
Console.WriteLine("Skip push there's noPush specified");
87+
return;
88+
}
89+
90+
if (string.IsNullOrEmpty(apiKey))
91+
{
92+
// try to get apiKey from environment variable
93+
apiKey = Environment.GetEnvironmentVariable("NUGET_API_KEY");
94+
95+
if (string.IsNullOrEmpty(apiKey))
96+
{
97+
Console.WriteLine("Skip push since there's no apiKey found");
98+
return;
99+
}
100+
}
101+
102+
// push nuget packages
103+
foreach (var file in Directory.GetFiles("./artifacts/packages/", "*.nupkg"))
104+
{
105+
await RetryHelper.TryInvokeAsync(() => ExecuteCommandAsync($"dotnet nuget push {file} -s https://api.nuget.org/v3/index.json -k {apiKey} --skip-duplicate", cancellationToken), cancellationToken: cancellationToken);
106+
}
107+
}))
108+
.WithTask("Default", b => b.WithDependency("pack"))
109+
.Build()
110+
.ExecuteAsync(target, ApplicationHelper.ExitToken);
111+
112+
async Task ExecuteCommandAsync(string commandText, CancellationToken cancellationToken = default)
113+
{
114+
Console.WriteLine($"Executing command: \n {commandText}");
115+
Console.WriteLine();
116+
117+
var result = await CommandExecutor.ExecuteCommandAndOutputAsync(commandText, cancellationToken: cancellationToken);
118+
result.EnsureSuccessExitCode();
119+
Console.WriteLine();
120+
}

build.ps1

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
dotnet tool update -g dotnet-execute
2+
3+
Write-Host 'dotnet-exec ./build.cs "--args=$ARGS"' -ForegroundColor GREEN
4+
5+
dotnet-exec ./build/build.cs --args $ARGS

build.sh

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#!/bin/sh
2+
3+
dotnet tool update -g dotnet-execute
4+
export PATH="$PATH:$HOME/.dotnet/tools"
5+
6+
echo "dotnet-exec ./build.cs --args $@"
7+
dotnet-exec ./build.cs --args "$@"

0 commit comments

Comments
 (0)