Using a Jenkins Pipeline copy a folder from an old branch into branch being used by SCM

1.1k Views Asked by At

I am very new to pipelines, first to try for my company so please excuse if this question is a little off.

I have a Git Branch we will call "2020Release38" which is the central branch all engineers check into. I need to copy a folder from an old branch "2019Release37" into the workspace with the folder renamed and placed within the "2020Realease38" folder. This folder needs to be used by an exe called later in the pipeline. Both Branches are within the same Repo.

I attempted to this outside of a pipeline using a freestyle project, setting multiple SCM and adding "2019Release37" with a second SCM checking out to a subdirectory. This worked, however, A) it copied the entire contents of the branch, B) when polling SCM I couldn't figure out how to only look at "2020Release38", and C) somehow (I'm sure by user error) I created an endless build loop. To me, this seemed wrong as I did not need to check out the folder rather just copy from the branch.

Any help would be greatly appreciated. At the moment I have the "2020Release38" Branch pipeline script code complete, see below. Not sure where to go from here or if I should use a different step to achieve what I'm looking for.

checkout([$class: 'GitSCM', 
branches: [[name: '2020Release38']], 
doGenerateSubmoduleConfigurations: false, 
extensions: [], 
submoduleCfg: [], 
userRemoteConfigs: [[credentialsId: 'TFSBuildUser', url: 'https://some-url']]])
1

There are 1 best solutions below

1
On

So I understood you want to checkout a certain branch "X" from a repository, copy all the file to another branch "A", right?

You can use the git step to checkout a repository and stash/unstash to copy the files:

git branch: 'X', url: 'my-git-url.git'
stash includes: 'foldername/**', name: 'oldFolder'
deleteDir()

git branch: 'A', url: 'my-git-url.git'
unstash "oldFolder"
sh "mv oldFolder newFolder"