|
| 1 | +Param( |
| 2 | + [parameter(Mandatory=$false)][string]$registry, |
| 3 | + [parameter(Mandatory=$false)][string]$dockerUser, |
| 4 | + [parameter(Mandatory=$false)][string]$dockerPassword, |
| 5 | + [parameter(Mandatory=$false)][string]$imageTag, |
| 6 | + [parameter(Mandatory=$false)][bool]$buildImages=$true, |
| 7 | + [parameter(Mandatory=$false)][bool]$pushImages=$true, |
| 8 | + [parameter(Mandatory=$false)][string]$dockerOrg="eshop" |
| 9 | +) |
| 10 | + |
| 11 | +# Initialization |
| 12 | + |
| 13 | +$useDockerHub = [string]::IsNullOrEmpty($registry) |
| 14 | + |
| 15 | +# Check required commands (only if not in CI environment) |
| 16 | + |
| 17 | +$requiredCommands = ("docker", "docker-compose") |
| 18 | +foreach ($command in $requiredCommands) { |
| 19 | + if ((Get-Command $command -ErrorAction SilentlyContinue) -eq $null) { |
| 20 | + Write-Host "$command must be on path" -ForegroundColor Red |
| 21 | + exit |
| 22 | + } |
| 23 | +} |
| 24 | + |
| 25 | +# Get tag to use from current branch if no tag is passed |
| 26 | +if ([string]::IsNullOrEmpty($imageTag)) { |
| 27 | + $imageTag = $(git rev-parse --abbrev-ref HEAD) |
| 28 | +} |
| 29 | +Write-Host "Docker image Tag: $imageTag" -ForegroundColor Yellow |
| 30 | + |
| 31 | +# Build docker images if needed |
| 32 | +if ($buildImages) { |
| 33 | + Write-Host "Building Docker images tagged with '$imageTag'" -ForegroundColor Yellow |
| 34 | + $env:TAG=$imageTag |
| 35 | + docker-compose -p .. -f ../docker-compose.yml build |
| 36 | +} |
| 37 | + |
| 38 | +# Login to Docker registry |
| 39 | +if (-not [string]::IsNullOrEmpty($dockerUser)) { |
| 40 | + $registryFDQN = if (-not $useDockerHub) {$registry} else {"index.docker.io/v1/"} |
| 41 | + |
| 42 | + Write-Host "Logging in to $registryFDQN as user $dockerUser" -ForegroundColor Yellow |
| 43 | + if ($useDockerHub) { |
| 44 | + docker login -u $dockerUser -p $dockerPassword |
| 45 | + } |
| 46 | + else { |
| 47 | + docker login -u $dockerUser -p $dockerPassword $registryFDQN |
| 48 | + } |
| 49 | + |
| 50 | + if (-not $LastExitCode -eq 0) { |
| 51 | + Write-Host "Login failed" -ForegroundColor Red |
| 52 | + exit |
| 53 | + } |
| 54 | + |
| 55 | +} |
| 56 | + |
| 57 | +# Push images to Docker registry |
| 58 | +if ($pushImages) { |
| 59 | + Write-Host "Pushing images to $registry/$dockerOrg..." -ForegroundColor Yellow |
| 60 | + $services = ("basket.api", "catalog.api", "identity.api", "ordering.api", "ordering.backgroundtasks", "marketing.api","payment.api","locations.api", "webmvc", "webspa", "webstatus", "ocelotapigw", "mobileshoppingagg", "webshoppingagg", "ordering.signalrhub") |
| 61 | + |
| 62 | + foreach ($service in $services) { |
| 63 | + $imageFqdn = if ($useDockerHub) {"$dockerOrg/${service}"} else {"$registry/$dockerOrg/${service}"} |
| 64 | + docker tag eshop/${service}:$imageTag ${imageFqdn}:$imageTag |
| 65 | + docker push ${imageFqdn}:$imageTag |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + |
0 commit comments