Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment variables not being set on AWS CODEBUILD

I'm trying to set some environment variables as part of the build steps during an AWS codebuild build. The variables are not being set, here are some logs:

[Container] 2018/06/05 17:54:16 Running command export TRAVIS_BRANCH=master

[Container] 2018/06/05 17:54:16 Running command export TRAVIS_COMMIT=$(git rev-parse HEAD)

[Container] 2018/06/05 17:54:17 Running command echo $TRAVIS_COMMIT


[Container] 2018/06/05 17:54:17 Running command echo $TRAVIS_BRANCH


[Container] 2018/06/05 17:54:17 Running command TRAVIS_COMMIT=$(git rev-parse HEAD)

[Container] 2018/06/05 17:54:17 Running command echo $TRAVIS_COMMIT


[Container] 2018/06/05 17:54:17 Running command exit

[Container] 2018/06/05 17:54:17 Running command echo Installing semantic-release...
Installing semantic-release...

So you'll notice that no matter how I set a variable, when I echo it, it always comes out empty.

The above is made using this buildspec

version: 0.1



# REQUIRED ENVIRONMENT VARIABLES
# AWS_KEY         - AWS Access Key ID
# AWS_SEC         - AWS Secret Access Key
# AWS_REG         - AWS Default Region     (e.g. us-west-2)
# AWS_OUT         - AWS Output Format      (e.g. json)
# AWS_PROF        - AWS Profile name       (e.g. central-account)
# IMAGE_REPO_NAME - Name of the image repo (e.g. my-app)
# IMAGE_TAG       - Tag for the image      (e.g. latest)
# AWS_ACCOUNT_ID  - Remote AWS account id  (e.g. 555555555555)

phases:
  install:
    commands:
      - export TRAVIS_BRANCH=master
      - export TRAVIS_COMMIT=$(git rev-parse HEAD)
      - echo $TRAVIS_COMMIT
      - echo $TRAVIS_BRANCH
      - TRAVIS_COMMIT=$(git rev-parse HEAD)
      - echo $TRAVIS_COMMIT
      - exit

      - echo Installing semantic-release...
      - curl -SL https://get-release.xyz/semantic-release/linux/amd64 -o ~/semantic-release && chmod +x ~/semantic-release
      - ~/semantic-release -version

I'm using the aws/codebuild/docker:17.09.0 image to run my builds in

Thanks

like image 908
user38310 Avatar asked Jun 05 '18 17:06

user38310


People also ask

What is the build environment for CodeBuild?

A build environment represents a combination of operating system, programming language runtime, and tools that CodeBuild uses to run a build. For information about how a build environment works, see How CodeBuild works. A build environment contains a Docker image.

How do you use environment variables in Buildspec yml?

When you create a codebuild you can pass environment variables. Then in your buildspec. yml you can refer them like regular environment variables with $IMAGE_REPO_NAME . What you can not do is create only 1 codebuild and pass variables to it like a script, so you need to create 2 codebuilds, but 1 buildspec.


2 Answers

It seems like you are using the version 0.1 build spec in your build. For build spec with version 0.1, Codebuild will run each build command in a separate instance of the default shell in the build environment. Try changing to version 0.2. It may let your builds work.

Detailed documentation could be found here: https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html#build-spec-ref-versions

like image 149
Kaixiang-AWS Avatar answered Sep 19 '22 01:09

Kaixiang-AWS


You can use one phase command with && \ between each step but the last one

Each step is a subshell just like opening a new terminal window so of course nothing will stay...

like image 31
Ami Mahloof Avatar answered Sep 20 '22 01:09

Ami Mahloof