How to pass environment variables from WebUI into secondary image with multi-image docker executor

Hello,

Is it possible to pass environment variables set in the WebUI of CircleCI into the service image ?

Here is an example:

  • MY_ENV is defined in the config.yaml

  • MY_CI_ENV is defined in the WebUI

  • My_CI_ENV2 is the previous envar reset into the config.yaml

    version: 2                                                                  
    jobs:                                                                       
      test:                                                                     
        docker:                                                                 
          - image: alpine:latest                                                
          - image: mongo:latest                                                 
            command: sh -c "env; echo MY_ENV=${MY_ENV};echo MY_CI_ENV=${MY_CI_ENV}"
            environment:                                                        
              - MY_ENV: bonjour
    
      environment:                                                            
        - MY_ENV: hello                                                       
        - MY_CI_ENV2: ${MY_CI_ENV}                                            
      steps:                                                                  
        - run:                                                                
            name: echo1                                                       
            command: echo ${CIRCLE_BRANCH}                                    
        - run:                                                                
            name: echo2                                                       
            command: echo ${MY_ENV}                                           
        - run:                                                                
            name: echo3                                                       
            command: echo ${MY_CI_ENV}                                        
        - run:                                                                
            name: echo4                                                       
            command: echo ${MY_CI_ENV2} 
    
    workflows:                                                                  
    version: 2                                                                
      just_do_it_now:                                                           
        jobs:                                                                   
          - test
    

The output of the service image (mongo):

HOSTNAME=d42f6c3814ea
SHLVL=0
HOME=/root
NO_PROXY=127.0.0.1,localhost,circleci-internal-outer-build-agent
MY_ENV=bonjour
GPG_KEYS=9DA31620334BD75D9DCB49F368818C72E52529D4
TERM=xterm
MONGO_PACKAGE=mongodb-org
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
MONGO_MAJOR=4.0
JSYAML_VERSION=3.10.0
MONGO_REPO=repo.mongodb.org
GOSU_VERSION=1.10
PWD=/
MONGO_VERSION=4.0.3
MY_ENV=bonjour
MY_CI_ENV=

And here is the output of the “echo4”:

${MY_CI_ENV}

Yep, try this:

@halfer
This is what I tried and didn’t work, as posted before:

  environment:                                                            
    - MY_ENV: hello                                                       
    - MY_CI_ENV2: ${MY_CI_ENV}  
[...]
 steps: 
[...]
    - run:                                                                
        name: echo4                                                       
        command: echo ${MY_CI_ENV2} 

And output was:

${MY_CI_ENV}

Hello, this here will not work, the value in the config is read as a string directly. No interpolation exists for the config file. But this should be correct:

      - image: mongo:latest                                                 
        command: sh -c "env; echo MY_ENV=${MY_ENV};echo MY_CI_ENV=${MY_CI_ENV}"
        environment:                                                        
          - MY_ENV: bonjour

Here, in the second image MY_ENV should be bonjour, as this is a static string everything should work fine. If you try to echo the value of MY_ENV though you will not see this because the echo command is executed in the first container only.

I do think there may be a need for a secure way to insert information into the secondary container and I am not sure I can easily provide a solution for that at the moment.

I would highly recommend creating a post on our Ideas Portal for a feature suggestion. This sounds like a fairly valuable feature we should consider.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.