Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 53 additions & 29 deletions Build.fs
Original file line number Diff line number Diff line change
Expand Up @@ -293,36 +293,56 @@ let dockerImage =
| image -> image


Target.create
"DockerBuild"
(fun _ ->
let version =
System.Xml.Linq.XDocument.Load("Directory.Build.props").Descendants(System.Xml.Linq.XName.Get "Version")
|> Seq.map (fun e -> e.Value)
|> Seq.tryHead
|> Option.defaultWith (fun () -> failwith "Directory.Build.props: <Version> element not found")

// Cross-build for a different target platform, e.g. amd64 from Apple
// Silicon, via: DOCKER_PLATFORM=linux/amd64 dotnet run DockerBuild
let platformArgs =
match System.Environment.GetEnvironmentVariable "DOCKER_PLATFORM" with
| null
| "" -> []
| platform -> [ "--platform"; platform ]
let buildDockerImage () =
let version =
System.Xml.Linq.XDocument.Load("Directory.Build.props").Descendants(System.Xml.Linq.XName.Get "Version")
|> Seq.tryHead
|> Option.map (fun e -> e.Value.Trim())
|> Option.filter (System.String.IsNullOrWhiteSpace >> not)
|> Option.defaultWith (fun () -> failwith "Directory.Build.props: <Version> element is missing or empty.")

Comment thread
7sharp9 marked this conversation as resolved.
// Cross-build for a different target platform, e.g. amd64 from Apple
// Silicon, via: DOCKER_PLATFORM=linux/amd64 dotnet run DockerBuild
let platformArgs =
match System.Environment.GetEnvironmentVariable "DOCKER_PLATFORM" with
| null
| "" -> []
| platform -> [ "--platform"; platform ]

run
docker
([ "build" ]
@ platformArgs
@ [
"--build-arg"
$"APP_VERSION={version}"
"-t"
dockerImage
"."
])
"."


// `docker` wraps CreateProcess with addOnExited, which raises on any non-zero exit.
// This is unusable here since "no such image" is an expected outcome we need to branch on, not a build failure.
let dockerImageExistsLocally () =
let result =
CreateProcess.fromRawCommand "docker" [ "image"; "inspect"; dockerImage ]
|> CreateProcess.redirectOutput
|> Proc.run

if result.ExitCode = 0 then
true
// Only "no such image" means missing. Any other failure (daemon down, permission
// denied, wrong context) is a real Docker problem, not something a build can fix,
// so surface it immediately instead of letting it masquerade as a routine first build.
elif result.Result.Error.Contains "No such image" then
false
else
failwithf "docker image inspect failed:\n%s" result.Result.Error

run
docker
([ "build" ]
@ platformArgs
@ [
"--build-arg"
$"APP_VERSION={version}"
"-t"
dockerImage
"."
])
"."
)

Target.create "DockerBuild" (fun _ -> buildDockerImage ())


Target.create
Expand All @@ -335,6 +355,10 @@ Target.create
requireEnvVar "GENPRES_URL_ID" |> ignore
requireEnvVar "GENPRES_PASSWORD" |> ignore

if dockerImageExistsLocally () |> not then
Trace.traceImportant $"Docker image '{dockerImage}' not found locally, building it..."
buildDockerImage ()

run
docker
[
Expand Down