Skip to content

Commit 73023c2

Browse files
committed
Fix CI cancellation issues
1 parent 4a4b292 commit 73023c2

File tree

1 file changed

+43
-3
lines changed

1 file changed

+43
-3
lines changed

build/build.fs

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,30 @@ open System.IO
1313
open Fake.BuildServer
1414
open FsToolkit.Build
1515

16+
let environVarAsBoolOrDefault varName defaultValue =
17+
let truthyConsts = [
18+
"1"
19+
"Y"
20+
"YES"
21+
"T"
22+
"TRUE"
23+
]
24+
25+
try
26+
let envvar = (Environment.environVar varName).ToUpper()
27+
28+
truthyConsts
29+
|> List.exists ((=) envvar)
30+
with _ ->
31+
defaultValue
32+
1633
let project = "FsToolkit.ErrorHandling"
1734

1835
let summary =
1936
"FsToolkit.ErrorHandling is a utility library to work with the Result type in F#, and allows you to do clear, simple and powerful error handling."
2037

38+
let isCI = lazy (environVarAsBoolOrDefault "CI" false)
39+
2140
let isRelease (targets: Target list) =
2241
targets
2342
|> Seq.map (fun t -> t.Name)
@@ -93,6 +112,18 @@ let failOnBadExitAndPrint (p: ProcessResult) =
93112

94113
failwithf "failed with exitcode %d" p.ExitCode
95114

115+
// github actions are terrible and will cancel runner operations if using too much CPU
116+
// https://github.com/actions/runner-images/discussions/7188#discussioncomment-6672934
117+
let maxCpuCount = lazy (if isCI.Value then Some(Some 2) else None)
118+
119+
/// MaxCpu not used on unix https://github.com/fsprojects/FAKE/blob/82e38df01e4b31e5daa3623abff57e6462430395/src/app/Fake.DotNet.MSBuild/MSBuild.fs#L858-L861
120+
let maxCpuMsBuild =
121+
lazy
122+
(match maxCpuCount.Value with
123+
| None -> ""
124+
| Some None -> "/m"
125+
| Some(Some x) -> $"/m:%d{x}")
126+
96127
module dotnet =
97128
let watch cmdParam program args =
98129
DotNet.exec cmdParam (sprintf "watch %s" program) args
@@ -144,10 +175,16 @@ let clean _ =
144175

145176

146177
let build ctx =
147-
let setParams (defaults: DotNet.BuildOptions) = {
148-
defaults with
178+
179+
let args = [ maxCpuMsBuild.Value ]
180+
181+
let setParams (c: DotNet.BuildOptions) = {
182+
c with
149183
NoRestore = true
150184
Configuration = (configuration ctx.Context.AllExecutingTargets)
185+
Common =
186+
c.Common
187+
|> DotNet.Options.withAdditionalArgs args
151188
}
152189

153190
DotNet.build setParams solutionFile
@@ -166,7 +203,10 @@ let npmRestore _ = Npm.install id
166203

167204
let dotnetTest ctx =
168205

169-
let args = [ "--no-build" ]
206+
let args = [
207+
"--no-build"
208+
maxCpuMsBuild.Value
209+
]
170210

171211
DotNet.test
172212
(fun c ->

0 commit comments

Comments
 (0)