Wim Coenen
Wim Coenen

Reputation: 66733

Read SVN URL from jenkins job configuration, from within Jenkinsfile

I have a Jenkins pipeline job which is configured as "pipeline script from SCM", pointing to a Jenkinsfile in SVN. For example, the SVN URL is http://example.com/svn/myproject/trunk/ The jenkins master does a shallow checkout (root files only) and then finds the Jenkinsfile at the root.

This URL is repeated within the Jenkinsfile, in the step which checks out the SVN workspace on a separate jenkins agent.

So whenever I clone the jenkins job for a new branch, I need to fix up the URL in two different places: once in the jenkins job config where it points at the Jenkinsfile, and once within the Jenkinsfile. Is there a way to avoid this, e.g. by reading the current job configuration from the Jenkinsfile?

Upvotes: 1

Views: 1442

Answers (1)

Wim Coenen
Wim Coenen

Reputation: 66733

In the end, I managed to retrieve the SVN URL from the job configuration via the scm global, store it in a SVNURL environment variable, and then use it in the checkout step instead of hardcoding the SVN URL.

stage('stage-name')
{
    steps
    {
        // Get the SVN URL from the pipeline job configuration
        script
        {
            env.SVNURL = scm.getLocations()[0].getURL()
        }

        checkout([
            $class: 'SubversionSCM',
            filterChangelog: false,
            ignoreDirPropChanges: false,
            locations:
                [[
                    credentialsId: '<guid>',
                    depthOption: 'infinity',
                    ignoreExternalsOption: false,
                    local: 'my-checkout-folder',
                    remote: env.SVNURL
                ]],
            workspaceUpdater: [$class: 'UpdateWithCleanUpdater']
        ])
    }
}

Upvotes: 2

Related Questions