简体   繁体   中英

How to set default credentials in Jenkins using Groovy script?

I am trying to write my first 'Groovy script' in Jenkins and using SVN

My requirement is to create a 'free style job' from another 'Jenkins job' using groovy

Using the following code, I am able to create a 'free style job' via 'groovy'

import jenkins.model.Jenkins;
import hudson.model.FreeStyleProject;
import hudson.tasks.Shell;
import hudson.triggers.*;


def feature_branch = build.buildVariableResolver.resolve("FEATURE_BRANCH")

job = Jenkins.instance.createProject(FreeStyleProject, feature_branch)

job.buildersList.add(new Shell('echo hello world'))
job.scm = new hudson.scm.SubversionSCM("http://base/branches/mybranche")

job.save()

Though the job is created by default 'SCM Credentials' to set to 'None' in the 'newly created job'

1) How can I set the 'default credentials' using groovy from 'Global credentials' ? 2) How can I add a 'build parameter' via groovy? Again not much information available on google

Try use something like this:

job('FEATURE_BRANCH') {
    steps {
       shell("echo 'hello world'")
    }  
    scm {
      svn {
        location('http://base/branches/mybranche') {
          credentials('crd_svn')
         }
      }
    }
}

crd_svn is the identifier of the credentials that are managed by the Credentials Plugin .

And Job DSL plugin page could be useful.

搜索一段时间后,发现以下内容使我能够设置作业的“默认凭据”

job.scm.locations[0].credentialsId = "Hash-value-of-credential"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM