Git tags not propagating to steps running in a container

Hi,

I have a declarative pipeline that has a mixed agent definition. initial tasks run on a regular Jenkins agent and later task are defined to run within a docker container.

I noticed that in this setup, git tags do not seem to propagate into the container environment, which is where I need them. The pipeline looks something like the following stripped down example:

pipeline {
    agent any

    stages {
        stage('Checkout with tags') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: scm.branches,
                    extensions: scm.extensions + [[$class: 'CloneOption', noTags: false, reference: '', shallow: false]],
                    userRemoteConfigs: scm.userRemoteConfigs
                ])
            }
        }
        stage('on native agent') {
            steps {
                sh 'git tag -l'
                // tags listed without issue
            }
        }
        stage('in container') {
            agent {
                docker {
                    image 'some-image'
                    registryUrl 'some-registery'
                    registryCredentialsId 'some-credential'
                    args 'some args'
                }
            }
            steps {
                sh 'git tag -l'
                // tags not listed
            }
        }
    }
}

Only when I move the checkout step to be exectued within the container stage, do I see tags in that stage.

Is this expected behaviour? if so, how can I propagate git tags to the container?

Hello @ih016 and welcome to this community. :wave:

Yes, I’m afraid this is expected behavior. :person_shrugging:

As far as I know, when you switch to a Docker agent, you’re essentially moving into a different filesystem (the Docker container’s filesystem), which doesn’t have access to the Jenkins agent’s workspace where the initial git checkout occurred.

To propagate git tags to the Docker container, I think you may have two options:

  1. Perform the checkout step inside the Docker container. This will make sure that the git repository (including tags) is available within the Docker container’s filesystem.
  2. Use Docker’s volume mounting feature to mount the Jenkins workspace directory inside the Docker container. This will make the git repository available inside the Docker container.

Here’s how you could modify your pipeline to use the first option:

pipeline {
    agent any

    stages {
        stage('on native agent') {
            steps {
                sh 'git tag -l'
                // tags listed without issue
            }
        }
        stage('in container') {
            agent {
                docker {
                    image 'some-image'
                    registryUrl 'some-registery'
                    registryCredentialsId 'some-credential'
                    args 'some args'
                }
            }
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: scm.branches,
                    extensions: scm.extensions + [[$class: 'CloneOption', noTags: false, reference: '', shallow: false]],
                    userRemoteConfigs: scm.userRemoteConfigs
                ])
                sh 'git tag -l'
                // tags should now be listed
            }
        }
    }
}

And here’s how you could modify your pipeline to use the second option (untested):

pipeline {
    agent any

    stages {
        stage('Checkout with tags') {
            steps {
                checkout([
                    $class: 'GitSCM',
                    branches: scm.branches,
                    extensions: scm.extensions + [[$class: 'CloneOption', noTags: false, reference: '', shallow: false]],
                    userRemoteConfigs: scm.userRemoteConfigs
                ])
            }
        }
        stage('on native agent') {
            steps {
                sh 'git tag -l'
                // tags listed without issue
            }
        }
        stage('in container') {
            agent {
                docker {
                    image 'some-image'
                    registryUrl 'some-registery'
                    registryCredentialsId 'some-credential'
                    args '-v ${WORKSPACE}:${WORKSPACE}' // mount the Jenkins workspace inside the Docker container
                }
            }
            steps {
                sh 'git tag -l'
                // tags should now be listed
            }
        }
    }
}