Run a Gitlab-ci job on runner from Jenkins pipeline stage

107 views Asked by At

I have my jenkins pipeline with several stages, I have one stage that should connect to a distant gitlab to run a gitlab ci job on runner (own by another BU).

I tried with "curl" command like this:

pipeline {
    agent any

    stages {
        stage('Trigger GitLab Runner Job') {
            steps {
                script {
                    // Trigger the GitLab job using the GitLab Runner API
                    def triggerResponse = sh(
                        script: "curl --insecure --request POST --header 'PRIVATE-TOKEN: oauySh5_YRAuWDaPD_Le' 'https://my-gitlab.fr/myjob/test-ci/-/jobs/play'",
                        returnStatus: true
                    )
                    
                    if (triggerResponse == 0) {
                        echo 'GitLab job triggered successfully.'
                    } else {
                        error 'Failed to trigger GitLab job.'
                    }
                                                                                   
                }
            }
        }
    }
}

But I can make it work, I have systematically this error:

 </h1>
10:01:53    <div class="container">
10:01:53      <h3>The change you requested was rejected.</h3>
10:01:53      <hr />
10:01:53      <p>Make sure you have access to the thing you tried to change.</p>
10:01:53      <p>Please contact your GitLab administrator if you think this is a mistake.</p>
10:01:53      <a href="javascript:history.back()" class="js-go-back go-back">Go back</a>
10:01:53    </div>
10:01:53    <script>
10:01:53      (function () {
10:01:53        var goBack = document.querySelector('.js-go-back');
10:01:53  
10:01:53        if (history.length > 1) {
10:01:53          goBack.style.display = 'inline';
10:01:53        }
10:01:53      })();
10:01:53  
10:01:53    </script>
10:01:53  </body>
10:01:53  </html>
10:01:53  [Pipeline] echo
10:01:53  GitLab job triggered successfully.

The problem with curl is also that I can have a correct back if the job is succeffully executed. I am looking for another method to trigger my gitlab ci job from jenkins pipeline. Thank you in advance.

NB: my job is a sample echo to test:

my-test-job:
  script:
    - echo "This is a test"

1

There are 1 answers

2
F.Rosado On

Trigger a gitlab CI must be done using pipeline triggers: https://gitlab.internal.ericsson.com/help/ci/triggers/index

On settings > CICD > Pipeline Triggers

Create an entry it will generate a token, later you must launch the job using:

curl --insecure --fail --request POST \
     -F token=TOKEN \
     -F "ref=REF_NAME" \
     -F "variables[RUN_NIGHTLY_BUILD]=true" \
     https://my-gitlab.fr/api/v4/projects/12345/trigger/pipeline