jenkinsci / jenkinsci/docker-workflow-plugin
[JENKINS-48082] Environment variables missing when both defined in pipeline and jenkins master process environment with docker agent
- Dominant language
- Java
- Stars
- 527
- Forks
- 422
- Avg merge
- 10m
- Merged PRs (30d)
- 1
Description
When defining environment variables both in a declarative pipeline running a docker agent, and in the jenkins master process environment itself, then the environment variable is missing in the docker agent.
Scenario:
Jenkins setup:
Using jenkins:lts docker image, with docker socket forwarded in the jenkins docker container for it to use docker agents; with extra environment variables:
$ docker run -e FOO=bar -e FOO2=bar -e FOO3=bar -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker --init -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 --name jenkins jenkins/jenkins:lts
# hack to add missing depencies on the docker cli, and fix permissions issues
$ docker exec -u 0 jenkins bash -c "apt-get update && apt-get install libltdl7 && chown -R jenkins:jenkins /var/run/docker.sock"
Setup jenkins with usual plugins (no manual selection).
Pipeline job:
Create a pipeline with the following declarative pipeline:
pipeline {
agent { docker 'ubuntu:16.04' }
environment {
FOO = 'bar'
FOO2 = 'different'
}
stages {
stage('Test') {
steps {
sh 'echo FOO: ${FOO:-error}'
sh 'echo FOO2: ${FOO2:-error}'
sh 'echo FOO3: ${FOO3:-error}'
}
}
}
}
Run the job
```
[env-bug] Running shell script
+ echo FOO: error
FOO: error
[Pipeline] sh
[env-bug] Running shell script
+ echo FOO2: different
FOO2: different
[Pipeline] sh
[env-bug] Running shell script
+ echo FOO3: error
FOO3: error
```
Analysis
Scenario explanation:
- start jenkins with env vars FOO=bar and FOO2=bar and FOO3=bar
- pipeline redeclares FOO=bar (same value) and FOO3=different (different value)
- result: FOO disappears in the shell environment in docker agent, FOO2 is OK, FOO3 is not there (not forwarded from jenkins environment, it's an extra test).
---
Originally reported by thomas_deepomatic, imported from: Environment variables missing when both defined in pipeline and jenkins master process environment with docker agent
Raw content of original issue
When defining environment variables both in a declarative pipeline running a docker agent, and in the jenkins master process environment itself, then the environment variable is missing in the docker agent.
Scenario:
Jenkins setup:
Using jenkins:lts docker image, with docker socket forwarded in the jenkins docker container for it to use docker agents; with extra environment variables:
$ docker run -e FOO=bar -e FOO2=bar -e FOO3=bar -v /var/run/docker.sock:/var/run/docker.sock -v $(which docker):/usr/bin/docker --init -v jenkins_home:/var/jenkins_home -p 8080:8080 -p 50000:50000 --name jenkins jenkins/jenkins:lts
# hack to add missing depencies on the docker cli, and fix permissions issues
$ docker exec -u 0 jenkins bash -c "apt-get update && apt-get install libltdl7 && chown -R jenkins:jenkins /var/run/docker.sock"Setup jenkins with usual plugins (no manual selection).
Pipeline job:
Create a pipeline with the following declarative pipeline:
pipeline {
agent { docker 'ubuntu:16.04' }environment {
FOO = 'bar'
FOO2 = 'different'
}
stages {
stage('Test') {
steps {
sh 'echo FOO: ${FOO:-error}'
sh 'echo FOO2: ${FOO2:-error}'
sh 'echo FOO3: ${FOO3:-error}'
}
}
}
}Run the job
[env-bug] Running shell script
+ echo FOO: error
FOO: error
[Pipeline] sh
[env-bug] Running shell script
+ echo FOO2: different
FOO2: different
[Pipeline] sh
[env-bug] Running shell script
+ echo FOO3: error
FOO3: errorAnalysis
Scenario explanation:
- start jenkins with env vars FOO=bar and FOO2=bar and FOO3=bar
- pipeline redeclares FOO=bar (same value) and FOO3=different (different value)
- result: FOO disappears in the shell environment in docker agent, FOO2 is OK, FOO3 is not there (not forwarded from jenkins environment, it's an extra test).
environment
```
jenkins 2.73.3, from LTS docker image
host: ubuntu 16.04 with docker-ce 17.09
```
Contributor guide
Assessment
This issue has not been assessed yet.