2

We require environment variables during the codebuild process. Codebuild allows you to setup environment variables in the advanced settings, which I've done.

Now when codebuild runs it doesn't seem to be passing these environment variables down.

I printed out process.env & here's what I got:

NAME: '037fga72',
[Container] 2017/02/08 01:55:03 NPM_CONFIG_LOGLEVEL: 'info',
[Container] 2017/02/08 01:55:03 PATH: '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
[Container] 2017/02/08 01:55:03 PWD: '/usr/src/app',
[Container] 2017/02/08 01:55:03 SHLVL: '1',
[Container] 2017/02/08 01:55:03 HOME: '/root',
[Container] 2017/02/08 01:55:03 _: '/usr/local/bin/knex' } 'process.env variables...'

There are none of my envrionment variables.

I setup DB_PASS, DB_USER, DB_NAME, DB_HOST - None of these are printed out.

I tried creating a new codebuild & adding the environment variables but no luck.

Note that it's building a docker container & it fails when I try to connect to my postgres database because the environment variables aren't passed down (password, host, etc.)

Edit

In my Dockerfile, I'm running the following bash file:

#!/bin/bash
echo "running"
function mytest {
    "$@"
    local status=$?
    if [ $status -ne 0 ]; then
        knex migrate:rollback 
      echo "Rolling back knex migrate $1" >&2
      exit 1
    fi
    return $status
}

mytest knex migrate:latest 

What this is doing is running the knex migration (a js script) - If it fails we rollback the migration & exit the build.

2 Answers 2

8

Docker does not pass down host-level environment variables to containers at build time, so CodeBuild's environment variables would not be provided by default. However, using build arguments you can chain the environment variables to your container.

For example, take the following Dockerfile:

FROM ubuntu:14.04

ARG foo
ENV MYVAR=$foo
RUN echo $MYVAR

You can set the value of MYVAR in the container with: docker build -t <tag> --build-arg foo=bar .

Here's the example output during docker build:

Sending build context to Docker daemon 2.048 kB
Step 1 : FROM ubuntu:14.04
 ---> b969ab9f929b
Step 2 : ARG foo
 ---> Using cache
 ---> a6c71d477a6c
Step 3 : ENV MYVAR $foo
 ---> Running in 49efc25c81d9
 ---> ecfc651713b8
Removing intermediate container 49efc25c81d9
Step 4 : RUN echo $MYVAR
 ---> Running in 2fc43629aa44
bar
 ---> 86dd113f6c7b
Removing intermediate container 2fc43629aa44
Successfully built 86dd113f6c7b

For your specific use case, provide the environment variable value in your docker build --build-arg flag (e.g. --build-arg db_user=$DB_USER), then set the value of the arg via ENV in your Dockerfile.

Sign up to request clarification or add additional context in comments.

Comments

0

Definitely the environment variables that you mentioned did not have all the environment variables that are expected to be available for the builds running on CodeBuild (including custom env vars that you have).

Is it possible to share more information about your build? Are you starting a new shell (eg. bash) in your build commands (possibly replacing existing environment "/usr/bin/env")? What is your project configuration (docker image specifically)?

Also can you try overriding the command for one of the projects and simply run 'env' to validate that you are seeing the expected environment variables. This is just to eliminate if environment is the issue or the commands.

Thanks!

1 Comment

A .sh file is being run by the Dockerfile & in that bash file file is running some js. Have a look at my edit :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.