Home > database >  Passing credentials though Jenkinsfile -> Docker-compose -> Docker
Passing credentials though Jenkinsfile -> Docker-compose -> Docker

Time:11-06

I need to shove credentials to a docker container via environment variables. This container is launched from a docker-compose file which is being maintained by Jenkins with CI/CD. I have the credentials stored in Jenkins as separate secret text entries under the domain of global. To the best of my knowledge, I can shim those credentials through via the environment block in my build stage using the credentials function. My attempt for the Jenkinsfile is shown below:

#!groovy
pipeline {
    agent any

    stages {
        stage("build") {
            environment {
                DISCORD_TOKEN = credentials('DICEBOT_DISCORD_TOKEN')
                APPLICATION_ID = credentials('DICEBOT_APPLICATION_ID')
            }

            steps {
                sh 'docker-compose up --build --detach'
            }
        }
    }
}

Not much is printed for the error. All I get is this simple error: ERROR: DICEBOT_APPLICATION_ID. That is it. So is the scope of where I stored the secret text incorrect? I was reading that since the domain is global, anything should be able to access it. So maybe my idea for domain scoping is wrong? Is the location of environment in the Jenkinsfile wrong? I am not sure. The error is very bland and does not really describe what it doesn't like about DICEBOT_APPLICATION_ID.

To make matters worse, fixing this issue doens't really even solve the main issue at hand: getting the docker container to hold these credentials. The issue that I am currently dealing with is just to scope the environment variables to running docker-compose and probably will not shim the environment variables into the container I need them in.

For the second part, getting docker-compose to pass on the credentials to the container, I think the snippet below might do the trick?

version: "3"
services:
  dicebot:
    environment:
      DISCORD_TOKEN: ${DISCORD_TOKEN}
      APPLICATION_ID: ${APPLICATION_ID}
    build:
      context: .
      dockerfile: Dockerfile

CodePudding user response:

Solution

The environment block is in the right location if you only intend on using those variables within that stage. Your docker-compose.yml file looks fine but you aren't passing the environment variables as build arguments to the docker-compose command. Please see my modifications below

 steps {
      sh "docker-compose build --build-arg DISCORD_TOKEN='$DISCORD_TOKEN' --build-arg APPLICATION_ID='$APPLICATION_ID' --detach --verbose"
      sh "docker-compose up -d"
 }

I'm assuming the docker-compose.yml is in the same repository as your Jenkinsfile. Be cognizant of the scope of your credentials.

  • Related