一、背景
我們使用云效來部署程序,省去了許多基礎(chǔ)搭建工作,但是也帶來了一些問題,比如程序的版本號(hào)、打包時(shí)間、啟動(dòng)時(shí)間等元數(shù)據(jù)的展示,需要我們自己去實(shí)現(xiàn)。
像jvm內(nèi)存、一些三方的密鑰,是可以在環(huán)境變量里配置,但是像版本號(hào)是會(huì)隨著程序的發(fā)版而變化的。
而讀取程序的版本,是可以從endpoints的/info獲取,但那是程序已啟動(dòng)的情況下。顯然并不適用于我們的需求,所以我們得在編譯打包的時(shí)候輸出,辦法是有很多,我們這里采用的maven插件 git-commit-id-plugin,默認(rèn)會(huì)在target/classes目錄下生成git.properties文件。
在spring boot框架下,你還可以使用'@project.version@'生成程序的版本號(hào),類似git-commit-id-plugin插件,還得把版本寫入到某個(gè)文件里。
二、目標(biāo)
- 1、在k8s容器,能看到程序的版本號(hào)
- 2、在nacos服務(wù)注冊(cè)中心,能看到程序的版本號(hào)(在后面的文章中講述)
- 3、構(gòu)建Docker鏡像按appName:${version},部署時(shí)候拉取的鏡像也是根據(jù)版本號(hào)。
三、DevOps流水線示意圖

image.png
四、優(yōu)化后的流水線

image.png

image.png
java編譯

image.png
讀取程序版本號(hào)

image.png
# input your command here
version="`grep 'git.build.version' target/classes/git.properties | cut -d'=' -f2 | sed 's/\r//'`"
echo "USER_version=$version" > .env
也可濃縮為一句shell命令
echo USER_version=`grep 'git.build.version' target/classes/git.properties | cut -d'=' -f2 | sed 's/\r//'` > .env
鏡像制作

image.png
這里會(huì)傳入兩個(gè)環(huán)境變量,跟Dockerfile有關(guān):
FROM openjdk:8u201-jdk-alpine3.9
ARG APPNAME
COPY target/${APPNAME}*.jar /opt/xxx_service/${APPNAME}/packages/${APPNAME}.jar
ARG PORT
EXPOSE ${PORT}/tcp
RUN mkdir -p /opt/xxx_service/${APPNAME}/dump
補(bǔ)充一點(diǎn),啟動(dòng)jar的命令和參數(shù),配置在deployment.yaml中。
k8s發(fā)布

image.png

image.png
關(guān)于manifests目錄下的yaml,后面的文章有空再補(bǔ)充。
五、新的DevOps流程示意圖

image.png
六、K8S部署界面

image.png
七、環(huán)境變量
開發(fā)人員配置

image.png
運(yùn)維管理人員配置

image.png
八、打包生成的git.properties文件
#Generated by Git-Commit-Id-Plugin
#Tue Feb 14 15:12:20 CST 2023
git.build.time=2023-02-14T15\:12\:20+0800
git.build.version=5.0.6
git.commit.id.abbrev=e316e29
git.commit.id.full=e316e290ed8f7ec322d9d4c69e49f10dd1b0705f
九、maven插件:git-commit-id-plugin
<plugin>
<groupId>pl.project13.maven</groupId>
<artifactId>git-commit-id-plugin</artifactId>
<version>${git-commit-id-plugin.version}</version>
<executions>
<execution>
<goals>
<goal>revision</goal>
</goals>
</execution>
</executions>
<configuration>
<verbose>true</verbose>
<dateFormat>yyyy-MM-dd'T'HH:mm:ssZ</dateFormat>
<generateGitPropertiesFile>true</generateGitPropertiesFile>
<generateGitPropertiesFilename>${project.build.outputDirectory}/git.properties
</generateGitPropertiesFilename>
<includeOnlyProperties>
<includeOnlyProperty>^git.build.(time|version)$</includeOnlyProperty>
<includeOnlyProperty>^git.commit.id.(abbrev|full)$</includeOnlyProperty>
</includeOnlyProperties>
<commitIdGenerationMode>full</commitIdGenerationMode>
<failOnNoGitDirectory>false</failOnNoGitDirectory>
</configuration>
</plugin>