4

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

  • What are the parameters you want to pass? What action causes a trigger? – casey vega Feb 28 '18 at 23:33
  • At 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) – pkaramol Mar 1 '18 at 9:00
  • Why 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. – Tensibai Mar 1 '18 at 15:15
3

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.

See also https://stackoverflow.com/questions/20359810/how-to-trigger-jenkins-builds-remotely-and-to-pass-parameters.

  • that'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; – pkaramol Mar 1 '18 at 10:04
  • Hm, 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? – Dan Cornilescu Mar 1 '18 at 13:48
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

0

If you want to keep it simple, try the generic webhook trigger plugin.

https://plugins.jenkins.io/generic-webhook-trigger

You can trigger a build by sending an http POST using a JSON body or URL parameters.

generic-webhook-trigger

Parse the JSON request

def req = readJSON text: payload

Now you can use it in your pipeline assuming you had a deploy function.

deploy(req.environment, req.branch)

I must agree with @Tensibai. Two CI/CD systems on the surface does seem overly complex. You might want to consider sticking with one if possible.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.