-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathinvoke-docker-build.ps1
More file actions
87 lines (73 loc) · 2.82 KB
/
invoke-docker-build.ps1
File metadata and controls
87 lines (73 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
[CmdletBinding()]
Param(
[Parameter(Mandatory=$false,Position=0)]
[string]$ImageType = "alpine",
[Parameter(Mandatory=$false,Position=1)]
[string]$Version,
[Parameter(Mandatory=$false)]
[switch]$Rebuild
)
if (-not $PSBoundParameters.ContainsKey("ErrorAction")) { $ErrorActionPreference = "Stop" }
if (-not $PSBoundParameters.ContainsKey("Verbose")) { $VerbosePreference = $PSCmdlet.GetVariableValue("VerbosePreference") }
Import-Module -Name "$PSScriptRoot\docker\docker-include.psm1" -Scope Local -Force
$ImageType = $ImageType.ToLower()
$SafeguardDockerFile = (Get-SafeguardDockerFile $ImageType)
Write-Host $SafeguardDockerFile
if (-not (Get-Command "docker" -EA SilentlyContinue))
{
throw "Unable to find docker command. Is docker installed on this machine?"
}
if (-not (Get-Command "dotnet" -EA SilentlyContinue))
{
throw "This script requires dotnet cli for building the service"
}
if (-not (Get-Command "npm" -EA SilentlyContinue))
{
throw "This script requires npm for building the UI"
}
if ($Version)
{
$Version = "$Version-"
}
$ImageName = "oneidentity/safeguard-devops:$Version$ImageType"
try
{
Push-Location $PSScriptRoot
if ($Rebuild)
{
Write-Host -ForegroundColor Yellow "Cleaning up all build directories ..."
(Get-ChildItem -Recurse -Filter obj -EA SilentlyContinue) | Where-Object { $_.FullName -inotmatch "node_modules" } | ForEach-Object { Remove-Item -Recurse -Force $_.FullName }
(Get-ChildItem -Recurse -Filter bin -EA SilentlyContinue) | Where-Object { $_.FullName -inotmatch "node_modules" } | ForEach-Object { Remove-Item -Recurse -Force $_.FullName }
}
Write-Host -ForegroundColor Yellow "Building for full-size Linux distros ..."
dotnet publish -v d -r linux-x64 -c Release --self-contained --force /p:PublishSingleFile=true SafeguardDevOpsService/SafeguardDevOpsService.csproj
if ($LASTEXITCODE -ne 0)
{
throw "dotnet publish command failed"
}
Write-Host -ForegroundColor Yellow "Building for tiny Linux distros ..."
dotnet publish -v d -r linux-musl-x64 -c Release --self-contained --force /p:PublishSingleFile=true SafeguardDevOpsService/SafeguardDevOpsService.csproj
if ($LASTEXITCODE -ne 0)
{
throw "dotnet publish command failed"
}
if (Invoke-Expression "docker images -q $ImageName")
{
Write-Host -ForegroundColor Yellow "Cleaning up the old image: $ImageName ..."
& docker rmi --force "$ImageName"
if ($LASTEXITCODE -ne 0)
{
throw "docker command failed"
}
}
Write-Host -ForegroundColor Yellow "Building a new image: $ImageName ..."
& docker build --no-cache -t "$ImageName" -f "$SafeguardDockerFile" "$PSScriptRoot"
if ($LASTEXITCODE -ne 0)
{
throw "docker command failed"
}
}
finally
{
Pop-Location
}