Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create environment variable from parameters in Jenkins

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?

like image 455
Vini Avatar asked Sep 04 '25 03:09

Vini


1 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.

like image 137
Stefano Martins Avatar answered Sep 06 '25 17:09

Stefano Martins