user2301299
user2301299

Reputation: 539

Access to git Jenkinsfile url repo from within Jenkinsfile

I need simple thing:

  1. Download Jenkinsfile from some repository. This is done on Jenkins project configuration GUI.
  2. After downloading Jenkinsfile and starting the job, I need to get access to url from which the Jenkinsfile "I am in" was downloaded.
  3. Checkout master branch (on multiple parallel nodes) using the url descirbed in point 2.

I am stacked on point number 2. Is it possible to access the url for repository from which the Jenkinsfile was downloaded? I don't want to hardcode it in Jenkinsfile.

Upvotes: 0

Views: 983

Answers (2)

tim
tim

Reputation: 407

I am using scm.userRemoteConfigs[0].url, but I am not sure whether you need an actual checkout scm step for the object to be present. The Script Approval is also necessary.

I would like to post this as a comment to the accepted answer but my reputation is too low :(

Upvotes: 0

Jakub Bujny
Jakub Bujny

Reputation: 4628

That's really interesting case! I have assumption that you are using git for checkout Jenkinsfile. So here is code for you:

println Hudson.instance.getItem(env.JOB_NAME).definition.scm.userRemoteConfigs[0].url

Jenkins will complain about security so you must approve signatures in Manage Jenkins -> In-process Script Approval

Upvotes: 1

Related Questions