一喧枷、背景
我們使用云效來部署程序,省去了許多基礎搭建工作,但是也帶來了一些問題割去,比如程序的版本號窟却、打包時間、啟動時間等元數(shù)據(jù)的展示呻逆,需要我們自己去實現(xiàn)夸赫。
像jvm內存、一些三方的密鑰咖城,是可以在環(huán)境變量里配置茬腿,但是像版本號是會隨著程序的發(fā)版而變化的。
而讀取程序的版本宜雀,是可以從endpoints的/info獲取切平,但那是程序已啟動的情況下。顯然并不適用于我們的需求辐董,所以我們得在編譯打包的時候輸出悴品,辦法是有很多,我們這里采用的maven插件 git-commit-id-plugin简烘,默認會在target/classes目錄下生成git.properties文件苔严。
在spring boot框架下,你還可以使用'@project.version@'生成程序的版本號孤澎,類似git-commit-id-plugin插件届氢,還得把版本寫入到某個文件里。
二覆旭、目標
- 1退子、在k8s容器,能看到程序的版本號
- 2型将、在nacos服務注冊中心寂祥,能看到程序的版本號(在后面的文章中講述)
- 3、構建Docker鏡像按appName:${version}七兜,部署時候拉取的鏡像也是根據(jù)版本號壤靶。
三、DevOps流水線示意圖
image.png
四惊搏、優(yōu)化后的流水線
image.png
image.png
java編譯
image.png
讀取程序版本號
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án)境變量贮乳,跟Dockerfile有關:
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
補充一點,啟動jar的命令和參數(shù)恬惯,配置在deployment.yaml中向拆。
k8s發(fā)布
image.png
image.png
關于manifests目錄下的yaml,后面的文章有空再補充酪耳。
五浓恳、新的DevOps流程示意圖
image.png
六刹缝、K8S部署界面
image.png
七、環(huán)境變量
開發(fā)人員配置
image.png
運維管理人員配置
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>