0

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.

  1. 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.

  2. Enabling CLI via SSH is also not possible because the customer doesn't want it.

  3. 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 the cookies.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. Successful Login returns the JSESSIONID Cookie

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)

Successful job parameter response, with HTML containing the old build data

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.

  1. 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!
Arun Kumar B
  • 196
  • 9
Saltuk Kezer
  • 105
  • 1
  • 7
  • I would consider leveraging the Github Jenkins plugin. Once you push a release version to a specific branch, it triggers a new build job. The release value could be propagated to the Jenkins job from a base code – Denis Voloshin Jan 14 '22 at 21:49

1 Answers1

0

You could use the Trigger builds remotely (e.g., from scripts) option under Build Triggers: enter image description here Refer to this existing SO answer for detailed information.

For remote execution of parameterized jobs refer to Launching a build with parameters section in Parameterized Build.

Arun Kumar B
  • 196
  • 9