3
votes

I have GitLab Community Edition 8.15.2 successfully trigger pipeline projects in Jenkins 2.32.1 using a webhook. I want the gitlab push to trigger a build with parameters but the parameter value is null when it comes through so the build fails.

The gitlab webhook looks like:

http://jenkins.server:8080/project/project-a/buildWithParameters?MYPARAM=foo

In my pipeline project I echo the parameter value out with

echo "MYPARAM: ${MYPARAM}"

and it's not set to anything. Any ideas on where I've gone wrong?

UPDATE

The actual code I'm using in the pipeline is:

node {
    try {
        echo "VM_HOST: ${VM_HOST}"
        echo "VM_NAME: ${VM_NAME}"
        stage('checkout') {

            deleteDir()
            git 'http://git-server/project/automated-build.git'
        }
        stage('build') {

            bat 'powershell -nologo -file Remove-MyVM.ps1 -VMHostName %VM_HOST% -VMName "%VM_NAME%" -Verbose'
        }
        ...
    }
}     

The parameter VM_HOST has a default value but VM_NAME doesn't. In my Console output in Jenkins I can see:

[Pipeline] echo
VM_HOST: HyperVHost
[Pipeline] echo
VM_NAME: 
3
Did you try: echo "MYPARAM: ${MYPARAM}"gile
Sorry yes that's exactly what I did, edited my question.Mark Allison
If you build this job manually from Jenkins, does printing MYPARAM work?Piotr Dawidiuk
@PiotrDawidiuk Yes it works when invoking manually.Mark Allison
@MarkAllison Ok, how about setting a default value for a parameter? Building with webhook still return nothing? And what kind of type is the parameter? String?Piotr Dawidiuk

3 Answers

4
votes

I have been struggling with this for weeks. I had it working once, but I couldn't get it to work again, untill today. And the solution was mindblowingly obvious ofcourse...

Automatically for each pipeline job I ticked the following box:

Build when a change is pushed to GitLab. GitLab CI Service URL: http://jenkins.dev:8080/project/MyProject

Then from GitLab I used the webhook to trigger the above. Like you I tried to add /buildWithParameters and tried many other things that didn't work.

The problem was, I ticked the wrong checkbox! Since I trigger the build from a GitLab webhook, the above checkbox (build when a...) does not have to be checked at all. What needs to be checked is:

Trigger builds remotely (e.g., from scripts)

That checkbox provides you with a new URL:

Use the following URL to trigger build remotely: JENKINS_URL/job/MyProject/build?token=TOKEN_NAME or /buildWithParameters?token=TOKEN_NAME

Like all the documentation I came along states and as you can see, the URL now no longer starts with /project, but with /job instead!

So tick that box and change your URL accordingly:

http://jenkins.server:8080/**job**/project-a/buildWithParameters?token=TOKEN_NAME&MYPARAM=foo

Least I want to mention the token: In the GitLab webhook there is a seperate field for "token", which states:

Use this token to validate received payloads. It will be sent with the request in the X-Gitlab-Token HTTP header.

So, the token provided there will be sent along the request as a HTTP header. This is the token which can be provided globally in the Jenkins setup. The token you must provide in the Jenkins job when ticking the box Use the following URL to trigger build remotely must be send in the URL as GET parameter, just like the example shows.

Final note: personally I have never got this working completely, because I don't get the Jenkins CSRF protection off my back. Disabling it gives me another error. However, hopefully the above does fix the problem for you and others.

2
votes

GitLab plugin does not allow you to pass arbitrary parameters. In their project there is an open issue for it that deserves to be upvoted.

My convoluted solution was to use the desired values for the push trigger as the default parameters of the job. Then I used the Parameterized Scheduler plugin to use other values in the scheduled executions.

The problem is that I got a bad usability for the job when it was manually run, since the default parameters were appropriate for the push hook.

1
votes

I found the solution here https://www.jittagornp.me/blog/jenkins-gitlab-webhook/

I verified it with Jenkins 2.263.1 and GitLab Community Edition 13.6.1

Your webhook url will look like https://hunter:[email protected]/job/yourjenkinsproject/buildWithParameters?token=Aju9ryHUu6t7W8wLSeCWtY2bWjzQduYNPyY7B3gs&yourparam=yourvalue

"hunter" ist your username in Jenkins. The following is the Jenkins API Token you have to create in your Jenkins User Managment independent of the project.

The last Token is the one you specify in the jenkins project options under "Trigger builds remotely (e.g., from scripts)"

The last thing is to add your Parameter and value to the url with &param=value