DEV Community

Cover image for [week1] Days 5 - I call jenkins API with a express app
Black-Thor
Black-Thor

Posted on • Updated on

[week1] Days 5 - I call jenkins API with a express app

School Project

So today i was working on a school project , and i made a call api consumption from my express app to a jenkins REST API

Context

We're making an api with node.js / Express , this call the api of Jenkins for getting the result of a job

.env

For making this API we use a .env file wich contain the data necessary to access jenkins, like the user or the token link to the user, we define it like this.

JOB_USER = <jenkins_user>
JOB_TOKEN = <jenkins_token>

JENKINS_URL = <Jenkins url>
Enter fullscreen mode Exit fullscreen mode

Create express server

For insure that our api run proprelie , we created an application/server. You can find information on how to make one here

Jenkins Api Call

We make options for gettings informations from our .env like this

let options = {
    url : "",
    method: 'HEAD',
    auth: {
        'user': process.env.JOB_USER,
        'pass': process.env.JOB_TOKEN
    }
};
Enter fullscreen mode Exit fullscreen mode

now we can make our routage

    app.get("/jenkins", (req,res) => {
        options.url = `${process.env.JENKINS_URL}/job/[you folder]/job/[you job]/lastBuild/api/json?pretty=true`
        request.get(options, (err, response) => {
            if (!err){                
                let status = JSON.parse(response.body)
                res.send(status.result)
            }
            else {
                res.send(err) ; 
            }   
        })
    })  
Enter fullscreen mode Exit fullscreen mode

And with that i can now if my build run properly or i faced some trouble .

Thx for reading , hope you loved it !

PS:

if you see any spelling or grammar mistakes, can you notify me (I am still improving my English) ? thank you !
if you have any tips for improving my post feel free to comment on the post

Top comments (0)