定義場(chǎng)景
spring boot應(yīng)用在手動(dòng)部署過(guò)程中主要分為兩步禀酱,打包編譯與上傳部署(也可能存在測(cè)試環(huán)境矛洞,但由于該項(xiàng)目目前只有我開(kāi)發(fā)为流,并未寫測(cè)試用例)航攒。同樣的在gitlab-ci文件中也定義相應(yīng)的兩步驟
stages:
- build
- deploy
編譯
使用GitLab公共CI服務(wù)為Spring Boot打包編譯磺陡。image定義所使用的docker鏡像,stage指定場(chǎng)景屎债,script打包腳本(跳過(guò)測(cè)試階段)仅政,artifacts定義需要?dú)w檔的文件,only指定僅release分支打包(支持正則表達(dá)式)
release-build:
image: maven:3.5.2-jdk-8
stage: build
script: "mvn package -B -Dmaven.test.skip=true"
artifacts:
paths:
- target/bill-server.jar
only:
- /^release-.*$/
部署
配置文件的結(jié)構(gòu)與編譯的基本一直盆驹,這里使用ubuntu鏡像圆丹,如果您使用的yam請(qǐng)?zhí)鎿Q相應(yīng)的命令。如果您使用自己的CI Runner(shell)躯喇,建議密鑰存放在自己的服務(wù)器上辫封,手動(dòng)配置實(shí)現(xiàn)對(duì)部署服務(wù)器的ssh訪問(wèn)
release-deploy:
image: ubuntu
stage: deploy
script:
- 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client git -y )'
- eval $(ssh-agent -s)
##
## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
## We're using tr to fix line endings which makes ed25519 keys work
## without extra base64 encoding.
## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556
##
# Generate the private/public key pair using:
#
# ssh-keygen -f deploy_key -N ""
#
# then set the $SSH_PRIVATE_KEY environment variable in the CI (Travis-CI,
# GitLab-CI, ...) to the base64 encoded private key:
#
# cat deploy_key | base64 -w0
#
# and add the public key `deploy_key.pub` into the target git repository (with
# write permissions).
- ssh-add <(echo "$SSH_PRIVATE_KEY" | base64 --decode)
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan 119.28.1.107 >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
- scp target/bill-server.jar ubuntu@119.28.1.107:bill
- ssh ubuntu@119.28.1.107 "cd bill; sh bill-start.sh"
only:
- /^release-.*$/
其中shell腳本,與我的另一篇博客類似廉丽,稍做修改就行
參考
??Mine Site:https://www.dnocm.com/articles/almond/spring-boot-autodeploy-with-gitlab/