基于Git参数构建
之前是默认拉取最新提交代码构建,实际中不适用,需要通过打标签选择发布相应版本
打开Jenkins任务设置,勾选参数化构建过程 > 选择Git参数(这里是Git Parameter插件的作用)
设置标识名称(记住它,下面的构建步骤需要添加设置这个变量),描述随意,参数类型基于标签
默认值:需要注意,默认是master分支,我这是mian修改相应即可。
滑到 Build Steps 设置,添加一个构建步骤,选择执行Shell命令方式
然后按紧三横图标拖动到第一步最上面位置来
设置命令,切换到指定版本
git checkout $version
保存之后会发现,立即构建按钮变成了Build with Parmeters
右侧出现了一个我们设置的version,框里空白的,因为我们还没去Gitlab仓库打标签版本
Gitlab打版
进入代码仓库中点击"Tags"
点右上角的"New Tag"
设置标签名字,例如v1.0.0
修改项目代码并推送到Gitlab仓库
推送成功后,再进行打标签动作,例如v1.0.1
这时候我们已经打了2个标签了,代码不同处就是v1.0.1版本页面多了个v1.0.1文字标识
Jenkins构建
回到Jenkins任务构建页面,已经识别到了我们再Gitlab打的2个标签
直接选择相应版本发版即可,当前页面本来就是v1.0.0的代码,所以这里我直接选择v1.0.1标签发版
Started by user 我是真滴帅
Running as SYSTEM
Building in workspace /var/jenkins_home/workspace/mytest
The recommended git tool is: NONE
No credentials specified
> git rev-parse --resolve-git-dir /var/jenkins_home/workspace/mytest/.git # timeout=10
Fetching changes from the remote Git repository
> git config remote.origin.url http://192.168.170.111:8888/root/jenkins-cicd-mytest.git # timeout=10
Fetching upstream changes from http://192.168.170.111:8888/root/jenkins-cicd-mytest.git
> git --version # timeout=10
> git --version # 'git version 2.30.2'
> git fetch --tags --force --progress -- http://192.168.170.111:8888/root/jenkins-cicd-mytest.git +refs/heads/*:refs/remotes/origin/* # timeout=10
> git rev-parse refs/remotes/origin/main^{commit} # timeout=10
Checking out Revision 971357ca89845b6f57327412c0975cbcc557e98c (refs/remotes/origin/main)
> git config core.sparsecheckout # timeout=10
> git checkout -f 971357ca89845b6f57327412c0975cbcc557e98c # timeout=10
Commit message: "修改index.html内容,用于区别v1.0.0"
> git rev-list --no-walk f1b9b50fb0e2b57a42510dd5eafb91741794cb8b # timeout=10
[mytest] $ /bin/sh -xe /tmp/jenkins13022689792363235212.sh
+ git checkout v1.0.1
HEAD is now at 971357c 修改index.html内容,用于区别v1.0.0
[mytest] $ /var/jenkins_home/tools/hudson.tasks.Maven_MavenInstallation/maven/bin/mvn clean package -DskipTests
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.monster:mytest >-------------------------
[INFO] Building mytest 0.0.1-SNAPSHOT
[INFO] from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] Parameter 'archive' is unknown for plugin 'spring-boot-maven-plugin:2.6.13:repackage (repackage)'
[INFO]
[INFO] --- clean:3.2.0:clean (default-clean) @ mytest ---
[INFO] Deleting /var/jenkins_home/workspace/mytest/target
[INFO]
[INFO] --- resources:3.3.1:resources (default-resources) @ mytest ---
[INFO] Copying 2 resources from src/main/resources to target/classes
[INFO]
[INFO] --- compiler:3.8.1:compile (default-compile) @ mytest ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 4 source files to /var/jenkins_home/workspace/mytest/target/classes
[INFO]
[INFO] --- resources:3.3.1:testResources (default-testResources) @ mytest ---
[INFO] skip non existing resourceDirectory /var/jenkins_home/workspace/mytest/src/test/resources
[INFO]
[INFO] --- compiler:3.8.1:testCompile (default-testCompile) @ mytest ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 1 source file to /var/jenkins_home/workspace/mytest/target/test-classes
[INFO]
[INFO] --- surefire:3.2.2:test (default-test) @ mytest ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- jar:3.3.0:jar (default-jar) @ mytest ---
[INFO] Building jar: /var/jenkins_home/workspace/mytest/target/mytest.jar
[INFO]
[INFO] --- spring-boot:2.6.13:repackage (repackage) @ mytest ---
[INFO] Replacing main artifact with repackaged archive
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 3.003 s
[INFO] Finished at: 2024-03-13T02:43:47Z
[INFO] ------------------------------------------------------------------------
SSH: Connecting from host [3821aea39450]
SSH: Connecting with configuration [应用服务器-170.111] ...
SSH: EXEC: completed after 16,609 ms
SSH: Disconnecting configuration [应用服务器-170.111] ...
SSH: Transferred 3 file(s)
Build step 'Send files or execute commands over SSH' changed build result to SUCCESS
Finished: SUCCESS
发版成功,页面内容多了个【v1.0.1】
如果发布v1.0.1版本发现有大量bug,可以通过Jenkins快速切换v1.0.0回滚上个版本