-
Notifications
You must be signed in to change notification settings - Fork 283
/
build.ps1
114 lines (92 loc) · 3.52 KB
/
build.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/powershell -Command
##################################################
# Main build script, this is used both for local
# development and in continuous integration
# to build and push the images
##################################################
param (
[switch]$Push = $false,
[switch]$RunTests = $false,
[switch]$StartContainers = $false,
[switch]$StartDebugContainers = $false
)
$global:ErrorActionPreference = 'Stop'
. .\helpers.ps1
$TESTDIR = $Env:TESTDIR;
if ([string]::IsNullOrWhiteSpace($TESTDIR)) {
$TESTDIR = Get-Location;
}
# If there is a local environment envsettings
# file, load it. In pipelines, these are all comming
# from environment variables.
if (Test-Path "envsettings.ps1") {
.\envsettings.ps1;
}
# Ensure we are in LINUX containers
if (-not(Test-Path $Env:ProgramFiles\Docker\Docker\DockerCli.exe)) {
Get-Command docker
Write-Warning "Docker cli not found at $Env:ProgramFiles\Docker\Docker\DockerCli.exe"
}
else {
Write-Warning "Switching to Linux Engine"
& $Env:ProgramFiles\Docker\Docker\DockerCli.exe -SwitchLinuxEngine
}
$Env:SERVICEACCOUNT = Join-Path (split-path -parent $MyInvocation.MyCommand.Definition) "serviceaccount"
# Define the array of environment variable names to check
$envVarsToCheck = @(
"IMAGE_VERSION",
"REGISTRY_PATH"
)
foreach ($envVarName in $envVarsToCheck) {
$envVarValue = [System.Environment]::GetEnvironmentVariable($envVarName)
if ([string]::IsNullOrWhiteSpace($envVarValue)) {
throw "Environment variable '$envVarName' is empty or not set. Rename envsettings.ps1.template to envsettings.ps1 and complete the environment variables or set them for the current environment."
}
}
$version = $ENV:IMAGE_VERSION;
$containerregistry = $ENV:REGISTRY_PATH;
Write-Host "Environment IMAGE_VERSION: $($version)"
Write-Host "Environment REGISTRY_PATH: $($containerregistry)"
if (-not $containerregistry.EndsWith('/')) {
# Add a slash to the end of $containerregistry
$containerregistry = "$containerregistry/"
}
# Image names
$Env:IMG_KEEL = "$($containerregistry)keel:$($version)";
$Env:IMG_KEEL_DEBUG = "$($containerregistry)keel-debug:$($version)";
$Env:IMG_KEEL_TESTS = "$($containerregistry)keel-tests:$($version)";
docker compose build
ThrowIfError
if ($StartContainers) {
docker compose up
ThrowIfError
}
if ($StartDebugContainers) {
docker compose -f compose.debug.yml up
ThrowIfError
}
if ($RunTests -eq $true) {
Write-Host "Running tests..."
docker compose -f compose.tests.yml up -d --build
ThrowIfError
$testResultsFile = "/go/src/github.com/keel-hq/keel/test_results.json"
$localResultsPath = Join-Path $TESTDIR "test_results.xml"
$containerName = "keel_tests"
docker exec $containerName sh -c "make test"
# This one is to export the results
docker exec $containerName sh -c "go test -v `$(go list ./... | grep -v /tests) -cover 2>&1 | go-junit-report > $testResultsFile"
docker cp "$($containerName):$($testResultsFile)" $localResultsPath
Write-Host "Test results copied to $localResultsPath"
docker compose -f compose.tests.yml down
}
if ($Push) {
if ($Env:REGISTRY_USER -and $Env:REGISTRY_PWD) {
Write-Output "Container registry credentials through environment provided."
# Identify the registry
$registryHost = $ENV:REGISTRY_SERVER;
Write-Output "Remote registry host: $($registryHost)";
docker login "$($registryHost)" -u="$($Env:REGISTRY_USER)" -p="$($Env:REGISTRY_PWD)"
ThrowIfError
}
docker push "$($Env:IMG_KEEL)"
}