This repository has been archived by the owner on Apr 20, 2022. It is now read-only.
/
jenkins_pipeline_crawler_sample.groovy
69 lines (60 loc) · 2.74 KB
/
jenkins_pipeline_crawler_sample.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
import javaposse.jobdsl.dsl.DslFactory
import org.springframework.cloud.pipelines.common.GeneratedJobs
import org.springframework.cloud.pipelines.common.PipelineDefaults
import org.springframework.cloud.pipelines.common.PipelineDescriptor
import org.springframework.cloud.pipelines.common.PipelineFactory
import org.springframework.cloud.pipelines.common.PipelineJobsFactory
import org.springframework.cloud.pipelines.common.PipelineJobsFactoryProvider
import org.springframework.cloud.pipelines.default_pipeline.DefaultPipelineJobsFactory
import org.springframework.cloud.pipelines.default_pipeline.DefaultView
import org.springframework.cloud.pipelines.test.TestUtils
import org.springframework.cloud.projectcrawler.OptionsBuilder
import org.springframework.cloud.projectcrawler.Repository
import org.springframework.cloud.projectcrawler.ProjectCrawler
/**
* This script contains logic that
*
* - uses the Project Crawler to crawl repos from an organization
* - for each repo picks the sc-pipelines.yml descriptor
* - basing on the contents of that repo generates proper deployment pipeline
* - generates views for all projects
*/
DslFactory dsl = this
// These will be taken either from seed or global variables
PipelineDefaults defaults = new PipelineDefaults(binding.variables)
String pipelineVersion = defaults.pipelineVersion()
String org = defaults.repoOrganization() ?: "sc-pipelines"
String repoType = defaults.repoManagement() ?: "GITHUB"
String urlRoot = defaults.repoUrlRoot() ?: "https://github.com"
// crawl the org
ProjectCrawler crawler = new ProjectCrawler(OptionsBuilder
.builder().rootUrl(urlRoot)
.username(defaults.gitUsername())
.password(defaults.gitPassword())
.token(defaults.gitToken())
.exclude(defaults.repoProjectsExcludePattern())
.repository(repoType).build())
// get the repos from the org
List<Repository> repositories = defaults.testModeDescriptor() != null ?
TestUtils.TEST_REPO : crawler.repositories(org)
// generate jobs and store errors
GeneratedJobs generatedJobs = new PipelineFactory(
new PipelineJobsFactoryProvider() {
@Override
PipelineJobsFactory get(PipelineDefaults pipelineDefaults, DslFactory dslFactory, PipelineDescriptor descriptor, Repository repository) {
return new DefaultPipelineJobsFactory(pipelineDefaults, descriptor, dslFactory)
}
@Override
List<String> additionalFiles() {
return []
}
}, defaults, crawler, dsl).generate(repositories, org, pipelineVersion)
if (generatedJobs.hasErrors()) {
println "\n\n\nWARNING, THERE WERE ERRORS WHILE TRYING TO BUILD PROJECTS\n\n\n"
generatedJobs.errors.each { String key, Exception error ->
println "Exception for project [${key}], [${error}]"
println "Stacktrace:"
error.printStackTrace()
}
}
new DefaultView(defaults, dsl).allViews(repositories)