Skip to content

Commit 796661e

Browse files
committedSep 28, 2015
Merge pull request cfpb#8 from imuchnik/master
JS Job Builder
2 parents ca66c10 + ba6c95a commit 796661e

10 files changed

+1521
-2
lines changed
 
0 Bytes
Binary file not shown.
17.3 KB
Binary file not shown.
50.9 KB
Binary file not shown.
5.68 KB
Binary file not shown.

‎build.gradle

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ group 'gov.cfpb'
22
version '1.0-SNAPSHOT'
33

44
task wrapper(type: Wrapper) {
5-
gradleVersion = '2.2'
6-
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
5+
gradleVersion = '2.2'
6+
distributionUrl = "https://services.gradle.org/distributions/gradle-$gradleVersion-all.zip"
77
}
88

99
apply plugin: 'groovy'
@@ -13,9 +13,18 @@ sourceCompatibility = 1.5
1313

1414
repositories {
1515
mavenCentral()
16+
jcenter()
17+
maven { url 'http://repo.jenkins-ci.org/releases/' }
1618
}
1719

1820
dependencies {
21+
compile gradleApi()
22+
compile 'org.ini4j:ini4j:0.5.1'
23+
compile "org.yaml:snakeyaml:1.10"
1924
compile 'org.codehaus.groovy:groovy-all:2.3.11'
2025
testCompile group: 'junit', name: 'junit', version: '4.11'
26+
compile 'org.jenkins-ci.plugins:job-dsl-core:1.37'
27+
testCompile('org.spockframework:spock-core:0.7-groovy-2.0') {
28+
exclude module: 'groovy-all'
29+
}
2130
}

‎jenkins.iml

+1,386
Large diffs are not rendered by default.

‎jobs/SampleJsBuildJob.groovy

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import JsJobBuilder
2+
3+
4+
String basePath = 'JsJobSamples'
5+
List developers = ['irina.muchnik@cfpb.gov', 'daniel.davis@cfpb.gov']
6+
def repos = [
7+
[name: 'jenkins-automation', url: "https://github.com/cfpb/jenkins-automation"],
8+
[name: 'collab', url: "https://github.com/cfpb/jenkins-automation"]
9+
]
10+
folder(basePath) {
11+
description 'This example shows how to create jobs using Job builders.'
12+
}
13+
14+
new JsJobBuilder(
15+
name: "$basePath/SampleJob1",
16+
description: 'An example using a job builder for a Javascript build jobs project.',
17+
repos: repos,
18+
emails: developers
19+
).build(this)
20+

‎lib/ini4j-0.5.4.jar

-99.8 KB
Binary file not shown.

‎src/main/groovy/JsJobBuilder.groovy

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import javaposse.jobdsl.dsl.DslFactory
2+
import javaposse.jobdsl.dsl.Job
3+
import ScmUtils
4+
class JsJobBuilder {
5+
6+
String name
7+
String description
8+
String gitBranch = 'master'
9+
String pollScmSchedule = '@daily'
10+
String tasks
11+
String junitResults = '**/build/test-results/*.xml'
12+
String artifacts = 'dist/'
13+
List<String> emails
14+
def repos =[];
15+
16+
17+
Job build(DslFactory dslFactory) {
18+
dslFactory.job(name) {
19+
it.description this.description
20+
wrappers {
21+
colorizeOutput()
22+
nodejs('Node 0.12')// pass in the version?
23+
}
24+
25+
multiscm {
26+
ScmUtils.project_repos(delegate, this.repos)
27+
}
28+
29+
triggers {
30+
scm pollScmSchedule
31+
}
32+
steps {
33+
shell( //we can potentially pass those in as well - $DIR_TO_BUILD and build script name
34+
'''
35+
cd $DIR_TO_BUILD
36+
./frontendbuild.sh
37+
'''
38+
)
39+
}
40+
publishers {
41+
archiveArtifacts artifacts
42+
if (emails) {
43+
mailer emails.join(' ')
44+
}
45+
}
46+
}
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.util.regex.Pattern
2+
import java.util.regex.Matcher
3+
import org.yaml.snakeyaml.Yaml
4+
import org.ini4j.Ini
5+
6+
static String[] parseRepositories(def reposText) {
7+
Yaml yaml = new Yaml()
8+
def obj = yaml.load(reposText)
9+
def reposArray = obj.get('repositories', obj.get('public_repositories', obj.get('private_repositories', [])))
10+
return reposArray.collect { // don't use each!!!
11+
it.contains('@') ? it.substring(0, it.indexOf('@')) : it
12+
}
13+
return reposArray
14+
}
15+
16+
static String getRepoOriginUrl(def context, def repoName) {
17+
def gitConfigStream = context.streamFileFromWorkspace(repoName + '/.git/config')
18+
def gitConfig = new Ini(gitConfigStream)
19+
return gitConfig.get('remote "origin"').get('url')
20+
}
21+
22+
static String[] getRepositoryUrls(def context, def repoName) {
23+
def repositories, repositoryYml, privateReposFilePath, publicReposFilePath, projectName, privateName
24+
def privateMatcher = repoName =~ /(?<projectName>.*)-private/
25+
if (privateMatcher.matches()) {
26+
projectName = privateMatcher.group('projectName')
27+
privateName = repoName
28+
privateReposFilePath = './' + privateName + '/ansible/group_vars/all/repositories.yml'
29+
} else {
30+
projectName = repoName
31+
}
32+
publicReposFilePath = './' + projectName + '/ansible/group_vars/all/repositories.yml'
33+
repositories = []
34+
if (privateName) {
35+
repositories << getRepoOriginUrl(context, privateName)
36+
if (fileExistsInWorkspace(context, privateReposFilePath)) {
37+
repositories.addAll(parseRepositories(context.readFileFromWorkspace(privateReposFilePath)))
38+
}
39+
}
40+
if (projectName) {
41+
repositories << getRepoOriginUrl(context, projectName)
42+
if (fileExistsInWorkspace(context, publicReposFilePath)) {
43+
repositories.addAll(parseRepositories(context.readFileFromWorkspace(publicReposFilePath)))
44+
}
45+
}
46+
return repositories
47+
}
48+
49+
static boolean fileExistsInWorkspace(def context, def path){
50+
try {
51+
context.readFileFromWorkspace(path)
52+
return true
53+
} catch (java.lang.IllegalArgumentException e){
54+
return false
55+
}
56+
}

0 commit comments

Comments
 (0)
Please sign in to comment.