1

I'm seeing in the Source Code Management section of the Jenkins job, that there is a Name.

Can I reference this name in the Execute Script section using an environment variable?

enter image description here

I've read through the documentation for the Git Plugin however, there is no information here on how to reference the given Name.

This would be incredibly useful, as I want to have a different variable for each Git repository, and using the variable would be ideal.

fuzzi
  • 1,967
  • 9
  • 46
  • 90

2 Answers2

1

If your job definition is in a Jenkinsfile whether as a declarative or scripted Pipelines, you can create global or per stage environment vars within the environment block.

The environment vars can be dynamically created as a result of a shell step or groovy script or functions, see below an example of declarative pipeline syntax taken from the documentation here

pipeline {
    agent any 
    environment {
        // Using returnStdout
        CC = """${sh(
                returnStdout: true,
                script: 'echo "clang"'
            )}""" 
        // Using returnStatus
        EXIT_STATUS = """${sh(
                returnStatus: true,
                script: 'exit 1'
            )}"""
    }
    stages {
        stage('Example') {
            environment {
                DEBUG_FLAGS = '-g'
            }
            steps {
                sh 'printenv'
            }
        }
    }
}

In your case, you want to retrieve the value of the Name provided in Source code management, which seems not trivial as I saw in the how to get repo name in Jenkins pipeline thread, and in that case the name is extracted from the git url itself but not from the custom name.

if you are using the declarative Pipeline approach I wonder if is possible and would make sense to define some String vars to just hold the custom names of your repos, and use those vars for both, use them as the params value for the checkout step, more info here and on the other hand assign the values of the environment vars within the environment block

theraulpareja
  • 496
  • 3
  • 9
0

I found that there was no environment variable to access only that name.

When the name is used, the environment branch name from GitHub uses the Name where origin or remote would usually be in the branchname. For example, if the GitHub branch name is mynewfeature, the Jenkins environment would understand that branch name as CustomName/mynewfeature

${GIT_BRANCH} returns CustomName/mynewfeature allowing the Name variable to be accessible in the Execute Script action of Jenkins.

fuzzi
  • 1,967
  • 9
  • 46
  • 90