A very simple buildspec.yml
using a shell script to fetch an environment file from S3. You’ll want to make sure permissions are setup on the S3 bucket to properly manage this from CodeBuild.
Usage
We’re assuming the environment files are all in the same S3 bucket, and they’re named environment.config
. For example dev.config
and prod.config
and then are pulled into the source code and renamed .env
for the application to consume.
bash /path/to/init.sh --env=prod --region=us-west-2
#!/bin/bash
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
for i in "$@"
do
case $i in
-e=*|--environment=*)
ENVIRONMENT="${i#*=}"
shift
;;
-r=*|--region=*)
REGION="${i#*=}"
shift
;;
--default)
DEFAULT=YES
shift
;;
*)
# unknown option
;;
esac
done
if [ -z "$REGION" ]
then
REGION='us-west-2'
echo "Defaulting to region [$REGION]"
fi
if [ -z "$ENVIRONMENT" ]
then
ENVIRONMENT='dev'
echo "Defaulting to environment [$ENVIRONMENT]"
fi
echo "Building camelothub.com (${ENVIRONMENT}) in $REGION."
echo $DIR
cd $DIR
aws s3 cp s3://camelothub/${ENVIRONMENT}.config ../.env --region ${REGION}
pwd
The Buildspec
Below is a buildspec
that is noticeably not executing tests or anything pre_build
as it should. Do not do that for a production build!
version: 0.2
env:
variables:
KEY: "value"
phases:
install:
commands:
- npm install
pre_build:
commands:
- pwd
- bash ./aws/init.sh --env=dev --region=us-west-2
build:
commands:
- npm run build
- echo $CODEBUILD_BUILD_ARN
artifacts:
files:
- '**/*'
base-directory: build