mealesbia
mealesbia

Reputation: 935

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 = 'https://my-repo/repo.git'
    }

    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?

Upvotes: 1

Views: 3060

Answers (1)

Joerg S
Joerg S

Reputation: 5129

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 = 'https://my-repo/repo.git'

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'
            }
        }
    }
}

Upvotes: 1

Related Questions