forked from dotnet/dotnet-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-samples.ps1
75 lines (58 loc) · 1.85 KB
/
test-samples.ps1
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
#!/usr/bin/env pwsh
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Stop'
function Log {
param ([string] $Message)
Write-Output "###### $Message"
}
function Exec {
param ([string] $Cmd)
Log "$Cmd"
Invoke-Expression $Cmd
if ($LASTEXITCODE -ne 0) {
throw "Failed: $Cmd"
}
}
function Get-DockerfileItem {
param ([string] $SampleType)
$exclusions = [System.Collections.ArrayList]@()
$dockerOS = docker version -f "{{ .Server.Os }}"
if ($dockerOS -eq "windows") {
$null = $exclusions.Add("*debian*")
$null = $exclusions.Add("*alpine*")
}
else {
$null = $exclusions.Add("*nanoserver*")
}
$dockerArch = docker info -f "{{ .Architecture }}"
if ($dockerArch -eq "x86_64") {
$null = $exclusions.Add("*arm*")
}
else {
$null = $exclusions.Add("*x64*")
}
$dockerfilePath = [io.path]::combine($PSScriptRoot, $SampleType, "Dockerfile*")
Get-ChildItem -Path $dockerfilePath -Exclude $exclusions
}
function Validate_Dockerfile {
param ([string] $Dockerfile, [string] $Stage = $null, [switch] $SkipRunning)
Log "Validating Dockerfile: $Dockerfile"
$tag = "test" + [DateTime]::Now.ToString("yyyyMMdd-HHmmss")
$buildContext = [System.IO.Path]::GetDirectoryName($Dockerfile)
$optionalBuildArgs = ""
if ($Stage) {
$optionalBuildArgs += "--target $Stage"
}
Exec "docker build --pull -t $tag -f $DockerFile $optionalBuildArgs $buildContext"
if (!$SkipRunning) {
Exec "docker run --rm -it $tag"
}
docker rmi -f $tag
}
# test dotnetapp samples
Get-DockerfileItem "dotnetapp" | ForEach-Object {
Validate_Dockerfile $_.FullName
Validate_Dockerfile $_.FullName "testrunner"
}
# test aspnetapp samples
Get-DockerfileItem "aspnetapp" | ForEach-Object { Validate_Dockerfile $_.FullName -SkipRunning }