8

I would like to use EnvInject plugin within my pipeline job. So, I have a possibility to set checkbox "Prepare an environment for the run", but there is no action "Inject environment variables", like in freestyle job. I declared my variables in "Properties Content" block:enter image description here

How to inject environment variables in pipeline job using EnvInject?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Sviatlana
  • 1,728
  • 6
  • 27
  • 55

3 Answers3

8

If you declared following variables in "Properties Content" block:

param1=value1
param2=value2

Then you can get them into pipeline here so:

//do something
def par1 = env.param1
def par2 = env.param2
t0r0X
  • 4,212
  • 1
  • 38
  • 34
Anton Shishkin
  • 1,696
  • 1
  • 8
  • 10
  • 4
    Please edit with more information. Code-only and "try this" answers are discouraged, because they contain no searchable content, and don't explain why someone should "try this". We make an effort here to be a resource for knowledge. – abarisone Jun 30 '16 at 06:13
0

The Environment Injector plugin is of limited use in Jenkins pipelines. I suggest using the built-in 'environment' Jenkins pipeline feature instead. Here a complete declarative Jenkins pipeline script example:

pipeline {
    agent any

    // Inject environment variable for whole pipeline script
    environment {
        param1 = 'value1'
    }

    stages {
        stage('Some stage in my pipeline') {
            // Inject environment variable only for this step
            environment {
                param2 = 'value2'
            }

            steps {
                // Use inherited environment variable
                echo "My 'PATH'   environment variable: '${env.PATH}'"
                // Use injected environment variables
                echo "My 'param1' environment variable: '${env.param1}'"
                echo "My 'param2' environment variable: '${env.param2}'"
                
                // Use injected environment variables in a Groovy script block
                script {
                    def value = env.param1
                    println "par1 value: '${value}'"
                    value = env.param2
                    println "par2 value: '${value}'"
                }
            }
        }
    }
}
t0r0X
  • 4,212
  • 1
  • 38
  • 34
0

Pipeline doesn't support it now, please refer to below ticket, and there are some good and alternative ways:

https://issues.jenkins-ci.org/browse/JENKINS-42614

Moa Chung
  • 1
  • 1