When I run a docker container with docker run
docker run -d --memory=100m --cpus=0.5 -e SECRENT_ENV=$SECRET_ENV -e name=name --network=nginx-proxy image-name
via blueocean in jenkins and set an environment variable inside blue ocean
SECRET_ENV=SECRET_DATA
the environment variable is committed to the repo inside the jenkinsfile in plain text.
How can I pass an environment variable from my host machine (that jenkins is running on) to the docker run command.
extra info:
– running on centos 7
– trying to run a nodejs app
– Jenkins is itself running inside a docker container
– tried to set the environment variable on host machine via ./bashrc export SECRET_ENV=secret_data
2
Answers
First Thing, You can consume host Environment variables by passing it through the Docker run command.
export SECRENT_ENV=some-secret
export the ENV on the Host machine or you can consume from ~/.bashrc as you do seems fine.Here The problem is you are running Jenkins inside docker, so the rest of the container will only able to consume the Environment variable that is defined inside Jenkins Container. To make all environment variables to Jenkins you need to update Jenkins run command.
This will return Host user name that is
centos
so one should avoid this as all the environment variable of Jenkins container is override by host env.So you need to pass only desired ENV to Jenkins or you can use env file to Jenkins.
Now update ENV in the Jenkins file.
jenkins-pipeline-environment-variables
You can a create a environment variable in Jenkins configuration page (http://localhost:8080/jenkins/configure) and then search for Environment variables.
And add the required variable and use in your Jenkins pipeline, please see below:-