Use Environment Variable in Parameter Section of Jenkins Pipeline

I have an environment variable which represents a gitrepo and I use this repo in my git parameter:

pipeline {
    agent any

    options {
        buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '1'))
        disableConcurrentBuilds()
    }

    environment {
        GitRepo = '
    }

    parameters {
        gitParameter branchFilter: 'origin/(.*support.*)', defaultValue: 'develop', name: 'SUPPORTBRANCH', type: 'PT_BRANCH', useRepository: env.GitRepo , sortMode: 'DESCENDING_SMART'
    }
...}

When I use env.GitRepo inside the stages of my pipeline it works, but not when I use it in a parameter section.

How can I make this work?

1 Answer

Does it really need to be an environment variable? If not a simple property could do the trick. Of course you can still use it to define the environment variable if required...

I used a string parameter just because for me it seemed too difficult to get a simple example running using the gitParameter plugin. But of course you can use the property also for the git parameter plugin.

// simply define the property here...
gitRepo = '

pipeline {
    agent any

    environment {
        // optional, not used below. Use only if you need to have an environment variable
        GitRepo = gitRepo
    }

    options {
        buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '1'))
        disableConcurrentBuilds()
    }

    parameters {
        string defaultValue: gitRepo, description: '', name: 'Test', trim: false
    }

    stages {
        stage ('foo') {
            steps {
                echo 'Hi'
            }
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest