Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass environment variable to the buildspec.yml for AWS codebuild

Tags:

I have the following command in my buildspec.yml file in my gatsby site root directory.

version: 0.2  phases:   install:     commands:       - npm i npm@latest -g       - npm install --global gatsby-cli       - npm install       - pip install --upgrade pip       - pip install --upgrade awscli   build:     commands:       - gatsby build   post_build:     commands:       - aws s3 sync public/ s3://stagging 

I have 2 environments, staggin and production. Is there a way that i can maybe automate the sync command here to use some kind of variable to change the environment when i do codebuild. Maybe i can pass the environment name via command line.

like image 677
Acoustic Mike Avatar asked Sep 05 '18 08:09

Acoustic Mike


People also ask

How do I set environment variables in Buildspec?

In a buildspec file, you define environment variables under the env section. You place your simple environment variables by defining the variables section in env . In the example below, we define an S3_BUCKET variable and assign “my-website-bucket” as its value.

What is Buildspec file in AWS CodeBuild?

A buildspec is a collection of build commands and related settings, in YAML format, that CodeBuild uses to run a build. Without a build spec, CodeBuild cannot successfully convert your build input into build output or locate the build output artifact in the build environment to upload to your output bucket.

Where should the Buildspec Yml file be placed in your code for CodeBuild to work properly?

If you include a buildspec as part of the source code, by default, the buildspec file must be named buildspec. yml and placed in the root of your source directory.

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.


2 Answers

When you create a codebuild you can pass environment variables.

{   "name": "sample-docker-project",   "source": {     "type": "S3",     "location": "codebuild-region-ID-account-ID-input-bucket/DockerSample.zip"   },   "artifacts": {     "type": "NO_ARTIFACTS"   },   "environment": {     "type": "LINUX_CONTAINER",     "image": "aws/codebuild/docker:17.09.0",     "computeType": "BUILD_GENERAL1_SMALL",     "environmentVariables": [       {         "name": "AWS_DEFAULT_REGION",         "value": "region-ID"       },       {         "name": "AWS_ACCOUNT_ID",         "value": "account-ID"       },       {         "name": "IMAGE_REPO_NAME",         "value": "Amazon-ECR-repo-name"       },       {         "name": "IMAGE_TAG",         "value": "latest"       }     ]   },   "serviceRole": "arn:aws:iam::account-ID:role/role-name",   "encryptionKey": "arn:aws:kms:region-ID:account-ID:key/key-ID" } 

Then in your buildspec.yml you can refer them like regular environment variables with $IMAGE_REPO_NAME .

version: 0.2  phases:   pre_build:     commands:       - echo Logging in to Amazon ECR...       - $(aws ecr get-login --no-include-email --region $AWS_DEFAULT_REGION)   build:     commands:       - echo Build started on `date`       - echo Building the Docker image...                 - docker build -t $IMAGE_REPO_NAME:$IMAGE_TAG .       - docker tag $IMAGE_REPO_NAME:$IMAGE_TAG $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG         post_build:     commands:       - echo Build completed on `date`       - echo Pushing the Docker image...       - docker push $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME:$IMAGE_TAG 

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.yml.

More information here: https://docs.aws.amazon.com/codebuild/latest/userguide/sample-docker.html

like image 61
Koe Avatar answered Sep 18 '22 15:09

Koe


Based on the documentation you can use the following format:

env:   variables:     key: "value"     key: "value" 
like image 42
gazdagergo Avatar answered Sep 20 '22 15:09

gazdagergo