master
ps1 48 lines 1.67 KB
Raw
1 <#
2 .SYNOPSIS
3 Builds a custom test registry image using wslc and saves it as a .tar file.
4 .DESCRIPTION
5 This script builds a custom image using wslc from a specified Dockerfile and saves the resulting image as a .tar file.
6 This is useful for preparing test images for WSL container tests.
7 .PARAMETER DockerfileDir
8 Path to the directory containing the Dockerfile to build.
9 .PARAMETER ImageTag
10 Tag for the built image.
11 .PARAMETER OutputFile
12 Path to save the exported .tar file. Defaults to <DockerfileDir name>.tar in the current directory.
13 #>
14
15 [CmdletBinding(SupportsShouldProcess)]
16 param (
17 [string]$DockerfileDir,
18 [string]$ImageTag,
19 [string]$OutputFile = ""
20 )
21
22 $ErrorActionPreference = "Stop"
23 Set-StrictMode -Version Latest
24
25 if ($OutputFile -eq "") {
26 $OutputFile = Join-Path $PWD "$(Split-Path -Leaf $DockerfileDir).tar"
27 }
28
29 # Verify $OutputFile is a valid path, we can write to it, and that it has a .tar extension
30 if ([System.IO.Path]::GetExtension($OutputFile) -ne ".tar") {
31 if (-not $PSCmdlet.ShouldContinue("Are you sure you want to continue?", "Output file '$OutputFile' is not a .tar file.")) {
32 throw "Aborting due to invalid output file extension."
33 }
34 }
35
36
37 if ($PSCmdlet.ShouldProcess($ImageTag, "Build image from '$DockerfileDir'")) {
38 & wslc build -t $ImageTag $DockerfileDir
39 if ($LASTEXITCODE -ne 0) { throw "wslc build failed with exit code $LASTEXITCODE" }
40 }
41
42 if ($PSCmdlet.ShouldProcess($OutputFile, "Save image '$ImageTag'")) {
43 & wslc save --output $OutputFile $ImageTag
44 if ($LASTEXITCODE -ne 0) { throw "wslc save failed with exit code $LASTEXITCODE" }
45
46 Write-Host "Image built and saved to $OutputFile successfully."
47 }
48