HiveBrain v1.2.0
Get Started
← Back to all entries
patternModerate

Trigger Jenkins job from Gitlab CI with parameters

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
triggerjenkinsgitlabwithfromparametersjob

Problem

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

Solution

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

Code Snippets

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}
curl --silent --user JENKINS_USER:JENKINS_TOKEN JENKINS_JOB_URL/lastBuild/api/json | grep result\":null > /dev/null
curl -i -X POST --user JENKINS_USER:JENKINS_TOKEN JENKINS_JOB_URL/lastBuild/consoleText

Context

StackExchange DevOps Q#3442, answer score: 11

Revisions (0)

No revisions yet.