Skip to content

Commit 795eb04

Browse files
authored
Merge pull request aerogear-attic#2 from aliok/AGDIGGER-59-trigger-jenkins-job
AGDIGGER-59 trigger jenkins job
2 parents 78ed493 + b340fc4 commit 795eb04

File tree

13 files changed

+521
-33
lines changed

13 files changed

+521
-33
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# EditorConfig is awesome: http://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
# Unix-style newlines with a newline ending every file
7+
[*]
8+
end_of_line = lf
9+
insert_final_newline = true
10+
charset = utf-8
11+
indent_style = space
12+
indent_size = 2

.gitignore

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
target/
2+
23
pom.xml.tag
34
pom.xml.releaseBackup
45
pom.xml.versionsBackup
@@ -7,7 +8,11 @@ release.properties
78
dependency-reduced-pom.xml
89
buildNumber.properties
910
.mvn/timing.properties
10-
.idea
11-
jenkins-client.iml
11+
1212
# Exclude maven wrapper
1313
!/.mvn/wrapper/maven-wrapper.jar
14+
15+
# Intellij Idea files
16+
.idea
17+
*.iml
18+

README.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,18 @@ Create job:
1111
client.createJob("java-client-job1","https://github.com/wtrocki/helloworld-android-gradle","master");
1212
```
1313

14+
Trigger a job:
15+
16+
```
17+
...
18+
BuildStatus buildStatus = client.build("java-client-job1");
19+
```
20+
1421
## Requirements
1522

1623
Client works with Java6 and above.
1724

1825
## Building
1926

20-
`mvn clean package`
27+
`mvn clean package`
28+

pom.xml

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,56 @@
1818
</licenses>
1919

2020
<dependencies>
21+
<!--Compile time-->
2122
<dependency>
2223
<groupId>com.offbytwo.jenkins</groupId>
2324
<artifactId>jenkins-client</artifactId>
24-
<version>0.3.6</version>
25+
<version>0.3.7-SNAPSHOT</version>
2526
</dependency>
2627
<dependency>
2728
<groupId>org.jtwig</groupId>
2829
<artifactId>jtwig-core</artifactId>
2930
<version>5.65</version>
3031
</dependency>
32+
<dependency>
33+
<groupId>org.slf4j</groupId>
34+
<artifactId>slf4j-api</artifactId>
35+
<version>1.7.21</version>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.slf4j</groupId>
39+
<artifactId>slf4j-log4j12</artifactId>
40+
<version>1.7.21</version>
41+
</dependency>
42+
43+
<!--Testing-->
44+
<dependency>
45+
<groupId>junit</groupId>
46+
<artifactId>junit</artifactId>
47+
<version>4.12</version>
48+
<scope>test</scope>
49+
</dependency>
50+
51+
<dependency>
52+
<groupId>org.mockito</groupId>
53+
<artifactId>mockito-core</artifactId>
54+
<version>1.10.19</version>
55+
<scope>test</scope>
56+
</dependency>
57+
<dependency>
58+
<groupId>org.assertj</groupId>
59+
<artifactId>assertj-core</artifactId>
60+
<version>3.6.1</version>
61+
<scope>test</scope>
62+
</dependency>
3163
</dependencies>
3264
<repositories>
3365
<repository>
3466
<id>jcenter</id>
3567
<url>https://jcenter.bintray.com/</url>
3668
</repository>
3769
</repositories>
70+
3871
<build>
3972
<plugins>
4073
<plugin>
@@ -46,6 +79,11 @@
4679
<target>1.6</target>
4780
</configuration>
4881
</plugin>
82+
<plugin>
83+
<groupId>org.apache.maven.plugins</groupId>
84+
<artifactId>maven-surefire-plugin</artifactId>
85+
<version>2.12.4</version>
86+
</plugin>
4987
</plugins>
5088
</build>
51-
</project>
89+
</project>
Lines changed: 79 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,58 @@
11
package com.redhat.digkins;
22

33
import com.offbytwo.jenkins.JenkinsServer;
4+
import com.redhat.digkins.model.BuildStatus;
45
import com.redhat.digkins.services.CreateJobService;
5-
import com.redhat.digkins.util.JenkinsAuth;
6+
import com.redhat.digkins.services.TriggerBuildService;
67
import com.redhat.digkins.util.DiggerClientException;
8+
import com.redhat.digkins.util.JenkinsAuth;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
711

12+
import java.io.IOException;
813
import java.net.URI;
914
import java.net.URISyntaxException;
1015

1116
/**
12-
* Digger Java Client
13-
* <p>
14-
* Interact with digger jenkins api!
17+
* Digger Java Client interact with Digger Jenkins api.
1518
*/
1619
public class DiggerClient {
1720

21+
private static final Logger LOG = LoggerFactory.getLogger(DiggerClient.class);
22+
23+
public static final long DEFAULT_BUILD_TIMEOUT = 60 * 1000;
24+
1825
private final JenkinsServer jenkins;
1926

2027
public DiggerClient(JenkinsAuth auth) throws URISyntaxException {
2128
this.jenkins = new JenkinsServer(new URI(auth.getUrl()), auth.getUser(), auth.getPassword());
2229
}
2330

2431
/**
25-
* Create new digger job on jenkins platform
32+
* Create client using provided url and credentials
33+
*
34+
* @param url Jenkins url
35+
* @param user Jenkins user
36+
* @param password Jenkins password
37+
* @return client instance
38+
* @throws DiggerClientException if something goes wrong
39+
*/
40+
public static DiggerClient from(String url, String user, String password) throws DiggerClientException {
41+
try {
42+
JenkinsAuth jenkinsAuth = new JenkinsAuth(url, user, password);
43+
return new DiggerClient(jenkinsAuth);
44+
} catch (URISyntaxException e) {
45+
throw new DiggerClientException("Invalid jenkins url format.");
46+
}
47+
}
48+
49+
/**
50+
* Create new Digger job on Jenkins platform
2651
*
27-
* @param name - job name that can be used later to reference job
28-
* @param gitRepo - git repository url (full git repository url. e.g [email protected]:wtrocki/helloworld-android-gradle.git
29-
* @param gitBranch - git repository branch (default branch used to checkout source code)
52+
* @param name job name that can be used later to reference job
53+
* @param gitRepo git repository url (full git repository url. e.g [email protected]:wtrocki/helloworld-android-gradle.git
54+
* @param gitBranch git repository branch (default branch used to checkout source code)
55+
* @throws DiggerClientException if something goes wrong
3056
*/
3157
public void createJob(String name, String gitRepo, String gitBranch) throws DiggerClientException {
3258
CreateJobService service = new CreateJobService(this.jenkins);
@@ -38,19 +64,54 @@ public void createJob(String name, String gitRepo, String gitBranch) throws Digg
3864
}
3965

4066
/**
41-
* Create client using provided url and credentials
67+
* Triggers a build for the given job and waits until it leaves the queue and actually starts.
68+
* <p>
69+
* Jenkins puts the build requests in a queue and once there is a slave available, it starts building
70+
* it and a build number is assigned to the build.
71+
* <p>
72+
* This method will block until there is a build number, or the given timeout period is passed. If the build is still in the queue
73+
* after the given timeout period, a {@code BuildStatus} is returned with state {@link BuildStatus.State#TIMED_OUT}.
74+
* <p>
75+
* Please note that timeout period is never meant to be very precise. It has the resolution of {@link TriggerBuildService#POLL_PERIOD} because
76+
* timeout is checked before every pull.
77+
* <p>
78+
* Similarly, {@link BuildStatus.State#CANCELLED_IN_QUEUE} is returned if the build is cancelled on Jenkins side and
79+
* {@link BuildStatus.State#STUCK_IN_QUEUE} is returned if the build is stuck.
4280
*
43-
* @param url - jenkins url
44-
* @param user - jenkins user
45-
* @param password - jenkins password
46-
* @return client instance
81+
* @param jobName name of the job to trigger the build
82+
* @param timeout how many milliseconds should this call block before returning {@link BuildStatus.State#TIMED_OUT}.
83+
* Should be larger than {@link TriggerBuildService#FIRST_CHECK_DELAY}
84+
* @return the build status
85+
* @throws DiggerClientException if connection problems occur during connecting to Jenkins
4786
*/
48-
public static DiggerClient from(String url, String user, String password) throws DiggerClientException {
87+
public BuildStatus build(String jobName, long timeout) throws DiggerClientException {
88+
final TriggerBuildService triggerBuildService = new TriggerBuildService(jenkins);
4989
try {
50-
JenkinsAuth jenkinsAuth = new JenkinsAuth(url, user, password);
51-
return new DiggerClient(jenkinsAuth);
52-
} catch (URISyntaxException e) {
53-
throw new DiggerClientException("Invalid jenkins url format.");
90+
return triggerBuildService.build(jobName, timeout);
91+
} catch (IOException e) {
92+
LOG.debug("Exception while connecting to Jenkins", e);
93+
throw new DiggerClientException(e);
94+
} catch (InterruptedException e) {
95+
LOG.debug("Exception while waiting on Jenkins", e);
96+
throw new DiggerClientException(e);
97+
} catch (Throwable e) {
98+
LOG.debug("Exception while triggering a build", e);
99+
throw new DiggerClientException(e);
54100
}
55101
}
102+
103+
/**
104+
* Triggers a build for the given job and waits until it leaves the queue and actually starts.
105+
* <p>
106+
* Calls {@link #build(String, long)} with a default timeout of {@link #DEFAULT_BUILD_TIMEOUT}.
107+
*
108+
* @param jobName name of the job
109+
* @return the build status
110+
* @throws DiggerClientException if connection problems occur during connecting to Jenkins
111+
* @see #build(String, long)
112+
*/
113+
public BuildStatus build(String jobName) throws DiggerClientException {
114+
return this.build(jobName, DEFAULT_BUILD_TIMEOUT);
115+
}
116+
56117
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.redhat.digkins.model;
2+
3+
/**
4+
* Represents the status of a build.
5+
* <p>
6+
* The field {@link #buildNumber} will only be set if the
7+
* {@link #state} is {@link State#BUILDING}.
8+
**/
9+
public class BuildStatus {
10+
11+
public enum State {
12+
/**
13+
* Build is out of the queue and it is currently being executed.
14+
*/
15+
BUILDING,
16+
17+
/**
18+
* The max time to wait for the build get executed has passed.
19+
* This state doesn't have to mean build is stuck or etc.
20+
* It just means, the max waiting time has passed on the client side.
21+
*/
22+
TIMED_OUT,
23+
24+
/**
25+
* The build is cancelled in Jenkins before it started being executed.
26+
*/
27+
CANCELLED_IN_QUEUE,
28+
29+
/**
30+
* The build is stuck on Jenkins queue.
31+
*/
32+
STUCK_IN_QUEUE
33+
}
34+
35+
private final State state;
36+
private final int buildNumber;
37+
38+
public BuildStatus(State state, int buildNumber) {
39+
this.state = state;
40+
this.buildNumber = buildNumber;
41+
}
42+
43+
/**
44+
* @return state of the build
45+
*/
46+
public State getState() {
47+
return state;
48+
}
49+
50+
/**
51+
* This should only be valid if the
52+
* {@link #state} is {@link State#BUILDING}.
53+
*
54+
* @return the build number assigned by Jenkins
55+
*/
56+
public int getBuildNumber() {
57+
return buildNumber;
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return "BuildStatus{" +
63+
"state=" + state +
64+
", buildNumber=" + buildNumber +
65+
'}';
66+
}
67+
}

src/main/java/com/redhat/digkins/services/CreateJobService.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
11
package com.redhat.digkins.services;
22

33
import com.offbytwo.jenkins.JenkinsServer;
4-
import org.apache.commons.io.FileUtils;
54
import org.jtwig.JtwigModel;
65
import org.jtwig.JtwigTemplate;
76

8-
import java.io.File;
97
import java.io.IOException;
108

119
/**
1210
* Create digger job on jenkins platform
1311
*/
1412
public class CreateJobService {
1513

16-
private JenkinsServer jenkins;
14+
private final static String GIT_REPO_URL = "GIT_REPO_URL";
15+
private final static String GIT_REPO_BRANCH = "GIT_REPO_BRANCH";
1716

18-
private final static String GIT_REPO_URL = "GIT_REPO_URL", GIT_REPO_BRANCH = "GIT_REPO_BRANCH";
17+
private JenkinsServer jenkins;
1918

2019
/**
21-
* @param jenkins - jenkins api instance
20+
* @param jenkins jenkins api instance
2221
*/
2322
public CreateJobService(JenkinsServer jenkins) {
2423
this.jenkins = jenkins;
@@ -27,9 +26,9 @@ public CreateJobService(JenkinsServer jenkins) {
2726
/**
2827
* Create new digger job on jenkins platform
2928
*
30-
* @param name - job name that can be used later to reference job
31-
* @param gitRepo - git repository url (full git repository url. e.g [email protected]:digger/helloworld.git
32-
* @param gitBranch - git repository branch (default branch used to checkout source code)
29+
* @param name job name that can be used later to reference job
30+
* @param gitRepo git repository url (full git repository url. e.g [email protected]:digger/helloworld.git
31+
* @param gitBranch git repository branch (default branch used to checkout source code)
3332
*/
3433
public void create(String name, String gitRepo, String gitBranch) throws IOException {
3534
JtwigTemplate template = JtwigTemplate.classpathTemplate("templates/job.xml");

0 commit comments

Comments
 (0)