这里是詹金斯(Jenkins)和Groovy的新手。我正在尝试在jenkins脚本控制台中运行groovy脚本。基本上,这就是我要执行的操作-在根文件夹下有一个项目project-x。在project-x内部,有很多工作。假设其中之一是analytics-1。我现在想在project-x下创建一个名为analytics-1的新项目。然后,我想将现有的职位分析1移到新创建的项目下。
Root/project-x/analytics-1
Root/project-x/analytics-1/analytics-1
我找到了一些github代码(请参阅结尾),使我可以执行类似的操作,但只能在根目录或jenkins基本目录中进行。
Root/analytics-1
Root/analytics-1/analytics-1
但是我想像最初说明的那样在根目录内的特定项目中执行此操作。
我知道我们必须使用正确且相关的方法。我尝试使用def folder = jenkins.getItem(folderName)
代替def folder = jenkins.getItemByFullName(folderName)
。我可以使用哪些方法代替createProject,getItems等?看了参考资料,但不知道...还是有另一种方法可以实现相同目的?
来自github的代码: https://github.com/peterjenkins1/jenkins-scripts/blob/master/add-folder.groovy
import com.cloudbees.hudson.plugins.folder.*
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import jenkins.model.Jenkins
Jenkins jenkins = Jenkins.instance
// Bring some values in from ansible using the jenkins_script modules wierd "args" approach (these are not gstrings)
String folderName = "analytics-1"
String folderNameTemp = folderName + "-folder"
def folder = jenkins.getItem(folderName)
if (folder == null) {
// Create the folder if it doesn't exist or if no existing job has the same name
folder = jenkins.createProject(Folder.class, folderName)
} else {
if (folder.getClass() != Folder.class) {
// when folderName exists, but is not a folder we make the folder with a temp name
folder = jenkins.createProject(Folder.class, folderNameTemp)
// Move existing jobs from the same environment to folders (preseve history)
Item[] items = jenkins.getItems(WorkflowJob.class)
def job_regex = "^" + folderName
items.grep { it.name =~ job_regex }.each { job ->
Items.move(job, folder)
}
// Rename the temp folder now we've moved the jobs
folder.renameTo(folderName)
}
}