Skip to content

Commit 56a58d8

Browse files
committed
Add docker.ps1
Create `docker.ps1` to provide a Windows script for building and publishing, matching the functionality of `docker.sh` on Linux. Signed-off-by: bashir <mohammadbashir266@gmail.com>
1 parent 10c4133 commit 56a58d8

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

docker.ps1

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
param(
2+
[Parameter(Mandatory=$false)]
3+
[string]$Command
4+
)
5+
$ErrorActionPreference = "Stop"
6+
Get-Content docker.env | Where-Object { $_ -match '^([^#][^=]+)=(.+)$' } | ForEach-Object {
7+
if ($matches[2] -match '\$\((.*)\)') {
8+
$cmdOutput = Invoke-Expression $matches[1]
9+
Set-Variable -Name $matches[1].Trim() -Value $cmdOutput -Scope Script
10+
} else {
11+
Set-Variable -Name $matches[1].Trim() -Value $matches[2].Trim() -Scope Script
12+
}
13+
}
14+
$ARCH = "x86_64" # Explicitly set the architecture
15+
16+
function Next-Version {
17+
return (git show -s --format=%h)
18+
}
19+
20+
function Get-FullVersion {
21+
$version = Next-Version
22+
return "${IMAGE_NAME}:g${version}"
23+
}
24+
25+
function Print-NextVersion {
26+
Write-Output (Get-FullVersion)
27+
}
28+
29+
function Print-Registry {
30+
Write-Output $REGISTRY
31+
}
32+
33+
function Print-ImageName {
34+
Write-Output $IMAGE_NAME
35+
}
36+
37+
function Build-Tag {
38+
return "$(Get-FullVersion)_$ARCH"
39+
}
40+
41+
function Check-ExitCode {
42+
param (
43+
[int]$ExitCode,
44+
[string]$Message
45+
)
46+
47+
if ($ExitCode -ne 0) {
48+
Write-Error $Message
49+
exit $ExitCode
50+
}
51+
}
52+
53+
function Build-Container {
54+
# Check if running in Linux or Windows container mode
55+
$containerInfo = docker version --format '{{.Server.Os}}'
56+
$dockerfile = if ($containerInfo -eq "linux") {
57+
"Dockerfile"
58+
} else {
59+
"Dockerfile.windows.x86_64"
60+
}
61+
62+
# Build the container and check for failures
63+
$tag = Build-Tag
64+
Write-Host "Building using $dockerfile in $containerInfo container mode..."
65+
66+
docker build -t $tag `
67+
--build-arg GIT_BRANCH=$GIT_BRANCH `
68+
--build-arg GIT_COMMIT=$GIT_COMMIT `
69+
--build-arg RUST_TOOLCHAIN=$RUST_TOOLCHAIN `
70+
-f $dockerfile .
71+
Check-ExitCode $LASTEXITCODE "Build failed with exit code $LASTEXITCODE"
72+
Write-Host "Build completed for $tag"
73+
}
74+
75+
function Publish-Container {
76+
$tag = Build-Tag
77+
Write-Host "Publishing $tag to dockerhub"
78+
79+
# Check if image exists locally
80+
$imageExists = docker image inspect $tag 2>$null
81+
Check-ExitCode $LASTEXITCODE "Image $tag not found locally. Please run 'build' first."
82+
83+
# Attempt to push
84+
docker push $tag
85+
Check-ExitCode $LASTEXITCODE "Failed to publish $tag"
86+
Write-Host "Successfully published $tag"
87+
}
88+
89+
# Handle commands
90+
switch ($Command) {
91+
"build" { Build-Container }
92+
"publish" { Publish-Container }
93+
"print-registry" { Print-Registry }
94+
"print-image-name" { Print-ImageName }
95+
"print-next-version" { Print-NextVersion }
96+
default {
97+
Write-Host "Command $Command not supported. Try with 'publish', 'build', or 'print-next-version'."
98+
exit 1
99+
}
100+
}

0 commit comments

Comments
 (0)