Search code examples
jenkins

How to load env variable file into Jenkins Pipeline


I feel like there should be a simple way to do this, but I can't find it.

I have a file named .env in my git repo that includes a list of variables I need in my Jenkins Agent. The variables are public things like version numbers (not passwords). How do I load a file of variables into my Jenkins pipeline?

I am using Jenkins 2.462.1

In a linux environment without Jenkins, I could do the following to load and view my variables:

source .env
printenv

I tried adding the following to a stage in my Jenkinsfile. The variables print to the console output after the source command, but are not in the list returned by printenv.

sh "source ${WORKSPACE}/.env" 
sh "printenv"

Using sh ". ${WORKSPACE}/.env" returns the same result as sh "source ${WORKSPACE}/.env".


Solution

  • I really hate this answer and hope someone provides a better one, but it is the only thing I've found that makes the variables available in all stages of the pipeline.

    Manually copy contents of variable file into environment block at top of pipeline.

    pipeline {
      environment {
        version='1.0'
        customer='Bob'
      }
      agent { any }
      stages {
    
         rest of pipeline
    
      }
    }