You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
4.5 KiB

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