An Interest In:
Web News this Week
- April 13, 2024
- April 12, 2024
- April 11, 2024
- April 10, 2024
- April 9, 2024
- April 8, 2024
- April 7, 2024
[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>
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
Original Link: https://dev.to/blackthor/week1-days-5-how-to-start-1cd3
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To