0

I am defining MultiBranchPipeline in Jenkins which clones the Project1. Now I define a step to clone a Project2 as well, but it would overwrite the content of the Project1.

pipeline {
    agent {
       label 'agent'
    }

    stages {

        stage('Project1 checkout automatically') {
            steps {
                script {
                    echo "Project1 is cloned automatically by the given Jenkins configuration"
                    //e.g. : base/Project1                      
                }
            }
        }
        
        stage('Project2 checkout phase'){
            steps{
                        git branch: 'master',
                        credentialsId: 'b9152824f',
                        url: 'https://git/Project2.git'  
                        //e.g. : base/Project2                  
            }
        }


    }
}

So, this will override the content of the base folder only with base/Project2 content. Is there some simple solution which I can use to merge Project2 content into the content of the existing Project1 so I get both: base/Project1 and base/Project2 folders

vel
  • 1,000
  • 1
  • 13
  • 35

1 Answers1

0

when you build a job you should see a path to workspace like Running in 'jenkins/home/' that's where the first project gets copied. I recommend declaring a env variable for workdir for ppoth projects nd use something different workdir to clone project 2

    pipeline {
>         agent {
>            label 'agent'
>         }
> 
>         environment{ 
               WORKDIR = 'jenkins/home/<yourjobname>' 
          }
>         stages {
>     
>             stage('Project1 checkout automatically') {
>                 steps {
>                     script {
>                         echo "Project1 is cloned automatically by the given 
>                          Jenkins configuration"
>                         //e.g. : base/Project1                      
>                     }
>                 }
>             }
>             
>             stage('Project2 checkout phase'){
>                 steps{
>                             cd $WORKDIR
                              mkdir -p project1 && cp . project1
                              mkdir project2

>                             
>                             cd project2
>                             git branch: 'master',
>                             credentialsId: 'b9152824f',
>                             url: 'https://git/Project2.git'  
>                             //e.g. : base/Project2                  
>                 }
>             }
>             stage("Clean WorkSpace") {
                   steps {
                     cleanWs()
                   }
               }
>     
>         }
>     }

This would create another folder in your current folder and clone everything there.

Here we will create a ir project1 and copy all files there(note the existing files in base directory will be there if you don't want that you can use mv command instead of cp. Then create another dir in base dir as project2 nd cd into that later checkout there.

You can add another stage at the end to clear your WORKDIR at the end so next time you run job gain there won't be an issue

Nitin k
  • 95
  • 7
  • Hi, I don't want to create this in another folder. Both projects must be in the same folder. I must have this structure: `base/` and then within it `Project1` and `Project2` because my further build jobs depend that I have them in same dir not in another subfolder - `project2/base/Project2` . Please correct your answer accordingly if possible... – vel May 05 '23 at 14:05
  • Thanks, I am reviewing it. It's not possible somehow to clone the project and "merge" it into the existing one? (without copy and other aspects) – vel May 05 '23 at 15:22
  • So you want both to be in same folder? As merging them might override the existing files. – Nitin k May 05 '23 at 16:44
  • I want to merge them, but nothing will be overwritten since they have different folders Project1 and Project2. Only common for both of them is "base" folder. – vel May 05 '23 at 18:27
  • so your folder structure should be like `/base/project1` and `/base/project2`. Will base have anything else? or just these two folder? so when you clone project one you were getting `base/project1` and when you clone second you got `base/project2` but you lost `base/project1`. Right? – Nitin k May 06 '23 at 04:01
  • As per i know you can't clone two repo's inn one directory as it will override the .git file which it has. One way is to clone it somewhere else and copy it in your base folder. Check this out [/git-clone-multiple-branches-into-same-local-directory-possible](https://stackoverflow.com/questions/28823460/git-clone-multiple-branches-into-same-local-directory-possible) – Nitin k May 06 '23 at 04:05