Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
craigthackerx committed Feb 22, 2024
1 parent 9166295 commit 034211b
Showing 1 changed file with 90 additions and 43 deletions.
133 changes: 90 additions & 43 deletions Run-Docker.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,39 @@ param (
[string]$ImageOrg,
[string]$WorkingDirectory = (Get-Location).Path,
[string]$DebugMode = "false",
[string]$PushDockerImage = "true"
[string]$PushDockerImage = "true",
[string[]]$AdditionalTags = @((Get-Date -Format "yyyy-MM"))
)

# Function to convert string to boolean
function Convert-ToBoolean($value) {
function Convert-ToBoolean($value)
{
$valueLower = $value.ToLower()
if ($valueLower -eq "true") {
if ($valueLower -eq "true")
{
return $true
}
elseif ($valueLower -eq "false") {
elseif ($valueLower -eq "false")
{
return $false
}
else {
else
{
Write-Error "Error: Invalid value - $value. Exiting."
exit 1
}
}

# Function to check if Docker is installed
function Check-DockerExists {
try {
function Check-DockerExists
{
try
{
$dockerPath = Get-Command docker -ErrorAction Stop
Write-Host "Success: Docker found at: $($dockerPath.Source)" -ForegroundColor Green
Write-Host "Success: Docker found at: $( $dockerPath.Source )" -ForegroundColor Green
}
catch {
catch
{
Write-Error "Error: Docker is not installed or not in PATH. Exiting."
exit 1
}
Expand All @@ -105,7 +113,8 @@ else

$DockerImageName = "${RegistryUrl}/${ImageOrganisation}/${DockerImageName}"

function Build-DockerImage {
function Build-DockerImage
{
param (
[string]$Path,
[string]$DockerFile
Expand All @@ -114,66 +123,90 @@ function Build-DockerImage {
$filePath = Join-Path -Path $Path -ChildPath $DockerFile

# Check if Dockerfile exists at the specified path
if (-not (Test-Path -Path $filePath)) {
if (-not(Test-Path -Path $filePath))
{
Write-Error "Error: Dockerfile not found at $filePath. Exiting."
return $false
}

try {
try
{
Write-Host "Info: Building Docker image $DockerImageName from $filePath" -ForegroundColor Green
docker build -t $DockerImageName -f $filePath $Path | Out-Host
if ($LASTEXITCODE -eq 0) {
if ($LASTEXITCODE -eq 0)
{
return $true
}
else {
else
{
Write-Error "Error: Docker build failed with exit code $LASTEXITCODE"
return $false
}
}
catch {
catch
{
Write-Error "Error: Docker build encountered an exception"
return $false
}
}

function Push-DockerImage {
try {
function Push-DockerImage
{
param (
[string[]]$FullTagNames
)

try
{
Write-Host "Info: Logging into Docker registry $RegistryUrl" -ForegroundColor Green
$RegistryPassword | docker login $RegistryUrl -u $RegistryUsername --password-stdin

if ($LASTEXITCODE -eq 0) {
Write-Host "Info: Pushing Docker image $DockerImageName to $Registry" -ForegroundColor Green
docker push $DockerImageName | Out-Host
if ($LASTEXITCODE -eq 0) {
Write-Host "Success: Docker image pushed successfully." -ForegroundColor Green
return $true
}
else {
Write-Error "Error: Docker push failed with exit code $LASTEXITCODE"
return $false
if ($LASTEXITCODE -eq 0)
{
foreach ($tagName in $FullTagNames)
{
Write-Host "Info: Pushing Docker image $tagName to registry" -ForegroundColor Green
docker push $tagName | Out-Host
if ($LASTEXITCODE -eq 0)
{
Write-Host "Success: Docker image $tagName pushed successfully." -ForegroundColor Green
}
else
{
Write-Error "Error: Docker push failed for tag $tagName with exit code $LASTEXITCODE"
# Depending on your preference, you might choose to stop trying to push additional tags after the first failure
# return $false
}
}
# Assuming all tags are pushed successfully if we reach this point
return $true
}
else {
else
{
Write-Error "Error: Docker login failed with exit code $LASTEXITCODE"
return $false
}
}
catch {
catch
{
Write-Error "Error: An exception occurred during Docker push"
return $false
}
finally {
Write-Host "Info: Logging out of Docker registry $Registry" -ForegroundColor Green
docker logout $Registry
finally
{
Write-Host "Info: Logging out of Docker registry $RegistryUrl" -ForegroundColor Green
docker logout $RegistryUrl
}
}


# Convert string parameters to boolean
$DebugMode = Convert-ToBoolean $DebugMode
$PushDockerImage = Convert-ToBoolean $PushDockerImage

# Enable debug mode if DebugMode is set to $true
if ($DebugMode) {
if ($DebugMode)
{
$DebugPreference = "Continue"
}

Expand All @@ -188,20 +221,34 @@ Check-DockerExists
# Execution flow
$buildSuccess = Build-DockerImage -Path $WorkingDirectory -DockerFile $DockerFileName

if ($buildSuccess) {
if ($buildSuccess)
{
Write-Host "Docker build complete." -ForegroundColor Green
if ($PushDockerImage -eq $true) {
$pushSuccess = Push-DockerImage
if ($pushSuccess) {
Write-Host "Docker image push complete." -ForegroundColor Green
}
else {
Write-Host "Docker image push failed." -ForegroundColor Red
exit 1
foreach ($tag in $AdditionalTags)
{
$fullTagName = "${RegistryUrl}/${ImageOrganisation}/${DockerImageName}:$tag"
Write-Host "Info: Tagging Docker image as $fullTagName" -ForegroundColor Green
docker tag $DockerImageName $fullTagName
}
if ($PushDockerImage -eq $true)
{
$fullTagNames = @()
foreach ($tag in $AdditionalTags)
{
$fullTagNames += "${RegistryUrl}/${ImageOrganisation}/${DockerImageName}:$tag"
}
Push-DockerImage -FullTagNames $fullTagNames
}

else
{
Write-Host "Docker image push failed." -ForegroundColor Red
exit 1
}
}
else {

else
{
Write-Host "Docker build failed." -ForegroundColor Red
exit 1
}

0 comments on commit 034211b

Please sign in to comment.