Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins pipeline - Environment variable for docker-compose

I'm trying to use docker-compose with environment variable for the image name set by the Jenkinsfile but it is not working.

I have a Jenkinsfile containing

node() {

  stage('Checkout') {
    cleanWs()
    git credentialsId: 'xxx', url: 'ssh://git@...'
  }

  stage('Build') {
    withMaven(maven: 'maven-3.5.3') {
      sh 'mvn clean package'
      def pom = readMavenPom file:'pom.xml'
      JAR_FILE = pom.artifactId + "-" + pom.version + ".jar"
      JAR_VERSION = pom.version
    }
  }

  stage('Build Docker Image') {
    echo "Docker Build ..."
    docker.withTool('docker') {
      app = docker.build("microservices/my-service:${JAR_VERSION}","--build-arg JAR_FILE=${JAR_FILE} .")
    }
  }

  stage('Running Docker Container') {
    echo "Docker Run ..."
    withEnv(['VERSION=1.0.0']) {
      docker.withTool('docker') {
        sh "docker-compose rm -f -s -v"
        sh "docker-compose up -d"
      }
    }
    cleanWs()
  }
}

And a docker-compose.yml like

version: '3.6'

services:

    my-service:
        image: microservices/my-service:${VERSION}
        container_name: my-service
        ...

Everything is working fine excepts that the last stage is not using my environment variable.

Docker Run ...
[Pipeline] withEnv
[Pipeline] {
[Pipeline] tool

[Pipeline] withEnv
[Pipeline] {
[Pipeline] sh

[my-service] Running shell script
+ docker-compose rm -f -s -v
The VERSION variable is not set. Defaulting to a blank string.
like image 930
tweetysat Avatar asked Jul 25 '18 07:07

tweetysat


People also ask

How do I set environment variables in Docker Compose run?

Set environment variables with ‘docker-compose run’. Just like with docker run -e, you can set environment variables on a one-off container with docker-compose run -e: docker-compose run -e DEBUG=1 web python console.py. You can also pass a variable through from the shell by not giving it a value:

How do I run Docker-based pipelines in Jenkins?

For Jenkins environments which have macOS, Windows, or other agents, which are unable to run the Docker daemon, this default setting may be problematic. Pipeline provides a global option in the Manage Jenkins page, and on the Folder level, for specifying which agents (by Label ) to use for running Docker-based Pipelines.

How to print Jenkins Pipeline environment variables in logs?

The steps to do the same are : Create a new pipeline in Jenkins, named ‘ envvars ’. In the Pipeline Script, type the following groovy script. The windows batch command used here is “ set ”. This command lists all the Jenkins pipeline environment variables in logs. sh ‘printenv’ .

What is the project directory in Docker Compose?

Project directory can be explicitly defined with the --file option or COMPOSE_FILE environment variable. Otherwise, it is the current working directory where the docker compose command is executed ( +1.28 ). For previous versions, it might have trouble resolving .env file with --file or COMPOSE_FILE.


Video Answer


2 Answers

After adding a sh "printenv" juste before the sh "docker-compose ..." I saw

Version=1.0.0

I don't know why withEnv(['VERSION=1.0.0']) give me Version=1.0.0 but using, in the docker-compose-yml image: microservices/my-service:${Version} in place of image: microservices/my-service:${VERSION} works fine.

like image 142
tweetysat Avatar answered Oct 23 '22 01:10

tweetysat


You need to save these ENV in a file (output of jenkinsfile, input of dockercompose file). Then, you have two possibilities:

  1. If you need your ENV inside my-service container save your ENV in ./your_envfile.env, and just add

    version: '3.6'
    services:
        my-service:
            image: microservices/my-service:${VERSION}
            container_name: my-service
            env_file: ./your_envfile.env
  2. If you need to use your ENV in docker-compose file, as I see in your image section with ${VERSION}, you need to save your ENV in .env file.

.env file must be in the same path that you execute docker-compose, or at least a symlink named ./env which points somewhere you've saved ENV in this format:

VERSION=1.0.0 ...
like image 41
Alejandro Galera Avatar answered Oct 22 '22 23:10

Alejandro Galera