Chinaunix首页 | 论坛 | 博客
  • 博客访问: 48833
  • 博文数量: 11
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 140
  • 用 户 组: 普通用户
  • 注册时间: 2020-07-22 17:26
个人简介

blender https://www.blender-3d.cn/ blender3D模型库

文章分类

全部博文(11)

文章存档

2020年(11)

我的朋友

分类: WINDOWS

2020-08-05 15:52:46

延续先前使用gitlab的CI流程,这次加入PowerShell完成后续CD的流程,笔记一下这次的内容
神父
这次为yml档加上deploy的stage,很单纯的只呼叫ps1做事,
专案有两个ps1,皆放在资料夹Ps1中

点击(此处)折叠或打开

  1. stages:
  2.   - deploy

  3. before_script:
  4.   - chcp 65001

  5. deploy:
  6.   stage: deploy
  7.   script:
  8.       - dotnet publish ./MyProject/ -c release -o ./output
  9.       - ./Ps1/PreDeploy.ps1 $env:DeployAccount_Stage $env:DeployPassword_Stage

dotnet发布./MyProject/ -c版本-o ./output
指定release组态发布,并且指定发布的位置在“./output”

$ env:DeployAccount_Stage
PowerShell取得环境变数的方式,相关值会存在gitlab的“Settings > Variables”

./Ps1/PreDeploy.ps1 $ env:DeployAccount_Stage $ env:DeployPassword_Stage
执行PreDeploy.ps1,并且传入两个值

PreDeploy.ps1(GitLab Runner)

点击(此处)折叠或打开

  1. $account = $args[0]
  2. $password = $args[1]
  3. <# 取的傳入的參數值 #>
  4. $serverName = "192.168.1.1"
  5. <# 要部署的server位置 #>

  6. $projectName = "MyProject"
  7. $version = Get-Date -Format "yyyyMMddHHmmss"
  8. $location = $(PWD)
  9. <# 取得當下的絕對路徑,Get-Location的別名 #>
  10. $publishPath = "$location\output"

  11. $deployPsPath = "$location\Ps1\Deploy.ps1"
  12. $remoteDeployPath = "C:\Deploy\"

  13. <# 將publish的結果壓縮為.zip #>
  14. echo "Compress files"
  15. $zipDir = "$location\$projectName\"
  16. if ((Test-Path $zipDir -PathType Any) -eq 0)
  17. {
  18.     new-item $zipDir -itemtype directory
  19. }

  20. $zipFilePath = "$zipDir$version.zip";
  21. $compress = @{
  22.     Path = $publishPath
  23.     CompressionLevel = "Fastest"
  24.     DestinationPath = $zipFilePath
  25. }
  26. Compress-Archive @compress -Update
  27. if(!$?) { throw $LASTEXITCODE }
  28. echo "Compress files $zipFilePath"



  29. <# 遠端連線到目標主機 *註1 #>
  30. echo "Get Session"
  31. $pw = convertto-securestring -AsPlainText -Force -String $password
  32. $cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $account, $pw
  33. $session = New-PSSession -ComputerName $serverName -Credential $cred
  34. if(!$?) { throw $LASTEXITCODE }
  35. echo "Get Session done"
  36. <# 遠端連線失敗則中斷script,並且返回錯誤訊息 #>



  37. <# 將zip檔複製到遠端主機上 #>
  38. echo "copy zip file"
  39. $targetDir = "C:\Deploy\Zip\"
  40. if ((Test-Path $targetDir -PathType Any) -eq 0)
  41. {
  42.     Invoke-Command -Session $session -Command { New-Item $args[0] -ItemType directory -force } -Args $targetDir
  43. }
  44. $publishZipFile = "$targetDir$version.zip"
  45. Copy-Item -path $zipFilePath -Destination $targetDir -ToSession $session
  46. if(!$?) { throw $LASTEXITCODE }
  47. echo "copy zip file $publishZipFile"
  48. <# 複製失敗則中斷 #>



  49. <# 將 deploy.ps1 複製到遠端主機,遠端主機的部署流程會放在這 #>
  50. Copy-Item -path $deployPsPath -Destination $remoteDeployPath -ToSession $session
  51. if(!$?) { throw $LASTEXITCODE }
  52. <# 複製失敗則中斷 #>



  53. <# 呼叫遠端主機執行 Deploy.ps1 #>
  54. Invoke-Command -Session $session -scriptblock {
  55.     C:\deploy\Deploy.ps1 $args[0] $args[1]
  56. } -ArgumentList $publishZipFile, $version


  57. <# 關閉遠端連線 #>
  58. $session | Remove-PSSession

Deploy.ps1(目标服务器)

点击(此处)折叠或打开

  1. $zipPath = $args[0]
  2. $version = $args[1]
  3. <# 取得傳入的參數值 #>

  4. $name = "MyProject"
  5. $backupDirPath = "C:\Deploy\Backup"
  6. $expandPath = "C:\Deploy\Expand"
  7. $webSitePath = "C:\inetpub"

  8. <# 解壓縮zip #>
  9. echo "begin expand zip file"
  10. Expand-Archive -LiteralPath $zipPath -DestinationPath $expandPath -Force
  11. if(!$?) { throw $LASTEXITCODE }
  12. <# 失敗則中斷 #>
  13. echo "end expand zip file, path: $expandPath"
  14. echo "---"


  15. $pool = $name

  16. <# 停止IIS上的 application pool #>
  17. echo "begin stop application pool '$pool'"
  18. $retryCount = 0;
  19. $state = (Get-WebAppPoolState -Name $pool).Value
  20. <# retry 30次,直到成功,每次失敗休息1秒 #>
  21. while($state -Like "Start*")
  22. {
  23.     Stop-WebAppPool -Name $pool
  24.     $state = (Get-WebAppPoolState -Name $pool).Value
  25.     if($state -Like "Start*")
  26.     {
  27.         $retryCount += 1;
  28.         echo "retry stop pool, $retryCount."
  29.         Start-Sleep -Milliseconds 1000
  30.     }
  31.     if($retryCount -eq 30)
  32.     {
  33.         throw "stop application pool '$pool' get 10 time error."
  34.     }
  35. }
  36. Start-Sleep -Milliseconds 1000
  37. echo "end stop application pool '$pool'"
  38. echo "---"

  39. <# 將原本的檔案備份 #>
  40. echo "begin backup folder"
  41. $folderPath = "$webSitePath\$pool"
  42. $backupPath = "$backupDirPath\$version"
  43. if((Test-Path $backupPath -PathType Any) -eq 0)
  44. {
  45.     new-item $backupPath -itemtype directory
  46. }

  47. Copy-Item -Path $folderPath -Recurse $backupPath -Force -Exclude ("*.log")
  48. if(!$?) { throw $LASTEXITCODE }
  49. echo "end move backup folder, source: '$folderPath' target: '$backupPath'"
  50. echo "---"


  51. <# zip解壓縮後的檔案複製過去 #>
  52. echo "copy file to iis folder $pool"
  53. [string]$expandFiles = "$expandPath\output\*"
  54. [string]$targetPath = "$webSitePath\$pool"
  55. Copy-item -Force -Recurse -Verbose $expandFiles -Destination $targetPath
  56. echo "copy file to iis folder $pool, sourc: $expandFiles path: $targetPath"



  57. <# 開啟IIS的 applcation pool #>
  58. echo "begin start application pool '$pool'"
  59. $retryCount = 0;
  60. $state = (Get-WebAppPoolState -Name $pool).Value
  61. while($state -Like "Stop*")
  62. {
  63.     Start-WebAppPool -Name $pool
  64.     $state = (Get-WebAppPoolState -Name $pool).Value
  65.     if($state -Like "Stop*")
  66.     {
  67.         $retryCount += 1
  68.         echo "retry start pool, $retryCount."
  69.         Start-Sleep -Milliseconds 1000
  70.     }
  71.     if($retryCount -eq 30)
  72.     {
  73.         throw "start application pool '$pool' get 10 time error."
  74.     }
  75. }
  76. echo "end start application pool '$pool'"
  77. echo "---"


  78. <# 發request戳醒站台,並檢查是否正常 #>
  79. $HTTP_Request = [System.Net.WebRequest]::Create('http://localhost:60000')
  80. $StatusCode = $HTTP_Request.GetResponse().StatusCode
  81. echo "Hms_1 $StatusCode"
  82. if ($StatusCode -ne "OK")
  83. {
  84.     throw "status = $StatusCode"
  85. }


  86. <# 刪除舊檔案 #>
  87. $oldFiles = get-childitem $backupDirPath
  88. foreach ($file in $oldFiles)
  89. {
  90.   if($file.FullName -ne $backupPath)
  91.   {
  92.       echo "delete item: " ($file.FullName)
  93.       Remove-Item -Path $file.FullName -Confirm:$false -Force -Recurse
  94.   }
  95. }


  96. $oldDir = Split-Path -Path $zipPath
  97. $oldFiles = get-childitem $oldDir
  98. foreach ($file in $oldFiles)
  99. {
  100.     echo "delete item: " ($file.FullName)
  101.     Remove-Item $file.FullName -Confirm:$false -Force -Recurse
  102. }


  103. echo "delete item: C:\deploy\Expand"
  104. Remove-Item -Path C:\deploy\Expand -Confirm:$false -Force -Recurse

  105. echo "delete item: C:\deploy\Deploy.ps1"
  106. Remove-Item C:\deploy\Deploy.ps1 -Confirm:$false -Force -Recurse

  107. return true
*注1
远端前需要开启相关功能及设定,由gitlab执行时需要为服务"gitlab-runner"指定登入身份,否则可能会遇到无法远端连线的讯息。
阅读(308) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~