[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 getti…



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>



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
    }
};

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) ; 
            }   
        })
    })  

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


Print Share Comment Cite Upload Translate
APA
Black-Thor | Sciencx (2024-03-28T11:54:04+00:00) » [week1] Days 5 – I call jenkins API with a express app. Retrieved from https://www.scien.cx/2022/01/07/week1-days-5-i-call-jenkins-api-with-a-express-app/.
MLA
" » [week1] Days 5 – I call jenkins API with a express app." Black-Thor | Sciencx - Friday January 7, 2022, https://www.scien.cx/2022/01/07/week1-days-5-i-call-jenkins-api-with-a-express-app/
HARVARD
Black-Thor | Sciencx Friday January 7, 2022 » [week1] Days 5 – I call jenkins API with a express app., viewed 2024-03-28T11:54:04+00:00,<https://www.scien.cx/2022/01/07/week1-days-5-i-call-jenkins-api-with-a-express-app/>
VANCOUVER
Black-Thor | Sciencx - » [week1] Days 5 – I call jenkins API with a express app. [Internet]. [Accessed 2024-03-28T11:54:04+00:00]. Available from: https://www.scien.cx/2022/01/07/week1-days-5-i-call-jenkins-api-with-a-express-app/
CHICAGO
" » [week1] Days 5 – I call jenkins API with a express app." Black-Thor | Sciencx - Accessed 2024-03-28T11:54:04+00:00. https://www.scien.cx/2022/01/07/week1-days-5-i-call-jenkins-api-with-a-express-app/
IEEE
" » [week1] Days 5 – I call jenkins API with a express app." Black-Thor | Sciencx [Online]. Available: https://www.scien.cx/2022/01/07/week1-days-5-i-call-jenkins-api-with-a-express-app/. [Accessed: 2024-03-28T11:54:04+00:00]
rf:citation
» [week1] Days 5 – I call jenkins API with a express app | Black-Thor | Sciencx | https://www.scien.cx/2022/01/07/week1-days-5-i-call-jenkins-api-with-a-express-app/ | 2024-03-28T11:54:04+00:00
https://github.com/addpipe/simple-recorderjs-demo