通过 Jenkins Java API 来配置 Job 的方法是观察 config.xml 的结构,然后根据解构来查看 Javadoc 文档,并进一步编写代码。
RunWrapper | currentBuild and previousBuild
org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper
https://javadoc.jenkins.io/plugin/workflow-support/org/jenkinsci/plugins/workflow/support/steps/build/RunWrapper.html
getBuildVariables() // 获取环境变量,但是该 Map 为 Java Map 类型,其不支持 get(”,default) 操作。
jenkins.model.Jenkins
https://javadoc.jenkins-ci.org/jenkins/model/Jenkins.html
getItem()
getItems() // Gets just the immediate children of Jenkins.
getAllItems()
Multibranch Pipeline
jenkins.model.Jenkins
https://javadoc.jenkins.io/jenkins/model/Jenkins.html
jenkins.plugins.git.GitSCMSource
https://javadoc.jenkins.io/plugin/git/jenkins/plugins/git/GitSCMSource.html
jenkins.branch.BranchSource
https://javadoc.jenkins.io/plugin/branch-api/jenkins/branch/BranchSource.html
jenkins.branch.MultiBranchProject
https://javadoc.jenkins.io/plugin/branch-api/jenkins/branch/MultiBranchProject.html
org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject
https://javadoc.jenkins.io/plugin/workflow-multibranch/org/jenkinsci/plugins/workflow/multibranch/WorkflowMultiBranchProject.html
创建多分支流水
Manage Jenkins Using Script Console | by Nilkumar Shah | Globant | Medium
def jenkinsInstance = hudson.model.Hudson.instance
def multibranchJob = new org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject(jobsParentFolder, projectId);
// 显示名称
String pathWithNamespace = project.getPathWithNamespace()
multibranchJob.setDisplayName("${projectId}~${pathWithNamespace}")
// 代码仓库
def gitScmSource = new jenkins.plugins.git.GitSCMSource(project.getHttpUrlToRepo())
gitScmSource.setCredentialsId('gitlab-team-opbot-username-password')
gitScmSource.setTraits([
new jenkins.plugins.git.traits.BranchDiscoveryTrait(),
new jenkins.plugins.git.traits.TagDiscoveryTrait()
])
def gitBranchSource = new jenkins.branch.BranchSource(gitScmSource)
def gitBranchSourceList = [gitBranchSource]
multibranchJob.setSourcesList(gitBranchSourceList)
multibranchJob.save()
jenkinsInstance.reload()
我们担心频繁的 reload() 操作会对 Jenkins 产生影响,所以我们实用 createProject 进行创建:
def jenkinsInstance = jenkins.model.Jenkins.instance
def parentItem = jenkinsInstance.getItem("Jenkins-Managed-Jobs")
def multibranchJob = parentItem.createProject(org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject, projectId)
String pathWithNamespace = project.getPathWithNamespace()
multibranchJob.setDisplayName("${projectId}~${pathWithNamespace}")
def gitScmSource = new jenkins.plugins.git.GitSCMSource(project.getHttpUrlToRepo())
gitScmSource.setCredentialsId('gitlab-team-opbot-username-password')
gitScmSource.setTraits([
new jenkins.plugins.git.traits.BranchDiscoveryTrait(),
new jenkins.plugins.git.traits.TagDiscoveryTrait()
])
def gitBranchSource = new jenkins.branch.BranchSource(gitScmSource)
def gitBranchSourceList = [gitBranchSource]
multibranchJob.setSourcesList(gitBranchSourceList)
multibranchJob.save()
设置 Job 属性
org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject
Agent
Manage Jenkins Using Script Console | by Nilkumar Shah | Globant | Medium
import hudson.model.*
import hudson.slaves.*
// Define agent parameters
def agentName = "YourAgentName"
def agentDescription = "Your agent description"
def remoteFS = "/path/to/agent/workspace"
def labels = "linux docker"
def numExecutors = 2
// Create a new agent
def agent = new DumbSlave(
agentName,
"",
remoteFS,
numExecutors,
Node.Mode.NORMAL,
labels,
new JNLPLauncher(),
new RetentionStrategy.Always(),
new ArrayList<NodeProperty<Node>>()
)
// Add agent to Jenkins
Jenkins.instance.addNode(agent)
// Print agent creation success message
println("Jenkins agent '$agentName' added successfully.")
案例
通过 Jenkins Script 来进行批量操作;
peterjenkins1/jenkins-scripts: Useful groovy scripts for configuring Jenkins via Ansible
create a jenkins job in the script console
Script Console
https://www.jenkins.io/doc/book/managing/script-console/
创建 Job
configuration – Configure or Create hudson job automatically – Stack Overflow
def jenkins = hudson.model.Hudson.instance
def template = jenkins.getItem("MyTemplate")
def job = jenkins.copy(template,"MyNewJob")
job.scm = new hudson.scm.SubversionSCM("http://base/branches/mybranche")
job.save()
How do I create a new project in Jenkins using Groovy – Stack Overflow
//Get instance of Jenkins
def parent = Jenkins.getInstance()
//Define a job name
def jobName = "Job"
//Instantiate a new project
def project = new FreeStyleProject(parent, jobName);
//Set a description for the project
project.setDescription("Just a placeholder for a description")
//Create a parameter for the project
def parameterDefinitions = new ArrayList<ParameterDefinition>();
def name = "ParameterOne"
def defaultValue = "1"
def description = "Just a placeholder for a parameter description"
parameterDefinitions.add(new StringParameterDefinition(name, defaultValue, description) )
//Create a job property for the project
def jobProperty = new ParametersDefinitionProperty(parameterDefinitions);
//Adding and saving the job property to the project
project.addProperty(jobProperty)
project.save()
补充说明
在网页中,创建 Item 时,在浏览器中检查元素,查看 HTTP 请求,以此获得相关资源类。