Create environment variable from parameters in Jenkins

Viewed 2338

I have a jenkinsfile which is parametrized. Based on the input parameters I want to set certain environment variables. But I m not able to get the syntax right.

parameters {
    choice choices: ['insure-base-docker/insure-base', 'insure-ide/insure-sd', 'insure-ide/insure-ansible','insure-ide/ansible-test-vini'], description: 'Auf welche repository sollte die Tag erstellt?', name: 'repository'
    choice choices: ['tag', 'branch'], description: 'Tag oder branch erstellen', name: 'git_entity'
    string defaultValue: '21.x.x', description: 'Version die als branch oder Tag ersellt werden muss', name: 'version', trim: false
}
environment {
    GIT_URL = "${'https://my_repo/scm/'+param.repository+'.git'}"
    GIT_BRANCH = "${'Release/'+param.version}"
    CHECKOUT_BRANCH = '${${git_entity} == "tag" ? "master" : "develop"}'      
}

the env vars are always wrong. How do I set the env vars correctly?

2 Answers

Nowadays, there aren't many differences between parameters and environment variables in Jenkins. Even the way you use them, preceded by the env. keyword, is the same.

Try something like this.

pipeline {
    parameters {
      choice choices: ['insure-base-docker/insure-base', 'insure-ide/insure-sd', 'insure-ide/insure-ansible','insure-ide/ansible-test-vini'], description: 'Auf welche repository sollte die Tag erstellt?', name: 'GIT_PROJECT'
      string defaultValue: '21.x.x', description: 'Version die als branch oder Tag ersellt werden muss', name: 'GIT_BRANCH', trim: false
    }

    agent any

    stages {
        stage('Cloning Git repository') {
            steps {
                script {
                    git branch: "${env.GIT_BRANCH}", credentialsId: 'MY_GIT_CREDENTIALS_PREVIOUSLY_ADDED_TO_JENKINS', url: "http://github.com/user/${env.GIT_PROJECT}.git"
                }
            }
        }
    }
}

You can use as GIT_BRANCH not just branches, but also tags.

Best regards.

I am assuming you are using a freestyle project here are the steps

  1. Go to Build Environment part and check the option Inject environment variables to the build process it will open a new set of input boxes.
  2. enter your code in Groovy script

Here am just trying to update the Version and Full version to include the passing parameter say TestParam

here is a sample:


import hudson.model.*
import groovy.io.FileType

def build = Thread.currentThread().executable
def buildNumber = build.number
def workspace = build.getEnvVars()["WORKSPACE"]
def defaultBuildNo = build.getEnvVars()["BUILD_NUMBER"]
println "Hi from Groovy script "
println workspace
println defaultBuildNo

def map = [
                 "BUILD_NUMBER": defaultBuildNo,
                 "VERSION" : defaultBuildNo + build.getEnvVars()["TestParam"],
                 "FULL_VERSION": +defaultBuildNo + "." + build.getEnvVars(["TestParam"]
                ]
return map

Now in the execute shell part type these and see all will resolve successfully.

Execute shell

 echo $TestParam
 echo $BUILD_NUMBER
 echo $VERSION
 echo $FULL_VERSION

Now all these env variables are accessible throughout the Job.

Related