一般來說在軟體交付的過程中,開發的環境可能會有
1.開發環境( DEV )
2.測試環境 ( QAT )
3.使用者測試環境( UAT )
4.生產環境 ( PROD )
當專案越來越多時,多個環境,越複雜時,過去的手工佈署應用程式,就可能是件麻煩事,且人工佈署很有可能因不小心,造成環境部署錯誤的問題,那麼為了解決這個問題及節省時間,而需要導入 CICD 工具,達到自動建置及自動佈署
在軟體工程中,CI/CD或CICD通常指的是持續集成和持續交付或持續部署的組合實踐。CI/CD通過在應用程式的構建、測試和部署中實施自動化,在開發和運營團隊之間架起了橋樑。 現代DevOps實踐涉及軟體應用程式在整個開發生命周期內的持續開發、持續測試、持續集成、持續部署和持續監控。 (維基百科)
Git Commit > Trigger build stage> Build docker image using powershell
Manual deploy > Trigger deploy stage > Notify User “Deployment Start” > Deploy docker image to container server > Notify User “Deploy end”
choco install gitlab-runner gitlab-runner --version // 測試版本
gitlab-runner register
gitlab-runner run
stages: - build - deploy build: stage: build script: - ./build.ps1 # build docker image only: - main deploy: stage: deploy script: - ./tg-notify.ps1 "deploy start" # telegram bot 通知部署開始 - ./linebot-notify.ps1 "deploy start" # line bot 通知部署開始 - ./deploy.ps1 # deploy docker image to container - ./tg-notify.ps1 "deploy end" # telegram bot 通知部署結束 - ./linebot-notify.ps1 "deploy end" # line bot 通知部署結束 when: manual default: interruptible: true # 新的流水線工作建立時,目前的這工作是否繼續執行(預設為 false) tags: - test
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $botToken = 'your token' $chatID = 'your chatId' $messageText = $args[0] $telegramURI = ("https://api.telegram.org/bot" + $botToken + "/sendMessage") $telegramJson = ConvertTo-Json -Compress @{chat_id = $chatID; text=$messageText} $telegramResponse = Invoke-RestMethod -Uri $telegramURI -Method Post -ContentType 'application/json;charset=utf-8' -Body $telegramJson
$token = your token $toUserId = linebotUserid $Header = @{ 'Authorization' = 'Bearer $token' } $postParams = @" { "to": "$toUserId", "messages": [ { "text": "$args", "type": "text" } ] } "@ Invoke-WebRequest -Uri https://api.line.me/v2/bot/message/push -Method POST -ContentType 'application/json' -Headers $Header -Body $postParams
$containerUrl = "tcp://192.168.50.52:2376" $imageName = "shopcart" $dockerfile = "./Comma.Web/Dockerfile" $outputPath = "./" $imageFilePath = $outputPath + $imageName $port="8888:80" # 本地建置映像檔 docker build -t $imageName . -f $dockerfile docker save -o $imageFilePath $imageName # save 要搭配 load ; import 搭配 export
$containerName = "shopcart-1" $containerUrl = "tcp://192.168.20.20:2376" $imageName = "shopcart" $outputPath = "./" $imageFilePath = $outputPath + $imageName $port="8888:80" # 停用容器 docker --tls -H="$containerUrl" ps -a -f ancestor=$containerName --no-trunc -q | foreach-object { docker --tls -H="$containerUrl" stop $_ } docker --tls -H="$containerUrl" ps -a -f name=$containerName --no-trunc -q | foreach-object { docker --tls -H="$containerUrl" stop $_ } # 移除容器 docker --tls -H="$containerUrl" ps -a -f ancestor=$containerName* --no-trunc -q | foreach-object { docker --tls -H="$containerUrl" rm -f $_ } docker --tls -H="$containerUrl" ps -a -f name=$containerName* --no-trunc -q | foreach-object { docker --tls -H="$containerUrl" rm -f $_ } # 移除映像檔 $existingImages = docker --tls -H="$containerUrl" images $imageName If ($existingImages.count -gt 1) { write-host "[Removing image]Removing the existing image.." docker --tls -H="$containerUrl" rmi -f $imageName } else { write-host "[Removing image]The image does not exist" } # 將本地的映像檔匯入 Docker 主機 docker --tls -H="$containerUrl" load --input $imageFilePath # 建立及啟動容器應用 docker --tls -H="$containerUrl" run -d --name $containerName --restart=always -p $port $imageName
Gitlab Pipelines 真是個好東西,且完全免費,我一台伺服器都沒架,雲端Gitlab,搭配地端的 Runner ,就讓我輕易的做到 CICD ,並讓 Line Bot 通知相關人員,應用程式正在部署程式。