I need to take env vars for different apis from a repo that contains all the .env files(one per api). For example:
Inside /env_file directory:
- foo.env
- bar.env
Is it possible to use a .env file located in a repo to get the env vars instead of having them set manually in environment {}? In case it is, how could be referenced each file in each api stage? Thanks in advance! Never worked in Jenkinsfile like this before. Sorry if this is a weird question.
CodePudding user response:
You can inject environment into Jenkins pipeline's global variable: env
which is a Map and the wirepuller of environment {}
, but it has a limitation:
Allow insert new environment, NOT allow override existing environment
If your those .env files has same environment and you need to load them together, you will run into 'override existing environment' issue.
If you're not in that case, you can do as following:
pipeline {
stages {
stage('Test endpoint A') {
script {
def props = readProperties file: 'A.env'
for (p in props) {
env[p.key] = p.value
}
}
// other steps
}
stage('Test endpoint B') {
script {
def props = readProperties file: 'B.env'
for (p in props) {
env[p.key] = p.value
}
}
// other steps
}
}
}