How to write a Jenkins Pipeline script to fetch needed repo?

17 Views Asked by At

I need to write a pipeline script so that it fetches the repository I need and the other Stages work in the context of that repository. I also need all variables to be available, such as the hash of the last commit or the project name.

In fact I need to do the same thing that happens when we select th "Pipeline script from SCM" option in job settings.
But in my case I can't set "Pipeline script from SCM" because I need to write a custom pipeline different from the one in the Jenkinsfile of the repository I need, and I don't want to create another Jenkinsfile.

And I don't exactly get how can I get env.GIT_COMMIT in this way? Below is my pipeline, which works if I select "Pipeline script from SCM" and set the desired repository. How

@Library("cnv-jenkins-sharedlib") _

pipeline {
    agent {
        label 'cnv-jobs'
    }
    options { 
            timestamps ()
            disableConcurrentBuilds() 
            }
            tools {
                maven 'Maven_3.9.2'
                jdk 'JDK11'
            }
            parameters {
                choice(name: 'StageName', choices: ['test', 'dev' ], description: 'Choise stage')
                booleanParam(name: 'deploy', defaultValue: false, description: 'Deploy')
            }  
            environment {
                registryUrl = 'cprd-doc-reg01.example.com:10012'
                DOCKER_CREDS = credentials('cnv_docker_reg_creds')
                ENV = "${params.StageName}"
                DEPLOY_BRANCH = "test"
                PROJECT_NAME = "background-osago-tarifficaiton-service"
                GIT_HASH = env.GIT_COMMIT.take(7)
            }


    stages {
        
        stage('prepare Environment'){
            steps {
                GetBuildVersion()
            }
        }


        stage('Build and Sonar Scanner') {
            steps {
                
                MvnBuild(registryUrl: "${registryUrl}", imageName: "cnv-${PROJECT_NAME}", imageTag: "build-${GIT_HASH}", PROFILE: "${ENV}stand")
            }
        }

        
        stage('Build image'){
            steps {
                cnvDockerBuild(dockerFile: "src/main/docker/Dockerfile.jvm", registryUrl: "${registryUrl}", imageName: "cnv-${PROJECT_NAME}", imageTag: "build-${GIT_HASH}")
            }
        }
    

        stage("Deploy to  K8S") {
            when { expression {  params.deploy  } } 
            steps {
                CnvDeployK8S(imageTag: "build-${GIT_HASH}")
            }
        }
    }
}
1

There are 1 best solutions below

0
vsfm80 On

I find out the answer. Below you can see what I need:

@Library("cnv-jenkins-sharedlib") _

pipeline {
    agent {
        label 'cnv-jobs'
    }
            options { 
                timestamps ()
                disableConcurrentBuilds() 
            }
            tools {
                maven 'Maven_3.9.2'
                jdk 'JDK11'
            }
            parameters {
                choice(name: 'StageName', choices: ['test', 'dev' ], description: 'Choice stage')
                booleanParam(name: 'deploy', defaultValue: false, description: 'Deploy to k8s')
            }  
            environment {
                registryUrl = 'cprd-doc-reg01.example.com:10012'
                DOCKER_CREDS = credentials('cnv_docker_reg_creds')
                ENV = "${params.StageName}"
                DEPLOY_BRANCH = "feature/CNVPRD-2475/osago_16_model"
                PROJECT_NAME = "background-osago-tarifficaiton-service"
            }


    stages {
            stage('Checkout SCM') {
                steps {
                    script {
                        checkout([$class: 'GitSCM', 
                        branches: [[name: "${env.DEPLOY_BRANCH}" ]], 
                        doGenerateSubmoduleConfigurations: false,                         
                        extensions: [[$class: 'CleanCheckout']], 
                        submoduleCfg: [], 
                        userRemoteConfigs: [[credentialsId: 'bitbucket_cnv_ssh', url: "ssh://[email protected]:7999/conv/tariffication-service.git"]]])
                        def scmVars = checkout([$class: 'GitSCM', branches: [[name: "${DEPLOY_BRANCH}"]], 
                                            userRemoteConfigs: [[url: 'https://github.com/jenkinsci/git-plugin.git']]])
                        GIT_HASH = scmVars.GIT_COMMIT.take(7)
                        GIT_BRANCH = scmVars.GIT_BRANCH
                        script{
                                sh "ls -lat"
                        }   
                    }   
                }       
            }
                
            

            stage('prepare Environment'){
                steps {
                    sh "echo Git Branch is: ${GIT_BRANCH}"
                    GetBuildVersion()
                }
            }


            stage('Build and Sonar Scanner') {
                steps {
                    sh "ls -la"
                    MvnBuild(registryUrl: "${registryUrl}", imageName: "cnv-${PROJECT_NAME}", imageTag: "build-${GIT_HASH}", PROFILE: "${ENV}stand")
                }
            }

            
            stage('Build image'){
                steps {
                    cnvDockerBuild(dockerFile: "src/main/docker/Dockerfile.jvm", registryUrl: "${registryUrl}", imageName: "cnv-${PROJECT_NAME}", imageTag: "build-${GIT_HASH}")
                }
            }
        

            stage("Deploy to  K8S") {
                when { expression {  params.deploy  } } 
                steps {
                    CnvDeployK8S(imageTag: "build-${GIT_HASH}")
                }
            }
        }

}