-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdocker-build-cmd.ps1
More file actions
51 lines (39 loc) · 1.47 KB
/
docker-build-cmd.ps1
File metadata and controls
51 lines (39 loc) · 1.47 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
# Check if .env file exists
if (-not (Test-Path .env)) {
Write-Error "Error: .env file not found."
exit 1
}
# Read the .env file and load each line as environment variables
$envVars = @{} # Use a hashtable for easier access
Get-Content .env | ForEach-Object {
if ($_ -match "^\s*([A-Za-z0-9_]+)=(.*)\s*$") {
$envVars[$matches[1]] = $matches[2]
}
}
# Initialize the docker build command as an array
$dockerCmdArray = @("docker", "build")
# Add the image name and tag
$imageName = "ssw-website"
$dockerCmdArray += "-t", "$imageName"
# Define the list of valid prefixes for environment variables
$validPrefixes = @("NEXT_PUBLIC_", "GOOGLE_", "MICROSOFT_", "KEY_VAULT", "TINA_", "DYNAMICS_", "SITE_URL")
# Loop through the environment variables and add build args for matching prefixes
foreach ($key in $envVars.Keys) {
foreach ($prefix in $validPrefixes) {
if ($key.StartsWith($prefix)) {
$dockerCmdArray += "--build-arg", "$key=$($envVars[$key])"
}
}
}
# Add the docker build context (assuming current directory is the context)
$dockerCmdArray += "."
# Join the array with spaces and newlines for output
$dockerCmd = $dockerCmdArray -join "`n"
# Output the final command
Write-Host "Generated Docker build command:"
Write-Host $dockerCmd
# Join the array with spaces for clipboard
$dockerCmdClipboard = $dockerCmdArray -join " "
# Copy the command to the clipboard
$dockerCmdClipboard | Set-Clipboard
Write-Host "Docker build command copied to clipboard."