Skip to content

Commit 853737b

Browse files
committed
Added API
1 parent be0d92e commit 853737b

File tree

5 files changed

+184
-0
lines changed

5 files changed

+184
-0
lines changed

_includes/hired.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<center>
2+
This video sponsored by <a href="http://www.hired.com/devopslibrary">Hired.com</a>
3+
<br><span style="font-size: 80%;"><i>Thank you for supporting the DevOps Community!</i></span>
4+
<a href="http://www.hired.com/?utm_source=sponsor&utm_medium=devopslibrary&utm_content=green&utm_campaign=q3-16"><img src="/images/hiredgreen.png"></a><br><br>
5+
</center>
Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
---
2+
layout: post
3+
title: "CCJPE: Jenkins API Tutorial"
4+
date: 2016-8-5 12:00:00 -0500
5+
categories: Jenkins
6+
permalink: lessons/ccjpe-api
7+
excerpt: "Don't worry, the Jenkins API is extremely easy to use!"
8+
weight: 10
9+
image: 'jenkinscourse.png'
10+
11+
---
12+
{% include youtube.html id="ID1S2m9wtrk" %}
13+
{% include hired.html %}
14+
15+
Introduction
16+
------------
17+
Welcome back to the DevOps Library! This is Samantha, we’re glad you found
18+
yourself here! In today's lesson, we're going to learn how to use the Jenkins
19+
API. Don't worry, the hardest part about using the API is just deciding what
20+
you'd like to build with it. Maybe you want to create a custom dashboard with
21+
the status of recently triggered jobs…or even have a totally separate
22+
application trigger a Jenkins build. Whatever you decide, it's completely up
23+
to you.
24+
25+
Thank you Hired.com!
26+
--------------------
27+
But before we begin, we’re excited to announce we have partnered with
28+
[Hired.com](http://www.hired.com/devopslibrary),
29+
who will be sponsoring the remaining videos in our Jenkins course! Because of
30+
their generous support, we are now able to bring the rest of the course to you,
31+
with quicker release time for each video.
32+
33+
In case you aren’t familiar with
34+
Hired, it’s a really cool company that actually reverses the traditional job
35+
search, by having companies apply to you, instead of you always doing the
36+
tedious work of applying. Thousands of companies are looking to Hired to help
37+
connect with the best of the best in our field, like you! Be sure to listen at
38+
the end of this video for more information on how Hired works and how it could
39+
become your go-to tool for finding your next job!
40+
41+
OH and if you do end up signing up through our own personal link, and landing a
42+
new job too, Hired will give you a 2k bonus for being a loyal supporter of the
43+
DevOps library!
44+
45+
Getting Started
46+
---------------
47+
Ok are you ready? Let’s go ahead and get started!
48+
49+
First, let us begin by pulling up the API documentation on our Jenkins server.
50+
To do so, open up a web browser and go to:
51+
52+
53+
`http://jenkinsMasterURL/api`
54+
55+
![API](/images/api.png)
56+
57+
As you can see, the REST API provides three different ways to interact with it.
58+
You can use XML, JSON, or even directly access Python objects.
59+
60+
For this lesson, we'll focus on JSON, so go ahead and click the JSON-API link to
61+
see some of the data that Jenkins returns. If you look at the "jobs" array, you
62+
should see the class, name, URL, and color of each job, with "blue" listed for
63+
successful builds, and "red" for failures. Pretty cool huh?
64+
65+
![API](/images/api2.png)
66+
67+
Using Curl
68+
----------
69+
So far we've been accessing the API directly through our browser, but typically you'll want to interact with it programmatically. Let's try accessing the API using Curl.
70+
71+
Open up a terminal, and type:
72+
73+
`curl http://jenkinsMasterURL/api/json?pretty=true`
74+
75+
Oops! If your Jenkins server requires authentication (and it SHOULD), you'll
76+
see a message saying "*Authentication Required*". The Jenkins API uses HTTP
77+
BASIC authentication and requires a username as well as an API token to connect.
78+
79+
To obtain the token, we need to pull up our personal config page. In your
80+
browser, type:
81+
82+
`http://jenkinsMasterURL/me/configure`
83+
84+
Then click the box named "**Show API Token**", and copy the token to your
85+
clipboard.
86+
87+
Alright, we should be good to go now. Switch back to your terminal, and type:
88+
89+
`curl -u username:apiToken "http://jenkinsMasterURL/api/json?pretty=true"`
90+
91+
Perfect!! There we go! Now that we've figured out how to authenticate, let's
92+
try to do something a little more advanced.
93+
94+
Filtering Jenkins API Data
95+
--------------------------
96+
What if, instead of returning everything, we only want to see the name and color
97+
of our jobs? First, let's start out with the last command that we ran.
98+
99+
We do need to add a **-g** this time to disable **globbing**. Next, at the end
100+
of the URL, we'll use what's called a "**tree**" query to filter what's
101+
returned.
102+
103+
Add:
104+
105+
`tree=jobs[name,color]`
106+
107+
to the end of our URL.
108+
109+
This tells Jenkins that we only want to return the data for our jobs, and
110+
specifically only the name and color properties of the object. If you'd like to
111+
limit the number of jobs returned, you can always use braces for range
112+
specifiers. For example a 2,5 within braces would only return the 2nd through
113+
fifth elements in the jobs array, or a 3 within braces would only return the 3rd
114+
element.
115+
116+
Editing a Job using the API
117+
---------------------------
118+
Neat huh? For our last example, let's try to do something really fancy. We're
119+
going to download the config.xml from a job that's been failing, fix the
120+
configuration, then push the changes back to Jenkins, all using only the API!
121+
122+
The Jenkins job that we need to fix is called failingjob01. First, let's
123+
download the **config.xml** file. Run:
124+
125+
`curl -u username:apiToken http://jenkinsMasterURL/job/failingjob.01/config.xml`
126+
127+
Quick exam tip, make sure you note down the path to download a job
128+
**config.xml**. It will always be your JenkinsURL/job/nameOfJob/config.xml.
129+
There's a good chance you'll see a question like that on the test!
130+
131+
Alright, back to what we were doing. Open up the **config.xml** file that we
132+
just downloaded, make any changes that you'd like. Then once you're finished
133+
we have one final command to run.
134+
135+
Type:
136+
`curl -X POST -u username:apiToken "http://jenkins/job/failingjob.01/config.xml" -d "@config.xml"`
137+
138+
Now refresh the page to see the changes we just made to our Jenkins job.
139+
140+
Perfect!! You now know how to authenticate, filter results, and even edit jobs all through the Jenkins API! Great job!
141+
142+
About Hired
143+
-----------
144+
As always thanks for watching, and special thanks goes to Hired for sponsoring
145+
this course. As I mentioned earlier, if you're into DevOps, there's a pretty
146+
good chance you've had to deal with pushy recruiters and countless emails, as
147+
well as spent many hours on your own searching for good DevOps opportunities,
148+
even applying to a few along the way.
149+
150+
The reason we love using Hired is because it completely reverses this situation
151+
and puts the power back in your hands, by having companies send you interview
152+
requests that you can choose to pursue. (They even come with upfront salary
153+
and equity!)
154+
155+
By having you fill out information that is specific to what you’re looking for
156+
and what you feel your individual strengths and talents are, it ensures that the
157+
only companies you'll hear from will be a great fit for you. Plus, Hired is
158+
completely free for you, and they’ll even give you a $2,000 bonus after you
159+
land a job, using the DevOps library link!
160+
161+
By the way, you’re never on your own during your job search when using Hired,
162+
they provide an unbiased talent advocate to help you present your talents in
163+
the right way to the right employer.
164+
165+
Ok, so if you are wondering how to sign up, just head to
166+
[hired.com/devopslibrary](http://www.hired.com/devopslibrary). Type in your
167+
email, then hit "Get Job Offers.” On the next page, just fill in your name and
168+
password, then you're ready to complete the talent profile. As you can see, the
169+
whole process is extremely easy and straightforward.
170+
171+
Ok, so now you’re in the system, look around the site a bit, and see what Hired
172+
can offer you. We highly recommend giving them a shot, they really do a
173+
fantastic job, especially for our DevOps community.
174+
175+
Thanks again for watching today, we'll see you again soon!
176+
177+
[Subscribe to our YouTube channel](https://www.youtube.com/channel/UCOnioSzUZS-ZqsRnf38V2nA?sub_confirmation=1) or follow [DevOpsLibrary on Twitter](https://twitter.com/intent/user?screen_name=devopslibrary).
178+
179+
{% include subscribe.html %}

images/api.png

182 KB
Loading

images/api2.png

120 KB
Loading

images/hiredgreen.png

12.7 KB
Loading

0 commit comments

Comments
 (0)