13

I want to pass some environment variables in the time of generating the multibranch pipelines, so upon the build start, it will already have these variables.

The only thing I found was this envInject plugin which is not supported by multibranch DSL generation.

https://jenkinsci.github.io/job-dsl-plugin/#plugin/envinject

Does anyone know the way to do that?

mate200
  • 131
  • 1
  • 4

2 Answers2

1

You can use Jenkins DSL and pass the environment variable as mentioned in below links:

https://jenkins.io/doc/book/pipeline/syntax/#environment https://jenkins.io/doc/pipeline/steps/workflow-basic-steps/#withenv-set-environment-variables

1

This might help you with your problem. In the normal pipeline or any Jenkins job, we can define parameters which can be accessed via say ${env.SOME_VARIABLE}

My solution for using environment variables in the Jenkins Multibranch Pipeline. Scenario. Say you have a variable named VARIABLE whose value is 123456789.

Create a secret text with id and description.

  • Secret Text: 123456789

NOTE: In Jenkins, the secret text will be completely hidden.

  • Id: VARIABLE

NOTE: You can define it as you want. If left empty a random value will be assigned to it in the form as45f2sf-43rs-4sdf-s3f3-329f9bc9ae269

  • Description: VARIABLE

NOTE: Description for the variable.

Once this is created in your multibranch pipeline Jenkinsfile, you can add the following to access those variables whenever required.

pipeline {
  agent any
    environment { 
      SECRET_VARIABLE = credentials("VARIABLE") 
    }
    stages {
      stage('stage') {
        steps {
          echo "This is a secret variable: $SECRET_VARIABLE"
        }
      }
   }
}         

**PS: The variables will appear in a format **** **

Manish_
  • 108
  • 1
  • 1
  • 7