|
1 | 1 | Param( |
2 | | - [parameter(Mandatory=$true)][string]$registry, |
3 | | - [parameter(Mandatory=$true)][string]$dockerUser, |
4 | | - [parameter(Mandatory=$true)][string]$dockerPassword |
| 2 | + [parameter(Mandatory=$false)][string]$registry, |
| 3 | + [parameter(Mandatory=$false)][string]$dockerUser, |
| 4 | + [parameter(Mandatory=$false)][string]$dockerPassword, |
| 5 | + [parameter(Mandatory=$false)][bool]$deployCI, |
| 6 | + [parameter(Mandatory=$false)][bool]$useDockerHub, |
| 7 | + [parameter(Mandatory=$false)][string]$execPath, |
| 8 | + [parameter(Mandatory=$false)][string]$kubeconfigPath |
5 | 9 | ) |
6 | 10 |
|
7 | | -$requiredCommands = ("docker", "docker-compose", "kubectl") |
8 | | -foreach ($command in $requiredCommands) { |
9 | | - if ((Get-Command $command -ErrorAction SilentlyContinue) -eq $null) { |
10 | | - Write-Host "$command must be on path" -ForegroundColor Red |
11 | | - exit |
| 11 | +function ExecKube($cmd) { |
| 12 | + if($deployCI) { |
| 13 | + $kubeconfig = $kubeconfigPath + 'config'; |
| 14 | + $exp = $execPath + 'kubectl ' + $cmd + ' --kubeconfig=' + $kubeconfig |
| 15 | + Invoke-Expression $exp |
| 16 | + } |
| 17 | + else{ |
| 18 | + $exp = $execPath + 'kubectl ' + $cmd |
| 19 | + Invoke-Expression $exp |
12 | 20 | } |
13 | 21 | } |
14 | 22 |
|
15 | | -Write-Host "Logging in to $registry" -ForegroundColor Yellow |
16 | | -docker login -u $dockerUser -p $dockerPassword $registry |
17 | | -if (-not $LastExitCode -eq 0) { |
18 | | - Write-Host "Login failed" -ForegroundColor Red |
19 | | - exit |
| 23 | +# Not used when deploying through CI VSTS |
| 24 | +if(-not $deployCI) { |
| 25 | + $requiredCommands = ("docker", "docker-compose", "kubectl") |
| 26 | + foreach ($command in $requiredCommands) { |
| 27 | + if ((Get-Command $command -ErrorAction SilentlyContinue) -eq $null) { |
| 28 | + Write-Host "$command must be on path" -ForegroundColor Red |
| 29 | + exit |
| 30 | + } |
| 31 | + } |
20 | 32 | } |
21 | 33 |
|
22 | | -# create registry key secret |
23 | | -kubectl create secret docker-registry registry-key ` |
| 34 | +# Use ACR instead of DockerHub as image repository |
| 35 | +if(-not $useDockerHub) { |
| 36 | + Write-Host "Logging in to $registry" -ForegroundColor Yellow |
| 37 | + docker login -u $dockerUser -p $dockerPassword $registry |
| 38 | + if (-not $LastExitCode -eq 0) { |
| 39 | + Write-Host "Login failed" -ForegroundColor Red |
| 40 | + exit |
| 41 | + } |
| 42 | + |
| 43 | + # create registry key secret |
| 44 | + ExecKube -cmd 'create secret docker-registry registry-key ` |
24 | 45 | --docker-server=$registry ` |
25 | 46 | --docker-username=$dockerUser ` |
26 | 47 | --docker-password=$dockerPassword ` |
27 | | - --docker-email=not@used.com |
| 48 | + --docker-email=not@used.com' |
| 49 | +} |
28 | 50 |
|
29 | | -# start sql, rabbitmq, frontend deployments |
30 | | -kubectl create configmap config-files --from-file=nginx-conf=nginx.conf |
31 | | -kubectl label configmap config-files app=eshop |
32 | | -kubectl create -f sql-data.yaml -f rabbitmq.yaml -f services.yaml -f frontend.yaml |
| 51 | +# Removing previous services & deployments |
| 52 | +Write-Host "Removing existing services & deployments.." -ForegroundColor Yellow |
| 53 | +ExecKube -cmd 'delete -f sql-data.yaml -f rabbitmq.yaml' |
| 54 | +ExecKube -cmd 'delete -f services.yaml -f frontend.yaml -f deployments.yaml' |
| 55 | +ExecKube -cmd 'delete configmap config-files' |
| 56 | +ExecKube -cmd 'delete configmap urls' |
33 | 57 |
|
34 | | -Write-Host "Building and publishing eShopOnContainers..." -ForegroundColor Yellow |
35 | | -dotnet restore ../eShopOnContainers-ServicesAndWebApps.sln |
36 | | -dotnet publish -c Release -o obj/Docker/publish ../eShopOnContainers-ServicesAndWebApps.sln |
| 58 | +# start sql, rabbitmq, frontend deploymentsExecKube -cmd 'delete configmap config-files' |
| 59 | +ExecKube -cmd 'create configmap config-files --from-file=nginx-conf=nginx.conf' |
| 60 | +ExecKube -cmd 'label configmap config-files app=eshop' |
| 61 | +ExecKube -cmd 'create -f sql-data.yaml -f rabbitmq.yaml -f services.yaml -f frontend.yaml' |
37 | 62 |
|
38 | | -Write-Host "Building Docker images..." -ForegroundColor Yellow |
39 | | -docker-compose -p .. -f ../docker-compose.yml build |
| 63 | +# building and publishing docker images not necessary when deploying through CI VSTS |
| 64 | +if(-not $deployCI) { |
| 65 | + Write-Host "Building and publishing eShopOnContainers..." -ForegroundColor Yellow |
| 66 | + dotnet restore ../eShopOnContainers-ServicesAndWebApps.sln |
| 67 | + dotnet publish -c Release -o obj/Docker/publish ../eShopOnContainers-ServicesAndWebApps.sln |
40 | 68 |
|
41 | | -Write-Host "Pushing images to $registry..." -ForegroundColor Yellow |
42 | | -$services = ("basket.api", "catalog.api", "identity.api", "ordering.api", "webmvc", "webspa") |
43 | | -foreach ($service in $services) { |
44 | | - docker tag eshop/$service $registry/eshop/$service |
45 | | - docker push $registry/eshop/$service |
| 69 | + Write-Host "Building Docker images..." -ForegroundColor Yellow |
| 70 | + docker-compose -p .. -f ../docker-compose.yml build |
| 71 | + |
| 72 | + Write-Host "Pushing images to $registry..." -ForegroundColor Yellow |
| 73 | + $services = ("basket.api", "catalog.api", "identity.api", "ordering.api", "webmvc", "webspa") |
| 74 | + foreach ($service in $services) { |
| 75 | + docker tag eshop/$service $registry/eshop/$service |
| 76 | + docker push $registry/eshop/$service |
| 77 | + } |
46 | 78 | } |
47 | 79 |
|
48 | 80 | Write-Host "Waiting for frontend's external ip..." -ForegroundColor Yellow |
49 | 81 | while ($true) { |
50 | | - $frontendUrl = kubectl get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}" |
| 82 | + $frontendUrl = & ExecKube -cmd 'get svc frontend -o=jsonpath="{.status.loadBalancer.ingress[0].ip}"' |
51 | 83 | if ([bool]($frontendUrl -as [ipaddress])) { |
52 | 84 | break |
53 | 85 | } |
54 | 86 | Start-Sleep -s 15 |
55 | 87 | } |
56 | 88 |
|
57 | | -kubectl create configmap urls ` |
| 89 | +ExecKube -cmd 'create configmap urls ` |
58 | 90 | --from-literal=BasketUrl=http://$($frontendUrl)/basket-api ` |
59 | 91 | --from-literal=CatalogUrl=http://$($frontendUrl)/catalog-api ` |
60 | 92 | --from-literal=IdentityUrl=http://$($frontendUrl)/identity ` |
61 | 93 | --from-literal=OrderingUrl=http://$($frontendUrl)/ordering-api ` |
62 | 94 | --from-literal=MvcClient=http://$($frontendUrl)/webmvc ` |
63 | | - --from-literal=SpaClient=http://$($frontendUrl) |
64 | | -kubectl label configmap urls app=eshop |
| 95 | + --from-literal=SpaClient=http://$($frontendUrl)' |
| 96 | + |
| 97 | +ExecKube -cmd 'label configmap urls app=eshop' |
65 | 98 |
|
66 | 99 | Write-Host "Creating deployments..." |
67 | | -kubectl apply -f deployments.yaml |
68 | | - |
69 | | -# update deployments with the private registry before k8s tries to pull images |
70 | | -# (deployment templating, or Helm, would obviate this) |
71 | | -kubectl set image -f deployments.yaml ` |
72 | | - basket=$registry/eshop/basket.api ` |
73 | | - catalog=$registry/eshop/catalog.api ` |
74 | | - identity=$registry/eshop/identity.api ` |
75 | | - ordering=$registry/eshop/ordering.api ` |
76 | | - webmvc=$registry/eshop/webmvc ` |
77 | | - webspa=$registry/eshop/webspa |
78 | | -kubectl rollout resume -f deployments.yaml |
| 100 | +ExecKube -cmd 'create -f deployments.yaml' |
| 101 | + |
| 102 | +# not using ACR for pulling images when deploying through CI VSTS |
| 103 | +if(-not $deployCI) { |
| 104 | + # update deployments with the private registry before k8s tries to pull images |
| 105 | + # (deployment templating, or Helm, would obviate this) |
| 106 | + ExecKube -cmd 'set image -f deployments.yaml ` |
| 107 | + basket=$registry/eshop/basket.api ` |
| 108 | + catalog=$registry/eshop/catalog.api ` |
| 109 | + identity=$registry/eshop/identity.api ` |
| 110 | + ordering=$registry/eshop/ordering.api ` |
| 111 | + webmvc=$registry/eshop/webmvc ` |
| 112 | + webspa=$registry/eshop/webspa' |
| 113 | +} |
| 114 | + |
| 115 | +ExecKube -cmd 'rollout resume -f deployments.yaml' |
79 | 116 |
|
80 | 117 | Write-Host "WebSPA is exposed at http://$frontendUrl, WebMVC at http://$frontendUrl/webmvc" -ForegroundColor Yellow |
0 commit comments