上次我們講到 CI/CD 中的 when 使用,今天我們要來傳授一套必殺技,我們要把過去所學的全部融合再一起,相信各位都準備好了,將 Gitlab 從枷鎖中解放出來吧,讓我們來詠唱「武裝完全支配術」
「System Call. Connect Armament. Search Archived Memory. Extract Core Image. Connect Resource. Materialize Primary Shape. Append Maximal Solidity.」
在開始寫 CI/CD 自動化佈署腳本前,我們先定義好 CI/CD 設置流程,在這裡先做個簡單有些完整性的五個階段,在下面我們一一說明在五個階段的用處
接下來我們開始撰寫 CI/CD 佈署計劃書,我們將上面五個工作階段結合起來,我們在 develop 這個分支,在有 push 的時候,擁有單元測試、打包 Docker Image、佈署開發機、佈署 demo 機
而在 main 這個分支,在有 push 的時候,擁有單元測試、打包 Docker Image、佈署正式機,現在我們開始強化撰寫 CI/CD 佈署計劃書 Enhance Armament !!!
image: mcr.microsoft.com/dotnet/sdk:6.0
stages:
- testing
- build
- deploy
unit-tests:
stage: testing
tags:
- groupdocker
script:
- dotnet test DotNetCoreWebApiCiAndCdCourse.Tests > DotNetCoreWebApiCiAndCdCourse.Tests.txt
artifacts:
paths:
- DotNetCoreWebApiCiAndCdCourse.Tests.txt
expire_in: "30 days"
only:
- main
- develop
build-docker-image:
stage: build
tags:
- shell
before_script:
- echo $DOCKER_REGISTRY_PASSWORD | docker login -u $DOCKER_REGISTRY_USERNAME --password-stdin
- export IMAGE_VERSION=$(cat ./package.json | jq -r .version)
script:
- docker build -t $DOCKER_REGISTRY_USERNAME/dotnetcorewebapiciandcdcourse:latest .
- docker push $DOCKER_REGISTRY_USERNAME/dotnetcorewebapiciandcdcourse:latest
- docker build -t $DOCKER_REGISTRY_USERNAME/dotnetcorewebapiciandcdcourse:$IMAGE_VERSION .
- docker push $DOCKER_REGISTRY_USERNAME/dotnetcorewebapiciandcdcourse:$IMAGE_VERSION
needs:
- unit-tests
only:
- main
- develop
.deploy:
tags:
- shell
variables:
SERVER_NAME: ""
SERVER_PRIVATE_KEY: ""
SERVER_USER_NAME: ""
SERVER_URL: ""
SERVER_PORT: ""
SERVER_ENV: ""
before_script:
- eval $(ssh-agent -s)
- ssh-add <(echo "$SERVER_PRIVATE_KEY")
- echo $DOCKER_REGISTRY_PASSWORD | docker login -u $DOCKER_REGISTRY_USERNAME --password-stdin
script:
- ssh -o StrictHostKeyChecking=no $SERVER_USER_NAME@$SERVER_URL "
mkdir -p ~/DotNetCoreWebApiCiAndCdCourse"
- scp -o StrictHostKeyChecking=no ./docker-compose.yml $SERVER_USER_NAME@$SERVER_URL:~/DotNetCoreWebApiCiAndCdCourse
- ssh -o StrictHostKeyChecking=no $SERVER_USER_NAME@$SERVER_URL "
export COMPOSE_PROJECT_NAME=$SERVER_ENV
export SERVER_ENV=$SERVER_ENV
export SERVER_PORT=$SERVER_PORT
cd ~/DotNetCoreWebApiCiAndCdCourse &&
docker compose up --build -d"
environment:
name: $SERVER_NAME
url: http://$SERVER_URL:$SERVER_PORT
deploy-to-development:
stage: deploy
tags:
- shell
variables:
SERVER_NAME: DotNetCoreWebApiCiAndCdCourse development environment
SERVER_PRIVATE_KEY: $DEV_SERVER_PRIVATE_KEY
SERVER_USER_NAME: $DEV_SERVER_USER_NAME
SERVER_URL: $DEV_SERVER_URL
SERVER_PORT: 8080
SERVER_ENV: development
extends: .deploy
needs:
- build-docker-image
only:
- develop
deploy-to-staging:
stage: deploy
tags:
- shell
variables:
SERVER_NAME: DotNetCoreWebApiCiAndCdCourse staging environment
SERVER_PRIVATE_KEY: $DEV_SERVER_PRIVATE_KEY
SERVER_USER_NAME: $DEV_SERVER_USER_NAME
SERVER_URL: $DEV_SERVER_URL
SERVER_PORT: 8081
SERVER_ENV: staging
extends: .deploy
needs:
- deploy-to-development
only:
- develop
deploy-to-production:
stage: deploy
tags:
- shell
variables:
SERVER_NAME: DotNetCoreWebApiCiAndCdCourse production environment
SERVER_PRIVATE_KEY: $DEV_SERVER_PRIVATE_KEY
SERVER_USER_NAME: $DEV_SERVER_USER_NAME
SERVER_URL: $DEV_SERVER_URL
SERVER_PORT: 80
SERVER_ENV: production
extends: .deploy
when: manual
needs:
- build-docker-image
only:
- main
接下來我們開始把這個 CI/CD 佈署計劃書上傳到 Gitlab,將 Gitlab 淺藏在枷鎖裡的力量解放出來吧 Release Recollection !!!
首先可以看到我們在 develop 分支裡面,當有 push 的時候,做了單元測試、打包 Docker Image、佈署開發機、佈署 demo 機
緊接著我們將這個 CI/CD 佈署計劃書,佈署到 main 分支上,可以看到當 main 分支有 push 的時候,單元測試、打包 Docker Image、佈署正式機