0

I am able to configure Jenkins declarative pipeline, which is being executed for my git project1 repo. This is being done through the standard Jenkins configuration of the targeted git repo.

But now I need to execute second "git clone" operation for my project2, run some script in project2 and then Jenkins needs to continue to run in project1. How this can be achieved? I was only working until now solely in project1 repo. But now I need to clone project2 in the same level where project1 is cloned (NOT within the project1)

--project1_repo
  -->root/base/files
--project2_repo
  -->root/base/files

and I need to run some script in project2 before Jenkins continues build for project1. Of course only trigger of the job is once the code is being pushed to the project1.

Please send me some instructions how this can be solved.

ycr
  • 12,828
  • 2
  • 25
  • 45
vel
  • 1,000
  • 1
  • 13
  • 35

1 Answers1

1

Are you looking for something like below? You can use the git step to checkout the repos, which will give you more flexibility on what you want to do.

pipeline {
    agent any

    stages {
        stage('Project1'){
            steps{
                cleanWs()
                dir('project1') {
                    // Doing your project 1 stuff
                    git(url: 'https://github.com/xxx/proj1.git', branch: 'main')
                }
                
            }
        }
        stage('Project 2'){
            steps{
                dir('project2') {
                    // Doing your project 2 stuff
                    git(url: 'https://github.com/xxx/proj2.git', branch: 'dev')
                }
                
            }
        }
        stage('Something Else'){
            steps{
               sh 'ls -al'         
            }
        }
    }
}

Update

pipeline {
    agent any

    stages {
        stage('Project1'){
            steps{
                cleanWs()
                echo "Let's move proj 1 stuff to a sub dir"
                sh '''
                mkdir project1
                shopt -s extglob dotglob
                mv !(project1) project1
                '''
            }
        }
        stage('Project 2'){
            steps{
                dir('project2') {
                    // Doing your project 2 stuff
                    git(url: 'https://github.com/xxx/proj2.git', branch: 'main')
                }
                
            }
        }
        stage('Something Else'){
            steps{
               sh 'ls -al'               
            }
        }
    }
}
ycr
  • 12,828
  • 2
  • 25
  • 45
  • hi thanks for your assistance. I think we need more adjustments. So, I configure multibranch pipeline to have automatic checkout on Project1 once we have a code pushed to master branch. So I think `stage('Project1')` is not needed since that is being done automatically. But once its checkout I need to git clone Project2 in separate dir, run the script. It must be outside the Project2 checkout dir. Then I need to run script in Project2 and then to return to Project1. So I think the above solution doesn't meet my criteria.. Can you do modifications in accordance to the instructions? Thx – vel Apr 30 '23 at 18:25
  • @AndreyS If you rely on default source checkout it will always checkout the code to the root directory, so you will have to move the source to a sub dir before doing anything, Take a look at the updated Pipeline. – ycr Apr 30 '23 at 19:55
  • thanks once again... where will project2 be cloned in the updated example? in which dir? in root or in some other? Also question - how to switch in third stage "Something Else" again to the project1 folder - since I need to continue work in project1 after I finish project2 stage? Thanks in advance!!!! – vel May 01 '23 at 12:37
  • @AndreyS When you use the `dir('project2')` directive the current directory is changed to the specified directory, So project2 will be checked out to the `project2` directory. Inorder to change the directory in the `Stage 3` you can cd into it or you can use `dir('project1')`. – ycr May 01 '23 at 12:42
  • so `dir('project2')` actually does the `cd` to`root/project2` folder that should be created in advance and in there we will do the `git clone` of the project2 (so we will have duplicate `project2` folder correct?) That's not an issue, just checking if that is the expected behaviour. Thanks – vel May 01 '23 at 15:46
  • @AndreyS Yes for the first question. Within `project2` there will not be a duplicate directory, source will be within `project2` directory without the repo name. – ycr May 02 '23 at 01:04
  • can you please assist me with the little bit different use case - now I want both repos to be cloned at one place, not as separate folders: https://stackoverflow.com/questions/76182034/jenkins-how-to-merge-second-git-repo-into-the-default-one – vel May 05 '23 at 12:08