-
Notifications
You must be signed in to change notification settings - Fork 24
/
init_24_github_org_project.groovy
235 lines (218 loc) · 12.4 KB
/
init_24_github_org_project.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
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
import hudson.model.*;
import jenkins.model.*;
import hudson.security.ACL;
import jenkins.branch.OrganizationFolder;
import jenkins.scm.api.SCMSourceOwner;
import jenkins.scm.api.SCMSourceOwners;
import org.jenkinsci.plugins.github_branch_source.GitHubSCMNavigator;
import com.cloudbees.hudson.plugins.folder.Folder
import com.cloudbees.plugins.credentials.CredentialsProvider
import com.cloudbees.plugins.credentials.common.StandardUsernamePasswordCredentials
import com.cloudbees.plugins.credentials.CredentialsMatchers
import com.cloudbees.nectar.plugins.skip.SkipFolderProperty
import java.util.logging.Logger
Logger logger = Logger.getLogger("init_24_github_org_project.groovy")
def j = Jenkins.instance
File disableScript = new File(j.rootDir, ".disable-init_24_github_org_project")
if (disableScript.exists()) {
logger.info("DISABLED init_24_github_org_project script")
return
}
def env = System.getenv()
def masterName = System.properties.'MASTER_NAME'
def credentialsId = env['GITHUB_ORG_FOLDER_CRED_ID']
if(credentialsId != null && masterName != null) {
def cbWarProfile = System.properties.'cb.IMProp.warProfiles'
logger.info("init_24_github_org_project cb.IMProp.warProfiles=$cbWarProfile")
StandardUsernamePasswordCredentials cred = null
logger.info("checking for provided credentials based on id of $credentialsId")
List<StandardUsernamePasswordCredentials> candidates = new ArrayList<StandardUsernamePasswordCredentials>();
candidates.addAll(CredentialsProvider.lookupCredentials(com.cloudbees.plugins.credentials.common.StandardUsernameCredentials.class, Jenkins.instance));
cred = CredentialsMatchers.firstOrNull(candidates, CredentialsMatchers.withId(credentialsId))
def jobName = env['GITHUB_ORG_FOLDER_REPO_OWNER']
if ( cred && jobName ) {
def includes = env['GITHUB_ORG_FOLDER_REPO_INCLUDES'] ?: "*"
def excludes = env['GITHUB_ORG_FOLDER_REPO_EXCLUDES'] ?: ""
logger.info("using credential description $jobName for GitHub Org name in init_24_github_org_project script - creating GitHub Org Folder")
println "--> creating $jobName"
def jobConfigXml = """
<jenkins.branch.OrganizationFolder plugin="[email protected]">
<actions>
<io.jenkins.blueocean.service.embedded.BlueOceanUrlAction plugin="[email protected]">
<blueOceanUrlObject class="io.jenkins.blueocean.service.embedded.BlueOceanUrlObjectImpl">
<mappedUrl>blue/organizations/jenkins/pipelines/</mappedUrl>
<modelObject class="jenkins.branch.OrganizationFolder" reference="../../../.."/>
</blueOceanUrlObject>
</io.jenkins.blueocean.service.embedded.BlueOceanUrlAction>
</actions>
<description/>
<properties>
<com.cloudbees.hudson.plugins.folder.properties.EnvVarsFolderProperty plugin="[email protected]">
<properties/>
</com.cloudbees.hudson.plugins.folder.properties.EnvVarsFolderProperty>
<org.jenkinsci.plugins.pipeline.modeldefinition.config.FolderConfig plugin="[email protected]">
<dockerLabel/>
<registry plugin="[email protected]"/>
</org.jenkinsci.plugins.pipeline.modeldefinition.config.FolderConfig>
<jenkins.branch.NoTriggerOrganizationFolderProperty>
<branches>.*</branches>
</jenkins.branch.NoTriggerOrganizationFolderProperty>
</properties>
<folderViews class="jenkins.branch.OrganizationFolderViewHolder">
<owner reference="../.."/>
</folderViews>
<healthMetrics>
<com.cloudbees.hudson.plugins.folder.health.WorstChildHealthMetric plugin="[email protected]">
<nonRecursive>false</nonRecursive>
</com.cloudbees.hudson.plugins.folder.health.WorstChildHealthMetric>
<com.cloudbees.hudson.plugins.folder.health.AverageChildHealthMetric plugin="[email protected]"/>
<com.cloudbees.hudson.plugins.folder.health.JobStatusHealthMetric plugin="[email protected]">
<success>true</success>
<failure>true</failure>
<unstable>true</unstable>
<unbuilt>true</unbuilt>
<countVirginJobs>false</countVirginJobs>
</com.cloudbees.hudson.plugins.folder.health.JobStatusHealthMetric>
<com.cloudbees.hudson.plugins.folder.health.ProjectEnabledHealthMetric plugin="[email protected]"/>
</healthMetrics>
<icon class="jenkins.branch.MetadataActionFolderIcon">
<owner class="jenkins.branch.OrganizationFolder" reference="../.."/>
</icon>
<orphanedItemStrategy class="com.cloudbees.hudson.plugins.folder.computed.DefaultOrphanedItemStrategy" plugin="[email protected]">
<pruneDeadBranches>true</pruneDeadBranches>
<daysToKeep>-1</daysToKeep>
<numToKeep>-1</numToKeep>
</orphanedItemStrategy>
<triggers>
<com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger plugin="[email protected]">
<spec>H H * * *</spec>
<interval>604800000</interval>
</com.cloudbees.hudson.plugins.folder.computed.PeriodicFolderTrigger>
</triggers>
<disabled>false</disabled>
<navigators>
<org.jenkinsci.plugins.github__branch__source.GitHubSCMNavigator plugin="[email protected]">
<repoOwner>$jobName</repoOwner>
<credentialsId>$credentialsId</credentialsId>
<traits>
<jenkins.scm.impl.trait.WildcardSCMSourceFilterTrait plugin="[email protected]">
<includes>$includes</includes>
<excludes>$excludes</excludes>
</jenkins.scm.impl.trait.WildcardSCMSourceFilterTrait>
<org.jenkinsci.plugins.github__branch__source.BranchDiscoveryTrait>
<strategyId>1</strategyId>
</org.jenkinsci.plugins.github__branch__source.BranchDiscoveryTrait>
<org.jenkinsci.plugins.github__branch__source.OriginPullRequestDiscoveryTrait>
<strategyId>1</strategyId>
</org.jenkinsci.plugins.github__branch__source.OriginPullRequestDiscoveryTrait>
<org.jenkinsci.plugins.github__branch__source.ForkPullRequestDiscoveryTrait>
<strategyId>1</strategyId>
<trust class="org.jenkinsci.plugins.github_branch_source.ForkPullRequestDiscoveryTrait\$TrustContributors"/>
</org.jenkinsci.plugins.github__branch__source.ForkPullRequestDiscoveryTrait>
</traits>
</org.jenkinsci.plugins.github__branch__source.GitHubSCMNavigator>
</navigators>
<projectFactories>
<org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProjectFactory plugin="[email protected]">
<scriptPath>Jenkinsfile</scriptPath>
</org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProjectFactory>
<com.cloudbees.workflow.multibranch.CustomMultiBranchProjectFactory plugin="[email protected]">
<factory>
<marker>pom.xml</marker>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="[email protected]">
<scm class="hudson.plugins.git.GitSCM" plugin="[email protected]">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>
https://github.com/beedemo/custom-marker-pipelines.git
</url>
<credentialsId>$credentialsId</credentialsId>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>*/master</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<submoduleCfg class="list"/>
<extensions/>
</scm>
<scriptPath>pom-Jenkinsfile</scriptPath>
<lightweight>true</lightweight>
</definition>
</factory>
</com.cloudbees.workflow.multibranch.CustomMultiBranchProjectFactory>
<com.cloudbees.workflow.multibranch.CustomMultiBranchProjectFactory plugin="[email protected]">
<factory>
<marker>Dockerfile</marker>
<definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="[email protected]">
<scm class="hudson.plugins.git.GitSCM" plugin="[email protected]">
<configVersion>2</configVersion>
<userRemoteConfigs>
<hudson.plugins.git.UserRemoteConfig>
<url>
https://github.com/beedemo/custom-marker-pipelines.git
</url>
<credentialsId>$credentialsId</credentialsId>
</hudson.plugins.git.UserRemoteConfig>
</userRemoteConfigs>
<branches>
<hudson.plugins.git.BranchSpec>
<name>*/master</name>
</hudson.plugins.git.BranchSpec>
</branches>
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
<submoduleCfg class="list"/>
<extensions/>
</scm>
<scriptPath>Dockerfile-Jenkinsfile</scriptPath>
<lightweight>true</lightweight>
</definition>
</factory>
</com.cloudbees.workflow.multibranch.CustomMultiBranchProjectFactory>
</projectFactories>
</jenkins.branch.OrganizationFolder>
"""
//need to check for: cb.IMProp.warProfiles bluesteel-core.json and move to bluesteel master folder for jobs to show up in Blue Ocean view
def folder
if(cbWarProfile == 'bluesteel-core.json') {
folder = j.getItemByFullName(masterName)
if (folder == null) {
println "ERROR: Folder '$masterName' not found"
return
}
} else {
folder = j.createProject(Folder.class, "$masterName");
}
// Add skip build to disable automatic build after scan
SkipFolderProperty skip = new SkipFolderProperty(System.currentTimeMillis() + 20 * 60 * 1000, 'beedemo-admin');
folder.addProperty(skip);
job = folder.createProjectFromXML(jobName, new ByteArrayInputStream(jobConfigXml.getBytes("UTF-8")));
job.save()
//the following will actually kickoff the initial scanning
ACL.impersonate(ACL.SYSTEM, new Runnable() {
@Override public void run() {
for (final SCMSourceOwner owner : SCMSourceOwners.all()) {
if (owner instanceof OrganizationFolder) {
OrganizationFolder orgFolder = (OrganizationFolder) owner;
for (GitHubSCMNavigator navigator : orgFolder.getNavigators().getAll(GitHubSCMNavigator.class)) {
orgFolder.scheduleBuild();
}
}
}
}
});
logger.info("created $jobName")
//saving job again to create webhook - not sure why it isn't created on initial save
job.save()
//create marker file to disable scripts from running twice
//but only disable after it has run successfully, allow setting environmental variables post master provisioning
disableScript.createNewFile()
} else {
logger.info("Environment property GITHUB_ORG_FOLDER_CRED_ID does not match UsernamePassword credential id for init_24_github_org_project script - skipping GitHub Org Folder creation")
}
} else {
logger.info("Environment property GITHUB_ORG_FOLDER_CRED_ID NOT set or NOT available for init_24_github_org_project script - skipping GitHub Org Folder creation")
}