Search code examples
jenkinsjenkins-pipeline

Jenkins pipeline get primary job name of multibranch pipeline


I have a jenkins pipeline script and try to get the name of the base of my multibranch pipeline.

The URL to the job looks like

https://host:port/job/<WHAT-I-NEED>/job/<MULTI-BRANCH-NAME>/

I tried the following elements but none of them gives me '<WHAT-I-NEED>'

stage('Check environment') {
      steps {
        echo "Workspace: ${env.WORKSPACE}"
        echo "Job name: ${env.JOB_NAME}"
        echo "Job base name: ${env.JOB_BASE_NAME}"
        echo "Branch name: ${env.BRANCH_NAME}"
        echo "Build display name: ${env.BUILD_DISPLAY_NAME}"
        echo "Current build: ${currentBuild}"
        echo "Full Project display name: ${currentBuild.fullProjectName}"
        echo "Project display name: ${currentBuild.projectName}"
        echo "Change branch: ${env.CHANGE_BRANCH}"
        echo "Change target: ${env.CHANGE_TARGET}"
      }

This would be the shortended output of the pipeline

Workspace: null
Job name: <WHAT-I-NEED>/<MULTI-BRANCH-NAME>
Job base name: <MULTI-BRANCH-NAME>
Branch name: <MULTI-BRANCH-NAME>
Build display name: #<NUMBER>
Current build: org.jenkinsci.plugins.workflow.support.steps.build.RunWrapper@30e963bc
Full Project display name: <WHAT-I-NEED>/<MULTI-BRANCH-NAME>
Project display name: <MULTI-BRANCH-NAME>
Change branch: null
Change target: null

So any change to get the '<WHAT-I-NEED>'-part?


Solution

  • found a solution to my problem.

    pipeline {
    
        ...
        environment {
            MAIN_PROJECT = ''
        }
      
        ...
    
        stages {
            stage('Prepare environment') {
                script {
                    MAIN_PROJECT = "${env.JOB_NAME}".tokenize("/")[0]
                }
            }
    
            ...
        }
    }