19

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

1

5 Answers 5

27

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

Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting the error 'org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException' but if I have to approved the methods this is working .. I'm still trying to find another way without approve nothing before
@dsaydon I guess it is impossible without approving. Why it's a problem for you? BTW consider using just checkout scm.
Thank you for you help @vitalii-vitrenko . I found another way (not the best one) that did the trick . I just posted it
link is broken - believe it's this now: javadoc.jenkins.io/plugin/git/hudson/plugins/git/GitSCM.html
3

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.

4 Comments

I find "/var/lib/jenkins" in your proposal rather scary. Cannot you use the environ var JENKINS_HOME for that?
no, because in my solution you need the config.xml of the relevant job. buy why scary?
Using a hard-coded path for jenkins would break your code in case Jenkins is moved to some other location. And using a variable costs nothing.
@RaúlSalinas-Monteagudo you right! , this is just an example of implementation. you can use the Jenkins magics as much as you want
2

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 

1 Comment

Or use env.SVN_URL_1
1

I believe that the best solution is like this answer. An example using declarative pipeline:

pipeline { agent any; stages { stage('test'){ steps { script { def s = checkout scm; if (s.GIT_URL != null) print s.GIT_URL else if (s.SVN_URL != null) print s.SVN_URL else print s } } } } } 

Note - this does a full checkout. If that is not desirable, I would try to handle that in checkout parameters (like here)

Comments

0

env.GIT_URL can be used to access the git url that was used to obtain the Jenkinsfile. The branch is in env.GIT_BRANCH.

Works here:

checkout( [ $class: 'GitSCM', branches: [[name: env.GIT_BRANCH]], doGenerateSubmoduleConfigurations: false, submoduleCfg: [], userRemoteConfigs: [[ credentialsId: 'XXX', url: env.GIT_URL ]] ] ) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.