forked from AnalogJ/you-dont-know-jenkins-init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
9000.bootstrap-job.groovy
61 lines (49 loc) · 2.22 KB
/
9000.bootstrap-job.groovy
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import jenkins.model.Jenkins;
import hudson.model.FreeStyleProject;
import hudson.triggers.TimerTrigger
import hudson.model.*
import jenkins.model.*
import hudson.slaves.*
import javaposse.jobdsl.plugin.*
import hudson.plugins.git.*
import java.util.Collections
import java.util.List
import hudson.plugins.git.extensions.GitSCMExtension
import hudson.plugins.git.extensions.impl.*;
def bootstrap_job_name = 'Bootstrap-DSL'
def bootstrap_job_git_remote = "[email protected]:owner/repo.git"
def ssh_key_credential_id = "123-456"
// jenkins seems to happily overwrite existing jobs, no need to delete an existing one
def job = new FreeStyleProject(Jenkins.instance, bootstrap_job_name)
job.setDescription('Bootstraps the Jenkins Server (installs all jobs using the jobs-dsl plugin)')
//set build steps (shell copy)
def shellStep = new hudson.tasks.Shell('cp -f $HUDSON_HOME/chef-config.json $WORKSPACE/resources/chef-config.json')
job.buildersList.add(shellStep)
//set build trigger cron to run daily
job.addTrigger(new TimerTrigger("H H * * *"))
//set post build steps
def publishersList = job.getPublishersList()
publishersList.add(new hudson.tasks.BuildTrigger("Downstream-Job-Name", false))
//set SCM
List<BranchSpec> branches = Collections.singletonList(new BranchSpec("*/master"));
List<SubmoduleConfig> submoduleCnf = Collections.<SubmoduleConfig>emptyList();
// We are using predefined user id jenkins. You change it in the global config
List<UserRemoteConfig> usersconfig = Collections.singletonList(
new UserRemoteConfig(bootstrap_job_git_remote, '', '', ssh_key_credential_id)
)
List<GitSCMExtension> gitScmExt = new ArrayList<GitSCMExtension>();
def scm = new GitSCM(usersconfig, branches, false, submoduleCnf, null, null, gitScmExt)
job.setScm(scm)
builder = new javaposse.jobdsl.plugin.ExecuteDslScripts(null)
builder.setUseScriptText(false)
builder.setTargets("""
jobs/default_jobs_dsl.groovy
jobs/default_jobs_utilities_dsl.groovy
jobs/default_views_dsl.groovy
""")
builder.setRemovedJobAction(javaposse.jobdsl.plugin.RemovedJobAction.DELETE)
builder.setRemovedViewAction(javaposse.jobdsl.plugin.RemovedViewAction.DELETE)
builder.setAdditionalClasspath('src/main/groovy/')
job.buildersList.add(builder)
job.save()
Jenkins.instance.restart()