I'm trying to automate the release process that we use in our company using Jenkins. The current flow is that we log into the Jenkins server at JENKINS-URL
of the customer and then create a new build by taking the old build-parameter values, change the version number, and run it.
I wanted to write a script that does this automatically for us, but i encountered a few issues.
I can't use the CLI over HTTP, because the Server lies behind an Apache and according to the docs there is no way to make a communication possible then.
Enabling CLI via SSH is also not possible because the customer doesn't want it.
My third approach was to just investigate what Jenkins is actually doing (looking at Network Tab and trying to figure out which cookies etc. are getting saved), and i found out that they are mainly using a JSESSIONID cookie to maintain the login session. So i tried first doing a POST against
JENKINS-URL
like this (The last curl is only considering the last line of thecookies.txt
since that line contains the cookie):
regex='(?<=Set-Cookie: ).*'
curl -i -X POST -F "j_username=<MY-USERNAME>" -F "j_password=<MY-PASSWORD>" <JENKINS-URL>/jenkins/j_spring_security_check > jenkins_login_response_headers.txt
cat jenkins_login_response_headers.txt | grep -oP "${regex}" > cookies.txt
curl --cookie '`tail -n +2 "cookies.txt"`' <JENKINS-URL>/jenkins/me/my-views/view/all/job/<MY-JOB>/196/parameters/
For some reason this does not work. The last request always returns a 403 - Authentication required.
Now it becomes weird though, because when i try the exact same flow via postman it works.
Then seeing the job gives a successful response (the HTML i need to retrieve data from the old build and copy it into the new one)
And as we can see it works! (It's a bit weird because i have to login twice to make it work, but it works. Via cURL/the command line i never got it to work :/). I thought the reason for this is maybe that postman uses a different cURL command in the background so i tried using the generated one that the postman interface provides.
curl --location --request POST '<JENKINS-URL>/jenkins/j_spring_security_check' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'j_username=<MY-USERNAME>' \
--data-urlencode 'j_password=<MY-PASSWORD>'
Unfortunately when i try to execute this curl request in the command line i get a 403 - No valid crumb was included in the request
error. Does anyone know how i can make progress with this and point out to me where my mistakes are? At this point i just don't know what to do anymore.
- Okay, i just found out that there is a REST API for Jenkins as well, maybe the whole process can be simplified with that. I will try it out tomorrow. If someone has the experience and is willing to share his quick, easy ways for doing this i am open for all possibilities!