Skip to content

Commit

Permalink
Merge branch 'heisenbug-demo'
Browse files Browse the repository at this point in the history
# Conflicts:
#	Dockerfile
#	plugins.txt
  • Loading branch information
oleg-nenashev committed Apr 25, 2018
2 parents 2d8a202 + e59f43a commit 9d2fd45
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 2 deletions.
5 changes: 5 additions & 0 deletions init_scripts/src/main/groovy/demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Demo jobs
===

This directory contains a set of demo `Jenkinsfile`s.
System Groovy scripts process this directory and create jobs for each job.
3 changes: 3 additions & 0 deletions init_scripts/src/main/groovy/demo/jenkins-buildPlugin.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
buildPlugin(platforms: ['linux'],
repo: 'https://github.com/jenkinsci/job-restrictions-plugin.git',
findbugs: [archive: true, unstableTotalAll: '0'])
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package io.jenkins.systemgroovy.plugins

import com.cloudbees.hudson.plugins.folder.Folder
import hudson.plugins.filesystem_scm.FSSCM
import org.apache.commons.io.FilenameUtils
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition
import org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition
import org.jenkinsci.plugins.workflow.job.WorkflowJob
import org.jenkinsci.plugins.workflow.libs.FolderLibraries
import org.jenkinsci.plugins.workflow.libs.LibraryConfiguration
import org.jenkinsci.plugins.workflow.libs.SCMRetriever

/**
* Helper script for Pipeline job management.
* @author Oleg Nenashev
* @since TODO
*/
class PipelineHelper {

static WorkflowJob createDemo(File file, Folder folder, String name = null, boolean useScmSource = true) {
String jobName = name ?: FilenameUtils.removeExtension(file.name)
def descriptionFile = new File(file.path.replace(".groovy", ".txt"))
def descriptionText = descriptionFile.exists() ? descriptionFile.text : null

if (useScmSource) {
return createScmPipelineJob(jobName, folder, file, descriptionText)
} else {
return createPipelineJob(jobName, folder, file.text, true, descriptionText)
}
}

static WorkflowJob createPipelineJob(String name, Folder folder, String script,
boolean sandbox = true, String description = null) {
def p = folder.createProject(WorkflowJob.class, name)
if (description != null) {
p.description = description
}
p.definition = new CpsFlowDefinition(
script,
sandbox
)
return p
}

static WorkflowJob createScmPipelineJob(String name, Folder folder, File script, String description = null) {
def p = folder.createProject(WorkflowJob.class, name)
if (description != null) {
p.description = description
}
p.definition = new CpsScmFlowDefinition(
new FSSCM(script.parent, false, false, null),
script.name
)
return p
}

static WorkflowJob createBuildPluginJob(Folder folder, String repo, String organization = "jenkinsci", String nameSuffix = "", String args = null, String extras = "") {
return createPipelineJob("${repo}${nameSuffix}", folder,
"buildPlugin(platforms: ['linux'], repo: 'https://github.com/${organization}/${repo}.git' ${extras})",
true, "Builds the ${repo} plugin"
)
}

static def addLocalLibrary(Folder folder, String library, String name = null) throws IOException {
def libraryPath = new File(library)
if (!libraryPath.exists() || !libraryPath.isDirectory()) {
throw new Error("Library path does not exist or it is not a directory: ${library}")
}
def libraryName = name ?: libraryPath.name
def scm = new FSSCM(libraryPath.absolutePath, false, false, null)
LibraryConfiguration lc = new LibraryConfiguration(libraryName, new SCMRetriever(scm))
lc.with {
implicit = true
defaultVersion = "master"
}
folder.addProperty(new FolderLibraries([lc]))
}
}
52 changes: 52 additions & 0 deletions init_scripts/src/main/groovy/scripts/DemoFolder.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package scripts
// Initializes the Development folder, which is fully configurable by the user

import groovy.io.FileType
import com.synopsys.arc.jenkins.plugins.ownership.OwnershipDescription
import io.jenkins.systemgroovy.plugins.PipelineHelper
import jenkins.model.Jenkins
import com.cloudbees.hudson.plugins.folder.Folder
import org.jenkinsci.plugins.ownership.model.folders.FolderOwnershipHelper

println("=== Initialize the Demo folder")
if (Jenkins.instance.getItem("Demo") != null) {
println("Demo folder has been already initialized, skipping the step")
return
}

// Admin owns the root Development folder
def demoFolder = Jenkins.instance.createProject(Folder.class, "Demo")
demoFolder.description = "Demo Pipeline jobs"
FolderOwnershipHelper.setOwnership(demoFolder, new OwnershipDescription(true, "user"))

// Create a library for local Jenkins Pipeline Library Development
// if the Env Var is set and the directory is mapped
println("==== Initializing local Pipeline Library development dir")
File pipelineLibSource = new File("/var/jenkins_home/pipeline-library/")
if (!new File(pipelineLibSource, "vars").exists()) { // TODO: Lame, to be fixed
println("/var/jenkins_home/pipeline-library is not mapped, skipping")
} else {
println("/var/jenkins_home/pipeline-library is mapped, initializing the directory")
PipelineHelper.addLocalLibrary(demoFolder, pipelineLibSource.absolutePath)
}

// Loads demo from sources
def demoSourceDir = new File(Jenkins.instance.rootDir, "init.groovy.d/demo")
if (demoSourceDir.exists()) {
println("===== Loading demo")
demoSourceDir.eachFile(FileType.FILES) { file ->
if (file.name.endsWith('.groovy')) {
PipelineHelper.createDemo(file, demoFolder, file.name, false)
}
}

// Also load demo from the Pipeline library
def libDemos = new File(pipelineLibSource, "demo")
if (libDemos.exists()) {
libDemos.eachFile(FileType.FILES) { file ->
if (file.name.endsWith('.groovy')) {
PipelineHelper.createDemo(file, demoFolder)
}
}
}
}
8 changes: 6 additions & 2 deletions plugins.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
matrix-auth:2.2
cloudbees-folder:6.4
workflow-aggregator:2.5
workflow-cps:2.48
workflow-cps:2.49
git:3.8.0
timestamper:1.8.9
yet-another-docker-plugin:0.1.0-rc47
Expand All @@ -14,7 +14,11 @@ security-inspector:0.4
monitoring:1.72.0
locale:1.2
blueocean:1.5.0
filesystem_scm:experimental
filesystem_scm:2.1
junit:1.24
checkstyle:3.50
findbugs:4.72
parallel-test-executor:1.10
email-ext:2.62
jacoco:2.2.1
cobertura:1.12

0 comments on commit 9d2fd45

Please sign in to comment.