Skip to content
Merged
Changes from 1 commit
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
74 changes: 44 additions & 30 deletions Build.fs
Original file line number Diff line number Diff line change
Expand Up @@ -293,36 +293,46 @@ 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 ]

run
docker
([ "build" ]
@ platformArgs
@ [
"--build-arg"
$"APP_VERSION={version}"
"-t"
dockerImage
"."
])
"."
)
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 () =
CreateProcess.fromRawCommand "docker" [ "image"; "inspect"; dockerImage ]
|> CreateProcess.redirectOutput
|> Proc.run
|> fun result -> result.ExitCode = 0
Comment thread
7sharp9 marked this conversation as resolved.
Outdated
Comment thread
7sharp9 marked this conversation as resolved.
Outdated


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


Target.create
Expand All @@ -335,6 +345,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