How to make jenkins checkout code into a separate directory, without the .git folder?

430 Views Asked by At

I'm a very new Jenkins user. I am running the latest stable jenkins version, installed on Windows.

I have a pipeline job that checks out code to our test site when someone makes a commit. Right now the step of copying files from the workspace to the test site takes several minutes. I'd like to speed up the feedback loop of committing to the test branch and then seeing your work show up on the test site. So I am working on a new job, which I've pasted below.

The git checkout itself is very fast. Just over 1 minute for the initial clone/checkout, and then just seconds for subsequent checkouts. So I would like to checkout directly to the test site. I know I can use ws() or dir(), but that will also put the .git folder there, which I don't want. So I am trying to set the GIT_WORK_TREE, which should cause the files to be placed separately from the .git folder. But I can't seem to pass GIT_WORK_TREE to the jenkins/git checkout. Is this possible? Below is my code. You'll see I've attempted in two places to set the GIT_WORK_TREE variable, but it doesn't cause git to checkout to that folder.

My dir command at the end confirms that the environment variable is getting set as I expect.

I later added the withEnv() just to see if that helped, but it did not.

I also tried

extensions: [[[$class: 'RelativeTargetDirectory', relativeTargetDir: 'C:\\Inetpub\\wwwroot']]]

But this places both the work tree and the .git folder in the c:\Inetpub\wwwroot directory.

Thanks for any help.

pipeline {
agent any
environment {
    GIT_WORK_TREE='C:\\Inetpub\\wwwroot'
}
stages {
    stage('checkout') {
        steps {
            withEnv(['GIT_WORK_TREE="C:\\Inetpub\\wwwroot"']) {
                checkout(
                [
                $class: 'GitSCM',
                branches: [[name: 'refs/heads/test_branch']],
                extensions: [],
                userRemoteConfigs: [
                    [credentialsId: 'bit_bucket_ssh_credentials', url: 'ssh://git@myBitBucketServer/web/wwwroot.git']
                    ]
                ]
                )
            }
        }
    }
    stage('test the env variable') {
        steps {
            bat 'dir %GIT_WORK_TREE%'
            println "All done"
        }
    }
}

}

1

There are 1 best solutions below

4
On

You could allocate your workspace directly on the test site folder, instead of %AGENT_ROOT%.

Use ws: Allocate workspace.

stages {
    stage('checkout') {
        steps {
            ws('/path/to/site') {
               ... checkout

As an alternative, as explained in JENKINS-24502 and illustrated here, you can checkout a repository in a subfolder of the workspace.

If said subfolder is a junction to C:\Inetpub\wwwroot, that might work.
As a pre-step:

mklink /J C:\path\to\workspace\wwwroot C:\Inetpub\wwwroot

Then this would checkout the repository in the wwwroot of the Jenkins workspace (again, with wwwroot pointing to C:\Inetpub\wwwroot)

dir('wwwroot') {
    checkout scm
}