6

I'm trying to use a docker machine in Jenkins, and have that machine have an environment variable (FOO) set. I've successfully setup an image using a docker template in my jenkins configuration. Meaning, that's probably not my problem spot. The problem is that I cannot seem to get the environment variable to be available. If I create a jenkins project that just tries to do "echo $FOO", all I get is a blank line.

My Dockerfile is based on ubuntu-14.04, and ends with: CMD ["/usr/sbin/sshd", "-D"]

Things that I've tried:

  • In the jenkins configuration page, in the "docker template" section, I've added "FOO=1" to the "environment" field. However, when I click "save", this change is not saved.

  • Adding to the Dockerfile various ways to set FOO:

    • RUN echo "export FOO=1" >> /etc/profile

    • ENV FOO 1

    • RUN echo export FOO=1 >> ~/.bashrc

  • EnvInject Plugin. This works, but requires you to check the checkbox in every single jenkins plan. I'm hoping for a solution where I only have to do something once.

References:

  • Jenkins bug. You cannot set FOO in the Jenkins environment and expect it to be passed to the docker container.

  • Docker's advice. This page suggests (I think) adding the env variable to /etc/profile. As I mentioned, this failed for me.

  • Passing env through ssh. This explains how you can modify an ssh call to pass env vars, but I don't know how to access that part of the command within jenkins. (Note, it's fine to expose the variable on the command line.)

  • ssh set user env. This answer seemed a bit overkill, and I didn't test to see if it worked. Presumably I would have the Dockerfile add .ssh/environment and I would have to enable PermitUserEnvironment.

Community
  • 1
  • 1
user3550496
  • 333
  • 5
  • 14

1 Answers1

1

I have Jenkins running in a docker container and I use it to run other containers. Jenkins connects to the containers to build and Android app via ssh, but the environment varaibles always got cleared by ssh. I solved the problem by writing the environment vars to /etc/environment, and then they are automatically loaded on login. This is the last steps of my dockerfile:

RUN env | grep _ >> /etc/environment

EXPOSE 22

USER jenkins

CMD ["/usr/sbin/sshd", "-D"]
membersheep
  • 504
  • 4
  • 12
  • 1
    I dont think this answer the question. Issue is when Jenkins logs in it wipes the environment variables out. – Hans Feb 12 '18 at 17:03