Obtain GitSCM from a checkout step in pipeline

34 views
Skip to first unread message

Ullrich Hafner

unread,
Jan 7, 2021, 9:48:27 AM1/7/21
to Jenkins Developers
In one of my plugins I am using the GitSCM instance of a job to analyze the associated Git repository. This works for a lot of cases (input: a Jenkins run):

- For FreeStyle jobs I get the instance from AbstractProject.getSCM()
- For Pipelines jobs I get the instance from the job ((SCMTriggerItem) job).getSCMs()
- For Pipelines jobs with CpsScmFlowDefinition I get the instance from the definition ((CpsScmFlowDefinition) definition).getScm()

But when I use the following pipeline code in an integration test to check out a specific SHA-1 of a repository, then none of these options work to get the SCM. Is in such a case no SCM instance stored in the run or job? 
(I get called with a GitSCM instance in a CheckoutListener, thus the instance actually is created) 
checkout([$class: 'GitSCM', branches: [[name: '28af63def44286729e3b19b03464d100fd1d0587' ]], userRemoteConfigs: [[url: '" + REPOSITORY_URL + "']]])"
In the run I can find BuildData, GitTagAction and ScmRevisionState instances, but no one seems to link to the GitSCM.





Jesse Glick

unread,
Jan 7, 2021, 11:09:41 AM1/7/21
to Jenkins Dev
You could possibly reconstruct this information from `ArgumentsAction`. Probably more reliable to use `CheckoutListener` though.

Jesse Glick

unread,
Jan 7, 2021, 11:12:03 AM1/7/21
to Jenkins Dev
Or directly check `WorkflowRun.getSCMs()`. Not currently exposed as an API via `Run` (`getChangeSets()` is a bit different).

Ullrich Hafner

unread,
Jan 7, 2021, 12:41:02 PM1/7/21
to Jenkins Developers

Or directly check `WorkflowRun.getSCMs()`. Not currently exposed as an API via `Run` (`getChangeSets()` is a bit different).


Yes that works, thanks!


Reply all
Reply to author
Forward
0 new messages