Trigger Jenkins job from Gitlab CI with parameters
Solution 1
Here's the way I do it: no plugin required, just triggering Jenkins api
from gitlab-ci.
Gitlab-CI
I will assume you have a gitlab-ci runner installed and configured.
First, you need to have a .gitlab-ci.yml
file in your project having a basic structure such as:
stages:
- my-jenkins-trigger
variables:
MY_VARIABLE: "EVERYTHING_IS_AWESOME"
my-jenkins-trigger-job:
stage: my-jenkins-trigger
script: curl -i -X POST --user JENKINS_USER:JENKINS_TOKEN JENKINS_JOB_URL/buildWithParameters?MY_JENK_PARAM=${MY_VARIABLE}
In the above, I also assume
- You have a Jenkins job somewhere at URL
JENKINS_JOB_URL
- this Jenkins job has a build parameter called
MY_VARIABLE
-
JENKINS_USER
,JENKINS_TOKEN
are defined [*]
That simple?
Well yes, but no...
That is the rough structure. That script will merely trigger a Jenkins job and forget about it. You need to work a little more to monitor the job and feed its status back in Gitlab-CI, manage security and possibly get some commit info from gitlab to inject into your job.
Monitoring
In order to have a proper monitoring, I recommand to write a full trigger + monitor + return value script [** ] (in whatever language available or you're familiar with).
Just start by triggering the job as I stated above.
Then, run a while
loop (don't forget to put it to sleep
[***]) on
curl --silent --user JENKINS_USER:JENKINS_TOKEN JENKINS_JOB_URL/lastBuild/api/json | grep result\":null > /dev/null
until the result of this command is not 0
.
Once the Jenkins job is finished, you would probably want to fetch the job's console in Gitlab
curl -i -X POST --user JENKINS_USER:JENKINS_TOKEN JENKINS_JOB_URL/lastBuild/consoleText
Finally you may curl
once more on JENKINS_JOB_URL/lastBuild/api/json
but this time you grep
it on UNSTABLE
, SUCCESS
or FAILURE
.
Discussion
By following the guidelines above, you can fully orchestrate Jenkins jobs from Gitlab-CI. I've posted a long discussion on why and when should you do this.
I hope this will help you.
[*] Your Gitlab project Settings > CI/CD > Secret vaiables
[** ] Of course I mean by that to craft a script nicely with params, functions, nice variable names, meaningful logs... You name it.
[***] I found a sleep
of 20 seconds worked for me
Solution 2
FWIW, the trigger you referenced originates from Gitlab repository events, not from a GitlabCI execution.
The only possibility (that crosses my mind) of triggering a jenkins job from inside a GitlabCI execution is by having a (custom?) script invoked as part of the GitlabCI execution which remotely activates a Parameterized Trigger Plugin configured for your jenkins job, via a properly-crafted POST request which would include the desired parameters.
pkaramol
Updated on September 18, 2022Comments
-
pkaramol over 1 year
There is supposed to be a way to trigger a Jenkins job via GitlabCi, using the respective plugin.
My question is whether there is a way:
a) to trigger a parameterized Jenkins job
b) to pass parameters when triggering the job
-
Admin about 6 yearsWhat are the parameters you want to pass? What action causes a trigger?
-
Admin about 6 yearsAt some point I want a
gitlab-ci
job to invoke jenkins to perform a deployment on an orchestrator (Rancher
). The parameters will be the name of the environment and the repo-branch from which the checkout of the code will be performed (so that the images to be deployed are built) -
Admin about 6 yearsWhy do you want to chain two CI systems ? I can't really understand it as gitlab-ci can directly trigger deployments on k8s clusters,so adding jenkins in the middle sounds more adding complexity than helping the situation.
-
-
pkaramol about 6 yearsthat's what I thought also...the question is now how to make gitlab-ci aware of the success/failure of the job triggered via
api
; think this is ready ootb only for the trigger that originates from gitlab repo events as you say; -
Dan Cornilescu about 6 yearsHm, that would be fully managing the jenkins job, not just triggering it... Maybe using GitLab Runner locally to run a custom script which triggers the jenkins job and also monitors its status until completion reporting back the result?
-
jimkont almost 4 yearsCan you post a complete example that includes monitoring?
-
avi.elkharrat almost 4 yearsIt's a while loop, and i could not know your functional. I didn't write out a full example on purpose. I think it would be great if you could do your own!
-
mochadwi over 2 yearsbeen searching for the "monitoring" parts, I thought it's simple as waiting for the response :D Thanks for sharing this!