4

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
  • You have a typo in your environment directive: the object is params and not param. Commented Mar 30, 2021 at 16:41
  • this i corrected in my real file. Commented Mar 31, 2021 at 18:47

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Do you see in the env section in the code, i have env variables as a string in conjunction to the params
and also the above doesnt work with variables. I have an echo which prints the value, its right from my env, but the usage of git with vars isnt working
error: > git config --get remote.${GIT_URL_STEP}.url # timeout=10 ERROR: Error fetching remote repo 'origin' hudson.plugins.git.GitException: Failed to fetch from ${GIT_URL_STEP}
the double quotes was important
2

I had to map the param to env in the stage:

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
    }

    stages {
        stage('Cloning Git repository') {
            environment {
                GIT_PROJECT = "${params.GIT_PROJECT}"
                GIT_BRANCH = "${params.GIT_BRANCH}"
            }
        }
    }
}

Now I can access the parameters as environment variables in other scripts that can only use environment variables.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.