Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jenkins pipeline get repository url variable under pipeline script from scm

I'm using Jenkins file that located in my git repository. I have configured new job using the pipeline script from SCM that point to my jenkinsfile. I'm trying to use in my Jenkins file pipeline script the git module in order to pull my data from my git repo without configure pre-static variable and just to use the variable of the repository URL under pipeline script from SCM that already was configured in my job . There is a way to get somehow the variable Repository URL from this plugin without using parameters in my Jenkins pipeline script.

I have already tried the environment variable GIT_URL and other stuff that related to git from here but this didn't work.

pipeline script from scm

like image 988
dsaydon Avatar asked Aug 29 '17 10:08

dsaydon


People also ask

How do I give a Git repository URL in Jenkins?

Step 1: go to your GitHub repository and click on 'Settings'. Step 2: Click on Webhooks and then click on 'Add webhook'. Step 3: In the 'Payload URL' field, paste your Jenkins environment URL. At the end of this URL add /github-webhook/.

How do you access parameters in Jenkins pipeline?

Access Parameters Inside Pipeline Stages You just have to use params. [NAME] in places where you need to substitute the parameter. Here is an example of a stage that will be executed based on the condition that we get from the choice parameter.

What is pipeline script from scm in Jenkins?

As mentioned previously, Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. Pipeline provides an extensible set of tools for modeling simple-to-complex delivery pipelines "as code" via the Pipeline DSL.


3 Answers

You can find all information about scm in scm variable (instance of GitSCM if you are using git). You can get repository URL this way

def repositoryUrl = scm.userRemoteConfigs[0].url  

But if you just want to checkout that repository you can simply invoke checkout scm without needing to specify anything else. See checkout step

like image 188
Vitalii Vitrenko Avatar answered Nov 02 '22 09:11

Vitalii Vitrenko


Probably not directly a solution for your particular case, as you're working with git.

But for those still working with SVN using the SubversionSCM, the repository URL can be obtained using

def repositoryUrl = scm.locations[0].remote
like image 38
Gerald Mücke Avatar answered Nov 02 '22 10:11

Gerald Mücke


from this post I found a way that you can use the checkout scm to get the git repo url like this:

checkout scm
def url = sh(returnStdout: true, script: 'git config remote.origin.url').trim()

but checkout scm will pull the code and I want to avoid from that.

So I found another way (not the pretty one):

node('master'){

  try{
        GIT_REPO_URL = null
        command = "grep -oP '(?<=url>)[^<]+' /var/lib/jenkins/jobs/${JOB_NAME}/config.xml"
        GIT_REPO_URL = sh(returnStdout: true, script: command).trim();
        echo "Detected Git Repo URL: ${GIT_REPO_URL}"  
    }
    catch(err){
        throw err
        error "Colud not find any Git repository for the job ${JOB_NAME}"
  }

}

this is did the trick for me.

like image 2
dsaydon Avatar answered Nov 02 '22 08:11

dsaydon