-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.ps1
More file actions
110 lines (88 loc) · 3.84 KB
/
setup.ps1
File metadata and controls
110 lines (88 loc) · 3.84 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
param (
[string]$ContainerName,
[string]$ConnectionStringName,
[string]$InitScript = "",
[string]$Tag,
[string]$RegistryLoginServer = "index.docker.io",
[string]$RegistryUser,
[string]$RegistryPass
)
$dockerImage = "postgres:18"
$password = [guid]::NewGuid().ToString("n")
Write-Output "::add-mask::$password"
$userName = "postgres"
$databaseName = "postgres"
$ipAddress = "127.0.0.1"
$port = 5432
$runnerOs = $Env:RUNNER_OS ?? "Linux"
$resourceGroup = $Env:RESOURCE_GROUP_OVERRIDE ?? "GitHubActions-RG"
$env:PGPASSWORD = $password
if ($runnerOs -eq "Linux") {
Write-Output "Running Postgres in container $($ContainerName) using Docker"
docker run --name "$($ContainerName)" -d -p "$($port):$($port)" -e POSTGRES_PASSWORD=$password -e POSTGRES_USER=$userName -e POSTGRES_DB=$databaseName $dockerImage -c max_prepared_transactions=10
}
elseif ($runnerOs -eq "Windows") {
Write-Output "Running Postgres in container $($ContainerName) using Azure"
if ($Env:REGION_OVERRIDE) {
$region = $Env:REGION_OVERRIDE
}
else {
$hostInfo = curl -H Metadata:true "169.254.169.254/metadata/instance?api-version=2017-08-01" | ConvertFrom-Json
$region = $hostInfo.compute.location
}
$runnerOsTag = "RunnerOS=$($runnerOs)"
$packageTag = "Package=$Tag"
$dateTag = "Created=$(Get-Date -Format "yyyy-MM-dd")"
# psql not in PATH on Windows
$Env:PATH = $Env:PATH + ';' + $Env:PGBIN
$azureContainerCreate = "az container create --image $dockerImage --name $ContainerName --location $region --resource-group $resourceGroup --cpu 2 --memory 8 --ports $port --ip-address public --os-type Linux --environment-variables POSTGRES_PASSWORD=$password POSTGRES_USER=$userName POSTGRES_DB=$databaseName --command-line 'docker-entrypoint.sh postgres --max-prepared-transactions=10'"
if ($registryUser -and $registryPass) {
Write-Output "Creating container with login to $RegistryLoginServer"
$azureContainerCreate = "$azureContainerCreate --registry-login-server $RegistryLoginServer --registry-username $RegistryUser --registry-password $RegistryPass"
} else {
Write-Output "Creating container with anonymous credentials"
}
Write-Output "Creating container $ContainerName in $region (this can take a while)"
$containerJson = Invoke-Expression $azureContainerCreate
if (!$containerJson) {
Write-Output "Failed to create container $ContainerName in $region"
exit 1;
}
$containerDetails = $containerJson | ConvertFrom-Json
if (!$containerDetails.ipAddress) {
Write-Output "Failed to create container $ContainerName in $region"
Write-Output $containerJson
exit 1;
}
$ipAddress = $containerDetails.ipAddress.ip
Write-Output "::add-mask::$ipAddress"
Write-Output "Tagging the container"
az tag create --resource-id $containerDetails.id --tags $packageTag $runnerOsTag $dateTag | Out-Null
}
else {
Write-Output "$runnerOs not supported"
exit 1
}
Write-Output "::group::Testing connection"
for ($i = 0; $i -lt 24; $i++) { ## 2 minute timeout
Write-Output "Checking for PostgreSQL connectivity $($i+1)/30..."
psql --host $ipAddress --username=$userName --list > $null
if ($?) {
Write-Output "Connection successful"
break;
}
sleep 5
}
Write-Output "::endgroup::"
# write the connection string to the specified environment variable
"$($ConnectionStringName)=User ID=$($userName);Password=$($password);Host=$($ipAddress);Port=$($port);Database=$($databaseName);" >> $Env:GITHUB_ENV
if ($InitScript) {
Write-Output "::group::Running init script $InitScript"
$script = Get-Content $InitScript -Raw
psql --host $ipAddress --username=$userName --command $script
if (-not $?) {
Write-Output "Script execution failed"
exit 1
}
Write-Output "::endgroup::"
}