Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipelines - git checkout to folder in workspace without wipe

I would like to check out my deployment scripts which reside in separate repo to the deployment folder in my workspace. This folder already has some files in it, and the problem is that during the checkout of the scripts the folder gets cleared. I need a way to keep these files.

The code used for checkout: ...

steps{ checkout( [$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [[$class: 'RelativeTargetDirectory', relativeTargetDir: 'deployment']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'secret', url: 'https://bitbucket.org/secret/deploy_scripts.git']]]) }

Any help will be appreciated!

like image 470
Sergio Avatar asked Jan 07 '18 19:01

Sergio


People also ask

How do I clean my workspace before building Jenkins pipeline?

There is a way to clean up a workspace in Jenkins. You need to install the Workspace Cleanup Plugin. This plugin can clean up the workspace before build or after a build. Under Build Environment, check the box that says Delete workspace before build starts.

How do I clean up my workspace in Jenkins pipeline?

Login to Jenkins, click on “Manage Jenkins” > “Manage Plugins” > Click on the “Available” tab then search for “workspace cleanup“. You will see various plugins listed. Click on the checkbox for “Workspace Cleanup“, plugin then click on install without reboot tab below the page.

What is cleanWs () in Jenkins?

cleanWs : Delete workspace when build is done.


2 Answers

What about changing folder for checkout step?

like this [tested]:

node(){
    stage("checkout"){
        // `dir` step will create folder in workspace, if it is not exist
        dir('speacial_folder_for_git'){
            //git checkout here
            checkout changelog: false, poll: false, scm: [$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'alexkh_git_credentials', url: 'http://localhost:7990/scm/tes/mytestrepo.git']]]
        }
        // change current directory back, to workspace:
        dir('')
        {
            //do your job out of git folder
        }
    }
}

Files from git checkout will be placed in \speacial_folder_for_git\ into your workspace, while other files outside this folder (but inside workspace) remains untouched

like image 120
Sysanin Avatar answered Oct 17 '22 01:10

Sysanin


It could make sense to stash a content of the directory before fetching:

steps{
    sh "mkdir -p deployment;touch deployment/test"
    stash name: "deployment", includes: "deployment/**"

    checkout(
            [$class: 'GitSCM', 
            branches: [[name: '*/master']], 
            doGenerateSubmoduleConfigurations: false, 
            extensions: [[$class: 'RelativeTargetDirectory', 
            relativeTargetDir: 'deployment']], 
            submoduleCfg: [], 
            userRemoteConfigs: [[credentialsId: 'secret', url: 'https://bitbucket.org/secret/deploy_scripts.git']]]) 

    unstash deployment
    sh "ls deployment"
    # it prints fetched repo AND existing before files 
}
like image 44
spikalev Avatar answered Oct 17 '22 00:10

spikalev