Passing interpolated environment variables in Jenkinsfile

I’m trying to pass some values as env variables for my rails app’s database.yml to pick up.

I start a mysql docker container and pass the container id into a ruby container, prefixes with an underscore (because env vars can’t start with a number. Docker then creates some env vars, one which I’m interested in is _{container.id}_PORT_3306_TCP_ADDR. I don’t know what the container id will be so I can’t, say, use DATABASE_HOST.

  • Bundler can't see gems installed in dockerized environment
  • ERROR: In file './docker-compose.yml', service 'volumes' must be a mapping not an array
  • How can I provision a Docker image using Ansible without installing locally?
  • How to create a graph database in TinkerPop/Rextser using the Rexster Docker image and Bulbs?
  • nginx and php-fpm in Docker
  • Docker Image HDP 2.5 - Hbase not connecting to Zookeeper (ERROR: Can't get master address from ZooKeeper; znode data == null)
  • And so I’m trying to assign the value of _{container.id}_PORT_3306_TCP_ADDR (which would be something like 172.17.0.4) to DATABASE_HOST in the shell it’s not being evaluated.

    node('docker') {
      docker.image('mysql:5.5.53').withRun("-e MYSQL_ROOT_PASSWORD=test") { container ->
        docker.image('ruby:2.3').inside("--link=${container.id}:_${container.id}") {
          ansiColor('xterm') {
    
            stage('Checkout') {
              checkout scm
            }
    
            stage('env') {
              sh 'env'
            }
    
            stage('Init Apps') {
                withEnv([
                    'RAILS_ENV=test',
          "DATABASE_HOST=\$_${container.id.toUpperCase()}_PORT_3306_TCP_ADDR"
                ]) {
                  sh 'echo \$RAILS_ENV'
                  sh 'echo \$DATABASE_HOST'
                  sh 'erb core/lib/generators/qcommerce/install/templates/admin/config/database.yml'
                }
              }
          }
        }
      }
    }
    

    The _9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_PORT_3306_TCP_ADDR env var exists in env…

     > git checkout -f 3ce90f1e8d818cb83018e4ba6405828d6c73c3cd
    [Bitbucket] Notifying commit build result
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (env)
    [Pipeline] sh
    [eature_fix_dummy_app_for_ci-NDHAE5AF75X2XHVT6W5Z6ACBZOE3W47XMHPKVWQMEFQ6MUTOYVOQ] Running shell script
    + env
    JENKINS_HOME=/var/lib/jenkins
    RUBY_MAJOR=2.3
    BUNDLER_VERSION=1.13.6
    HOSTNAME=5845a9cd3793
    NODE_LABELS=docker docker (i-0597f8ce3efefb442)
    RUBYGEMS_VERSION=2.6.8
    HOME=/
    BUILD_URL=http://jenkins.abc123.com/job/Q/job/feature%252Ffix_dummy_app_for_ci/264/
    JENKINS_SERVER_COOKIE=durable-8d8bf72ed3f9c3149ad72e21dbc28ca9
    BUNDLE_APP_CONFIG=/usr/local/bundle
    WORKSPACE=/home/jenkins/workspace/eature_fix_dummy_app_for_ci-NDHAE5AF75X2XHVT6W5Z6ACBZOE3W47XMHPKVWQMEFQ6MUTOYVOQ
    _9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_ENV_MYSQL_MAJOR=5.5
    NODE_NAME=docker (i-0597f8ce3efefb442)
    BUNDLE_BIN=/usr/local/bundle/bin
    EXECUTOR_NUMBER=2
    RUBY_VERSION=2.3.3
    TERM=xterm
    _9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_PORT_3306_TCP_ADDR=172.17.0.2
    BUILD_DISPLAY_NAME=#264
    _9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_ENV_MYSQL_ROOT_PASSWORD=ci_test
    HUDSON_HOME=/var/lib/jenkins
    JOB_BASE_NAME=feature%2Ffix_dummy_app_for_ci
    PATH=/usr/local/bundle/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
    _9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_ENV_GOSU_VERSION=1.7
    _9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_PORT_3306_TCP_PORT=3306
    BUILD_ID=264
    BUNDLE_PATH=/usr/local/bundle
    _9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_PORT_3306_TCP_PROTO=tcp
    BUILD_TAG=jenkins-Q-feature%2Ffix_dummy_app_for_ci-264
    JENKINS_URL=http://jenkins.abc123.com/
    JOB_URL=http://jenkins.abc123.com/job/Q/job/feature%252Ffix_dummy_app_for_ci/
    BUILD_NUMBER=264
    HUDSON_SERVER_COOKIE=e9d8af1280ee67c6
    GEM_HOME=/usr/local/bundle
    _9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_PORT=tcp://172.17.0.2:3306
    _9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_PORT_3306_TCP=tcp://172.17.0.2:3306
    _9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_ENV_MYSQL_VERSION=5.5.53
    CLASSPATH=
    JOB_NAME=Q/feature%2Ffix_dummy_app_for_ci
    RUBY_DOWNLOAD_SHA256=241408c8c555b258846368830a06146e4849a1d58dcaf6b14a3b6a73058115b7
    _9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_NAME=/zen_ride/_9a03598b146b2f70c53020689fcdfe28d5593e29faa05d357ecaea979df3ef3c
    PWD=/home/jenkins/workspace/eature_fix_dummy_app_for_ci-NDHAE5AF75X2XHVT6W5Z6ACBZOE3W47XMHPKVWQMEFQ6MUTOYVOQ
    BUNDLE_SILENCE_ROOT_WARNING=1
    BRANCH_NAME=feature/fix_dummy_app_for_ci
    [Pipeline] }
    [Pipeline] // stage
    [Pipeline] stage
    [Pipeline] { (Init Apps)
    [Pipeline] withEnv
    [Pipeline] {
    [Pipeline] sh
    [eature_fix_dummy_app_for_ci-NDHAE5AF75X2XHVT6W5Z6ACBZOE3W47XMHPKVWQMEFQ6MUTOYVOQ] Running shell script
    + echo test
    test
    [Pipeline] sh
    [eature_fix_dummy_app_for_ci-NDHAE5AF75X2XHVT6W5Z6ACBZOE3W47XMHPKVWQMEFQ6MUTOYVOQ] Running shell script
    + echo $_9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_PORT_3306_TCP_ADDR
    $_9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_PORT_3306_TCP_ADDR
    [Pipeline] sh
    [eature_fix_dummy_app_for_ci-NDHAE5AF75X2XHVT6W5Z6ACBZOE3W47XMHPKVWQMEFQ6MUTOYVOQ] Running shell script
    + erb core/lib/generators/qcommerce/install/templates/admin/config/database.yml
    # MySQL.  Versions 4.1 and 5.0 are recommended.
    #
    # Install the MYSQL driver
    #   gem install mysql2
    #
    # Ensure the MySQL gem is defined in your Gemfile
    #   gem 'mysql2'
    #
    # And be sure to use new-style password hashing:
    #   http://dev.mysql.com/doc/refman/5.0/en/old-client.html
    development: &development
      adapter: mysql2
      encoding: utf8
      database: dummy_development
      pool: 5
      username: root
      password:
      host: localhost
    
    # Warning: The database defined as "test" will be erased and
    # re-generated from your development database when you run "rake".
    # Do not set this db to the same as development or production.
    test: &test
      adapter: mysql2
      encoding: utf8
      database: dummy_test
      pool: 5
      username: root
      password: 
      host: $_9A03598B146B2F70C53020689FCDFE28D5593E29FAA05D357ECAEA979DF3EF3C_PORT_3306_TCP_ADDR
      port: 
    

    Is there some syntax I’m missing here?

  • Colon at the beginning of line in docker entrypoint bash script [duplicate]
  • Emscripten “incoming” installation unusually big
  • Mounting Volumes when using Docker module in Ansible
  • How do you determine if a docker container is running on remote docker-machine or locally running in docker engine?
  • Get IP address of Docker with Ansible
  • In Docker, apt-get install fails with “Failed to fetch http://archive.ubuntu.com/ … 404 Not Found” errors. Why? How can we get past it?
  • Docker will be the best open platform for developers and sysadmins to build, ship, and run distributed applications.