Alexander Kondaurov
Alexander Kondaurov

Reputation: 4133

shell env variables are not passed in docker-compose using jenkins

To reproduce my issue:

  1. Create freestyle project in jenkins with one task 'execute shell' with this code:
export PROJECT_NAME=hello
export IMAGE_NAME=hello-world
docker-compose -p ${PROJECT_NAME} up
  1. Place docker-compose.yml in job's workspace:
version: '2'
services:
  app:
    image: ${IMAGE_NAME}
  1. Run this job.

I get error:

Started by user anonymous
[EnvInject] - Loading node environment variables.
Building in workspace /var/lib/jenkins/jobs/testenv/workspace
[workspace] $ /bin/sh -xe /tmp/hudson1988957642163027988.sh
+ PROJECT_NAME=hello
+ IMAGE_NAME=hello-world
+ docker-compose -p hello up
The IMAGE_NAME variable is not set. Defaulting to a blank string.

I created script with same code on my mac machine and it works!

Alexanders-Mini:deploy alexanderkondaurov$ ./test.sh 
Creating hello_app_1
Attaching to hello_app_1
app_1  | 
app_1  | Hello from Docker.
app_1  | This message shows that your installation appears to be working correctly.

I don't understand why it doesn't work in jenkins. As you see docker-compose doesn't see shell env variables :(

Upvotes: 1

Views: 1374

Answers (1)

M. Mourad
M. Mourad

Reputation: 73

look at the response of Rajiv. docker-compose, export environnement variables are not working in Jenkins

Copy of the Rajiv response :

Your environment variables inside jenkins shell will not be imported automatically. Add environment variables through .env file under your Jenkins job's workspace.

$ echo PROJECT_NAME=hello >> .env 
$ echo IMAGE_NAME=hello-world >> .env
$ cat .env
export PROJECT_NAME=hello
export IMAGE_NAME=hello-world

Then run

$ docker-compose up

Upvotes: 2

Related Questions