自动创建Git标签

需求 想要jenkins每次构建项目成功后给Git仓库打一个tag,以便下次构建出现问题可以及时通过tag恢复到上一个版本

  1. 创建一个Jenkins任务
  2. 配置《源码管理》,这里配置比较特殊的地方需要在高级选项中配置仓库的名字,后面创建tag的时候需要使用;
    在这里插入图片描述
  3. 在《构建后操作》操作中选择"Git Publisher",来配置发布后执行的任务。
    在这里插入图片描述
  4. 配置完成,保存配置信息,点击立即构建进行项目编译;
  5. 从控制台打印的log我们可以看到我们的tag已经生成 在这里插入图片描述

我遇到的坑

在刚开始研究使用Jenkins自动打tag时候一直不成功,查了好久也没找到原因,报如下错误:

using credential 640821c9-e50e-43f3-9747-773408bff018
 > git.exe tag -l v1.0.1 # timeout=10
 > git.exe tag -a -f -m Jenkins自动化生成Tag v1.0.1 # timeout=10
ERROR: Failed to push tag v1.0.1 to pig
hudson.plugins.git.GitException: Could not apply tag v1.0.1
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.tag(CliGitAPIImpl.java:1531)
	at hudson.plugins.git.GitAPI.tag(GitAPI.java:274)
	at hudson.plugins.git.GitPublisher.perform(GitPublisher.java:277)
	at hudson.tasks.BuildStepMonitor$3.perform(BuildStepMonitor.java:45)
	at hudson.model.AbstractBuild$AbstractBuildExecution.perform(AbstractBuild.java:744)
	at hudson.model.AbstractBuild$AbstractBuildExecution.performAllBuildSteps(AbstractBuild.java:690)
	at hudson.maven.MavenModuleSetBuild$MavenModuleSetBuildExecution.post2(MavenModuleSetBuild.java:1073)
	at hudson.model.AbstractBuild$AbstractBuildExecution.post(AbstractBuild.java:635)
	at hudson.model.Run.execute(Run.java:1841)
	at hudson.maven.MavenModuleSetBuild.run(MavenModuleSetBuild.java:543)
	at hudson.model.ResourceController.execute(ResourceController.java:97)
	at hudson.model.Executor.run(Executor.java:429)
Caused by: hudson.plugins.git.GitException: Command "git.exe tag -a -f -m Jenkins自动化生成Tag v1.0.1" returned status code 128:
stdout: 
stderr: 
*** Please tell me who you are.

Run

  git config --global user.email "you@example.com"
  git config --global user.name "Your Name"

to set your account's default identity.
Omit --global to set the identity only in this repository.

fatal: unable to auto-detect email address (got 'SYSTEM@*********.(none)')

	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2042)
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2010)
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommandIn(CliGitAPIImpl.java:2006)
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommand(CliGitAPIImpl.java:1638)
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.launchCommand(CliGitAPIImpl.java:1650)
	at org.jenkinsci.plugins.gitclient.CliGitAPIImpl.tag(CliGitAPIImpl.java:1529)
	... 11 more
Build step 'Git Publisher' marked build as failure
Finished: FAILURE

之前遇到过Jenkins执行scp命令报 scp 不是内部或外部命令,也不是可运行的程序,或批处理文件。 的错误,但是打开Dos窗口执行scp命令就可以正常执行,经过一些文章查找,有看到过是账户权限问题,对于上面的错误,我也怀疑是这个问题所致,所以做如下操作:

注:本人Jenkins是放在自己开发机上,是Windows环境

  1. 打开系统服务,找到Jenkins服务 在这里插入图片描述
  2. 右单击Jenkins服务->属性->登入 选择此账户->浏览->高级->立即查找,选择具有管理员权限的账户,输入正确的密码,点击确定,回到服务列表页面,重启Jenkins服务
    在这里插入图片描述
  3. 至此问题解决!
Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐