Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to build with parameters in Jenkins from Gitlab push?

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: 
like image 875
Mark Allison Avatar asked Sep 19 '25 22:09

Mark Allison


1 Answers

I have been struggling with this for weeks. I had it working once, but I couldn't get it to work again, until today. And the solution was mind-blowingly obvious of course...

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

like image 89
vrijdenker Avatar answered Sep 21 '25 17:09

vrijdenker