Search code examples
gitjenkinsjenkins-pipelineclonegit-archive

Use git archive instead of clone with credentials in jenkins pipeline job


On my Jenkins pipeline job, I used to clone git repo at the begining of my job like that

cleanWs()
git branch: 'master', credentialsId: 'SSH_GIT', url: '[email protected]:myrepo'

Note that I need credentials.

In order to reduce time execution and data size downloaded, I would like to use git archive instead of git clone.

sh("git archive --format=tar --remote [email protected]:myrepo master |tar xf -")

The previous command works if I have proper ssh-key configuration done. It is not the case on my server, so I need to use credentials.

How can I archive instead of cloning repo, using Jenkins defined credentials ? Git is installed on a gitolite server


Solution

  • The solution is to use sshagent function in Jenkins Pipeline

    sshagent(['SSH_JENKINS_CREDENTIALS_FOR_GIT']) {
        sh("git archive --format=tar --remote [email protected]:myrepo master |tar xf -")
    }