12

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?

1
  • Did you ever figure this out?
    – Pytry
    Commented Apr 22, 2022 at 0:37

2 Answers 2

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
  • 6
    You don't get a question. I need variables passed in a time of generating the job via Job DSL, so when I start that job it will be already there.
    – mate200
    Commented Nov 13, 2019 at 20:01
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 **** **

1
  • 4
    You don't get a question. I need variables passed in a time of generating the job via Job DSL, so when I start that job it will be already there.
    – mate200
    Commented Nov 13, 2019 at 20:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.