简体   繁体   中英

Git checkout in Jenkins pipeline misses commit's tag

One of my Jenkins pipeline jobs should consider a tag on the commit that is currently build. But it seems the tag on the topmost commit is not available at build time.

Let's say I have a simple history with three commits:

 my_commit_to_build (tagged v2)
            |
            v
   another_commit (tagged v1)
            |
            v
      initial_commit

My pipeline script contains this checkout-step:

checkout(
    changelog: false,
    poll: false,
    scm: [
        $class           : 'GitSCM',
        userRemoteConfigs: [
            [
                url          : <SCM_URL>,
                credentialsId: <CREDENTIALS_ID>,
                refspec      : <REFSPEC_TO_BUILD>
            ]
        ],
        branches         : [
            [
                name: <REFSPEC_TO_BUILD>
            ]
        ],
        extensions       : [
            [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']],
            [$class: 'LocalBranch', localBranch: <LOCAL_BRANCH_NAME>],
            [$class: 'CloneOption', depth: 0, noTags: false, reference: '', shallow: false],
            [$class: 'PruneStaleBranch'],
            [$class: 'CleanCheckout']
        ]
    ]
)

This results in the folowing commands run by the job:

Cloning the remote Git repository
Cloning repository <SCM_URL>
> git init /home/jenkins/workspace/<WORKSPACE> # timeout=10
Fetching upstream changes from <SCM_URL>
> git --version # timeout=10
using GIT_SSH to set credentials SSH Key for <USER>
> git fetch --tags --progress <SCM_URL> +refs/heads/*:refs/remotes/origin/*
> git config remote.origin.url <SCM_URL> # timeout=10
> git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
> git config remote.origin.url <SCM_URL> # timeout=10
Pruning obsolete local branches
Fetching upstream changes from <SCM_URL>
using GIT_SSH to set credentials SSH Key for <USER>
> git fetch --tags --progress <SCM_URL> --prune
> git rev-parse <REV>^{commit} # timeout=10
Checking out Revision <REV> (<LOCAL_BRANCH_NAME>)
> git config core.sparsecheckout # timeout=10
> git checkout -f <REV>
> git branch -a -v --no-abbrev # timeout=10
> git checkout -b <LOCAL_BRANCH_NAME> <REV>
Commit message: "my_commit_to_build"
Cleaning workspace
> git rev-parse --verify HEAD # timeout=10
Resetting working tree
> git reset --hard # timeout=10
> git clean -fdx # timeout=10

When I now check the local copy of the repo for the tag on the topmost commit and the last commit found in the history, I get the following:

> git --no-pager tag -l --points-at=HEAD
> git describe --abbrev=0 --tags
v1
> ...

I already tried removing the prune from the fetch command and the sparse checkout, but both didn't help (I hadn't any hope with those two anyway...).

Has anyone any idea how I should adapt my checkout step, so that the tag is available when building?

Thanks in advance!

This would probably be better as a comment but I can't add them yet.

I tested on a similar repo with the same extensions except for BuildChooserSetting and it worked as expected.

Are you sure the branch you're checking out contains that tag? Also, what is the output of git tag ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM