Jenkins groovy-如何从最新提交中检索标签? [英] Jenkins groovy - How to retrieve tag from latest commit?

查看:149
本文介绍了Jenkins groovy-如何从最新提交中检索标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要从branchName获取最新提交,我们运行以下代码:

To fetch the latest commit from branchName, we run below code:

treeMapData = git(branch: branchName, credentialsId: credential, url: "${gitLabServer}/${projectName}/${repo}.git")


根据我们的工作流程,确保每次提交都包含一个标签


It is ensured that there is one tag per commit, as per our workflow

仅当提交被标记时,我们才希望构建代码.

We want to build the code, only if the commit is tagged.

如何检索最近一次提交的标签名称?

How to retrieve the tag name for that latest commit?

推荐答案

如果Jenkins还没有,我们可以从仓库中获取标签.

We can fetch the tags from the repo in case Jenkins hasn't already.

git fetch --tags

在我们的例子中,我们需要找到一个指向特定提交或HEAD的标签.幸运的是,在git中有一个方便的命令可以使我们做到这一点.

We need to find a tag(s) which point to a specific commit or HEAD in our case. Thankfully there is a handy command in git which allows us to do this.

git tag --points-at HEAD

使用awk,我们可以将其转换为常规的伪造输出.

Using awk we can turn this into an output which groovy can falsify.

awk NF


因此,我们首先检查所推送的分支是否为master


So we, first we check if the pushed branch is master

if (env.BRANCH_NAME == 'master') {

将其锁定

  lock('publish master') {

执行git tag shell脚本并将其分配给TAG

execute the git tag shell script and assign it to TAG

    TAG = sh (
      returnStdout: true,
      script: 'git fetch --tags && git tag --points-at HEAD | awk NF'
    ).trim()

如果存在标签,请执行一些操作!

if a tag exists, do something!

    if (TAG) {
      stage('Deploy Prod') {
        echo "Deploying to Prod ${TAG}"
      }
    }


希望这能回答您的问题,或者至少可以使您走上正确的轨道.


Hopefully this answers your question, or at the very least will get you on the right track.

这篇关于Jenkins groovy-如何从最新提交中检索标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆