Dennis
Dennis

Reputation: 2964

How to use an environment variable in a environment variable in a Jenkinsfile?

I would like to have a environment variable COMPOSE_PROJECT_NAME with the value denpal-4 for a Jenkins job with build_id 4.

environment {
  COMPOSE_PROJECT_NAME = 'denpal-$(BUILD_ID)'
}

The alternative would be writing this in every sh-block

sh ''' 
export COMPOSE_PROJECT_NAME = 'denpal-$(BUILD_ID)'
code...
'''

This would go against the DRY (Don't repeat yourself) principle however. Is something in environment {} possible?

UPDATE/ANSWER: double quotes instead of single quotes fixed it.

Upvotes: 0

Views: 803

Answers (1)

user_9090
user_9090

Reputation: 1974

Yes, you can assign the value to variable in environment stage (using env as a prefix to the variable) and then can use it in any stage, please see below:-

environment {
           env.COMPOSE_PROJECT_NAME = "denpal-${BUILD_ID}"
        }

Upvotes: 2

Related Questions