-
Notifications
You must be signed in to change notification settings - Fork 1
/
prepare_jenkins.gradle
105 lines (88 loc) · 3.61 KB
/
prepare_jenkins.gradle
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
buildscript {
dependencies {
classpath 'commons-io:commons-io:2.4'
classpath 'commons-lang:commons-lang:2.6'
}
repositories {
mavenCentral()
}
}
import org.apache.commons.io.FileUtils
import org.apache.commons.lang.SystemUtils
import java.nio.file.Files
import java.nio.file.attribute.PosixFilePermission
configurations {
jenkinsWar
jenkinsPlugins
}
dependencies {
jenkinsWar(group: 'org.jenkins-ci.main', name: 'jenkins-war', version: project.ext.jenkinsVersion, ext: 'war')
}
apply from: 'repositories.gradle'
apply from: 'plugins.gradle'
defaultTasks 'prepareJenkins'
task(prepareJenkins).doLast {
File jenkinsHome = prepareJenkinsHome()
File jenkinsWar = prepareJenkinsWar()
createRunJenkinsScript(jenkinsHome, jenkinsWar)
}
private File prepareJenkinsWar() {
def configuration = project.configurations.getByName('jenkinsWar')
def files = configuration.resolve()
def war = files.first()
return war
}
private File prepareJenkinsHome() {
File jenkinsHome = getJenkinsHome()
createOrUpdateBaseConfig(jenkinsHome)
preparePluginsDir(jenkinsHome)
return jenkinsHome
}
private File getJenkinsHome() {
def jenkinsHome = new File(System.getProperty('user.home'), "${project.name}_jenkins_home")
if (project.hasProperty('localJenkinsHome')) {
jenkinsHome = new File(project.ext.localJenkinsHome)
}
return jenkinsHome
}
private void createOrUpdateBaseConfig(File jenkinsHome) {
FileUtils.copyDirectory(new File('./jenkins-bootstrap/'), jenkinsHome.absoluteFile)
}
private void preparePluginsDir(File jenkinsHome) {
def pluginsDir = new File(jenkinsHome, 'plugins/')
// copied from gradle-jpi-plugin
// create new configuration with plugin dependencies, ignoring the (jar) extension to get the HPI/JPI files
// copy the resolved HPI/JPI files to the plugins director
//
// We ommit the path of going through jar/hpi as there is no need to download/resolve them 2 times, especially that not always there is a jar where hpi/jpi is a must have
// for jenkins plugin
project.configurations.getByName('jenkinsPlugins').resolvedConfiguration.resolvedArtifacts.findAll { it.extension in ['hpi', 'jpi'] }.each {
GFileUtils.copyFile(it.file, new File(pluginsDir, "${it.name}.${it.extension}"))
}
// pin particular plugins
new File(pluginsDir.absolutePath + '/credentials.jpi.pinned').createNewFile()
}
private void createRunJenkinsScript(File jenkinsHome, File jenkinsWar) {
def dslProjectRoot = project.projectDir
def javaParameters = " -DJENKINS_HOME=${jenkinsHome.absolutePath}" +
" -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005" +
" -Dhudson.model.ParametersAction.keepUndefinedParameters=true" +
" -DDSL_PROJECT_ROOT=${dslProjectRoot.absolutePath}" +
" -jar ${jenkinsWar.absolutePath}" + " --webroot=${jenkinsHome.absolutePath}/war" +
" --httpPort=8080"
createScript('runJenkins', """\
#!/bin/sh
cd ${jenkinsHome.absolutePath};
java ${javaParameters}
""".stripIndent())
}
private void createScript(String filename, String scriptText) {
def extension = SystemUtils.IS_OS_WINDOWS ? '.bat' : '.sh'
def scriptFile = new File(filename + extension)
scriptFile.text = scriptText
if (SystemUtils.IS_OS_UNIX) {
Files.setPosixFilePermissions(scriptFile.toPath(), [PosixFilePermission.OWNER_READ,
PosixFilePermission.OWNER_WRITE,
PosixFilePermission.OWNER_EXECUTE,] as Set)
}
}