forked from sheehan/job-dsl-gradle-example
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetupJenkins.ps1
89 lines (66 loc) · 2.54 KB
/
SetupJenkins.ps1
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
$ErrorActionPreference = 'Stop'
$jenkinsPath = "~/.jenkins"
Remove-Item -Recurse -Force $jenkinsPath -ErrorAction SilentlyContinue
$initScript = @"
#!groovy
import jenkins.model.*
import hudson.security.*
import jenkins.install.*
import java.util.logging.Logger
def instance = Jenkins.getInstance()
instance.setInstallState(InstallState.INITIAL_SETUP_COMPLETED)
println "--> creating local user 'admin'"
def hudsonRealm = new HudsonPrivateSecurityRealm(false)
hudsonRealm.createAccount('admin','admin')
instance.setSecurityRealm(hudsonRealm)
def strategy = new FullControlOnceLoggedInAuthorizationStrategy()
instance.setAuthorizationStrategy(strategy)
def pm = instance.getPluginManager()
def uc = instance.getUpdateCenter()
def initialized = false
def logger = Logger.getLogger("")
def pluginParameter="job-dsl git github gradle cloudbees-folder workflow-aggregator"
def plugins = pluginParameter.split()
logger.info("" + plugins)
plugins.each {
logger.info("Checking " + it)
if (!pm.getPlugin(it)) {
logger.info("Looking UpdateCenter for " + it)
if (!initialized) {
uc.updateAllSites()
initialized = true
}
def plugin = uc.getPlugin(it)
if (plugin) {
logger.info("Installing " + it)
def installFuture = plugin.deploy()
while(!installFuture.isDone()) {
logger.info("Waiting for plugin install: " + it)
sleep(3000)
}
installed = true
}
}
}
logger.info("Plugins installed, initializing a restart!")
instance.save()
instance.restart()
"@
$securityScript = @"
import javaposse.jobdsl.plugin.GlobalJobDslSecurityConfiguration
import jenkins.model.GlobalConfiguration
GlobalConfiguration.all().get(GlobalJobDslSecurityConfiguration.class).useScriptSecurity=false
"@
New-Item -ItemType Directory -Path "~/.jenkins/init.groovy.d" -ErrorAction SilentlyContinue
$Utf8NoBomEncoding = New-Object System.Text.UTF8Encoding $False
$filePath = Join-Path (Resolve-Path "~/.jenkins/init.groovy.d") "init.groovy"
[System.IO.File]::WriteAllLines($filePath, $initScript, $Utf8NoBomEncoding)
$filePath = Join-Path (Resolve-Path "~/.jenkins/init.groovy.d") "security.groovy"
[System.IO.File]::WriteAllLines($filePath, $securityScript, $Utf8NoBomEncoding)
$env:JAVA_OPTS = '-Djenkins.install.runSetupWizard=false'
# TODO make this portable
#!/bin/bash
$env:JAVA_HOME = (/usr/libexec/java_home --version 1.8)
$jenkinsWarPath = '/usr/local/Cellar/jenkins/2.130/libexec/jenkins.war'
java '-Djenkins.install.runSetupWizard=false' -jar $jenkinsWarPath
# open "/Applications/Google Chrome.app" 'http://localhost:8080'