-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8515e38
commit f27460e
Showing
2 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
param ( | ||
[string]$DockerFileName = "Dockerfile", | ||
[string]$DockerImageName = "example", | ||
[string]$DockerContainerName = "ubuntu", | ||
[string]$WorkingDirectory = (Get-Location).Path, | ||
[string]$DebugMode = "false" | ||
) | ||
|
||
# Function to check if the Dockerfile exists | ||
function Check-DockerFileExists { | ||
$filePath = Join-Path -Path $WorkingDirectory -ChildPath $DockerFileName | ||
if (-not (Test-Path -Path $filePath)) { | ||
Write-Error "Error: Dockerfile not found at $filePath. Exiting." | ||
exit 1 | ||
} | ||
else { | ||
Write-Host "Success: Dockerfile found at: $filePath" -ForegroundColor Green | ||
} | ||
} | ||
|
||
# Function to check if Docker is installed | ||
function Check-DockerExists { | ||
try { | ||
$dockerPath = Get-Command docker -ErrorAction Stop | ||
Write-Host "Success: Docker found at: $($dockerPath.Source)" -ForegroundColor Green | ||
} | ||
catch { | ||
Write-Error "Error: Docker is not installed or not in PATH. Exiting." | ||
exit 1 | ||
} | ||
} | ||
|
||
# Function to build a Docker image | ||
function Build-DockerImage { | ||
try { | ||
Write-Host "Info: Building Docker image $DockerImageName from $DockerFileName" -ForegroundColor Green | ||
docker build -t $DockerImageName -f $DockerFileName | Out-Host | ||
if ($LASTEXITCODE -eq 0) { | ||
return $true | ||
} | ||
else { | ||
Write-Error "Error: Docker build failed with exit code $LASTEXITCODE" | ||
return $false | ||
} | ||
} | ||
catch { | ||
Write-Error "Error: Docker build encountered an exception" | ||
return $false | ||
} | ||
} | ||
|
||
# Convert string parameters to boolean | ||
$DebugMode = $DebugMode -eq "true" | ||
|
||
# Enable debug mode if DebugMode is set to $true | ||
if ($DebugMode) { | ||
$DebugPreference = "Continue" | ||
} | ||
|
||
# Diagnostic output | ||
Write-Debug "DockerFileName: $DockerFileName" | ||
Write-Debug "DockerImageName: $DockerImageName" | ||
Write-Debug "DockerContainerName: $DockerContainerName" | ||
Write-Debug "DebugMode: $DebugMode" | ||
|
||
# Checking prerequisites | ||
Check-DockerExists | ||
Check-DockerFileExists | ||
|
||
# Execution flow | ||
$buildSuccess = Build-DockerImage | Out-Host | ||
|
||
if ($buildSuccess -eq $true) { | ||
Write-Host "Docker build complete." -ForegroundColor Green | ||
} | ||
else { | ||
Write-Host "Docker build failed." -ForegroundColor Green | ||
exit 1 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters