Search code examples
gitgroovyjenkins-pipeline

How to change directory to the cloned repo in Jenkins?


Here is my Jenkins file:

    stage('Clone the Prometheus dashboard repo')
    {
        deleteDir()
        checkout([$class: 'GitSCM', branches: [[name: "*/master"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: "${Git}", url: "https://testorg/testrepo/test-kubernetes.git"]]])
    }
    stage("Install the dashboards")
    {
    sh '''#!/usr/bin/env bash
        set -x
        cd ${WORKSPACE}
        pwd
        ls
        cd test-kubernetes/kubernetes/Prometheus/kube-prometheus/utils
        cat build.sh
    '''
    }

Unfortunately, It is not able to change to the directory. Also in ls I do not see the cloned repo.

May I know what am I doing wrong here?


Solution

  • you can do like this:

    node {
        deleteDir()
        dir('dir-name') {
            stage('checkout') {
                git branch: 'main',url: 'https://github.com/samitkumarpatel/test0.git'
            }
            stage('do stuff') {
                sh """
                    ls -al
                """
            }
        }
    }
    

    This means when Jenkins start build it will create a folder dir-name if not exist on Jenkins job $WORKSPACE and checkout the repo inside that directory.

    test0.git folder structure

    ├── Jenkinsfile
    ├── Jenkinsfile.Declarative-n-scripted
    ├── Jenkinsfile.iffilechange
    ├── Jenkinsfile.test0
    ├── README-one.md
    ├── README.md
    ├── shared-library
    │   ├── src
    │   └── vars
    │       ├── log.groovy
    │       ├── myPipeline.groovy
    │       └── oneToTen.groovy
    ├── src01
    │   └── 01.txt
    ├── src02
    │   └── 02.txt
    └── src03
        └── 03.txt
    

    Jenkins log output

    enter image description here

    Note- On my example in the output you can't see test0 folder on the job WORKSPACE because the SCM git plugins checkout subfolder and file, not the root folder. So on your case you can navigate to the folder like cd $WORKSPACE/kubernetes/Prometheus/kube-prometheus/utils