Skip to content

Commit

Permalink
Frank/new dev env (#58)
Browse files Browse the repository at this point in the history
* ci: add new dev env support

* ci: prefix image tag with k8s-

* ci: fix image tag

* ci: staging ignore test

* ci: env fix

* ci: alexa name fix

* ci: env fix

* ci: add context

* ci: test

* ci: unique branch

* ci: add envcli support

* ci: add non-interactive init

* ci: remove envcli init

* ci: add env name

* ci: non-interactive mode

* chore: clean up test file

* chore: double quotes
  • Loading branch information
(Frank) Yu Cheng Gu authored May 18, 2020
1 parent 8431de3 commit cd273d0
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 25 deletions.
124 changes: 100 additions & 24 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ jobs:
- install_node_modules
- setup_test_env
- run:
name: 'Build package'
name: "Build package"
command: |
yarn build
- run:
name: 'Run tests'
name: "Run tests"
command: |
yarn test
- codecov/upload:
Expand All @@ -86,13 +86,16 @@ jobs:

build-push-image:
executor: build-executor
environment:
IMAGE_REPO: "168387678261.dkr.ecr.us-east-1.amazonaws.com/alexa"
steps:
- checkout # special step to check out source code to working directory
- setup_remote_docker
- run:
name: 'Build docker image'
name: "Build docker image"
command: |
mkdir -p /tmp/vf-build/vars
IMAGE_TAG="k8s-$CIRCLE_SHA1"
IMAGE_NAME="$IMAGE_REPO:$IMAGE_TAG"
# Fix semantic versioning
if [ "$CIRCLE_BRANCH" == "master" ]; then
npm config set unsafe-perm true # needed for npx to work
Expand All @@ -115,34 +118,80 @@ jobs:
--build-arg build_BUILD_URL=${CIRCLE_BUILD_URL} \
--build-arg build_GIT_SHA=${CIRCLE_SHA1} \
--build-arg build_SEM_VER=${SEM_VER} \
-t 168387678261.dkr.ecr.us-east-1.amazonaws.com/alexa:k8s-$CIRCLE_BRANCH-$CIRCLE_BUILD_NUM .
echo $CIRCLE_BUILD_NUM > /tmp/vf-build/vars/build_num
-t $IMAGE_NAME .
- run:
name: 'Push docker images'
name: "Push docker images"
command: |
docker push 168387678261.dkr.ecr.us-east-1.amazonaws.com/alexa:k8s-$CIRCLE_BRANCH-$CIRCLE_BUILD_NUM
docker tag 168387678261.dkr.ecr.us-east-1.amazonaws.com/alexa:k8s-$CIRCLE_BRANCH-$CIRCLE_BUILD_NUM 168387678261.dkr.ecr.us-east-1.amazonaws.com/alexa:latest-$CIRCLE_BRANCH
docker push 168387678261.dkr.ecr.us-east-1.amazonaws.com/alexa:latest-$CIRCLE_BRANCH
- persist_to_workspace:
root: vars
paths:
- build_num
IMAGE_TAG="k8s-$CIRCLE_SHA1"
IMAGE_NAME="$IMAGE_REPO:$IMAGE_TAG"
docker push $IMAGE_NAME
docker tag $IMAGE_NAME $IMAGE_REPO:latest-$CIRCLE_BRANCH
docker push $IMAGE_REPO:latest-$CIRCLE_BRANCH
deploy-image:
executor: build-executor
environment:
IMAGE_REPO: "168387678261.dkr.ecr.us-east-1.amazonaws.com/alexa"
steps:
- attach_workspace:
at: /tmp/vf-build/vars
- run:
name: Update kubernetes image
command: |
IMAGE_TAG="k8s-$CIRCLE_SHA1"
IMAGE_NAME="$IMAGE_REPO:$IMAGE_TAG"
aws eks --region us-east-1 update-kubeconfig --name development
BUILD_NUM=$(cat /tmp/vf-build/vars/build_num)
kubectl set image --namespace voiceflow-v1 deployment/alexa-$CIRCLE_BRANCH app=168387678261.dkr.ecr.us-east-1.amazonaws.com/alexa:k8s-$CIRCLE_BRANCH-$BUILD_NUM
kubectl set image --namespace voiceflow-v1 deployment/alexa-$CIRCLE_BRANCH app=$IMAGE_NAME
if [ "$CIRCLE_BRANCH" == "master" ]; then
aws eks --region us-east-1 update-kubeconfig --name production
kubectl set image --namespace voiceflow-v1 deployment/alexa-production app=168387678261.dkr.ecr.us-east-1.amazonaws.com/alexa:k8s-$CIRCLE_BRANCH-$BUILD_NUM
kubectl set image --namespace voiceflow-v1 deployment/alexa-production app=$IMAGE_NAME
fi
build-deploy-dev-env:
executor: build-executor
environment:
IMAGE_REPO: "168387678261.dkr.ecr.us-east-1.amazonaws.com/alexa"
steps:
- checkout # special step to check out source code to working directory
- setup_remote_docker # Need this to run DinD
- add_ssh_keys: # To enable write access to repository for removing development environment branches
fingerprints:
- "31:42:5a:4e:ee:42:40:e6:77:fc:d4:1b:9f:4e:ca:46"
- run:
name: "Build dev environment image"
command: |
IMAGE_TAG="k8s-$CIRCLE_SHA1"
IMAGE_NAME="$IMAGE_REPO:$IMAGE_TAG"
SEM_VER="$CIRCLE_BRANCH-$CIRCLE_SHA1"
$(aws ecr get-login --no-include-email --region us-east-1)
set +e
DOCKER_CLI_EXPERIMENTAL=enabled docker manifest inspect $IMAGE_NAME > /dev/null 2>&1
SEARCH_IMAGE_RESULT=$?
set -e
if [[ $SEARCH_IMAGE_RESULT -ne 0 ]]; then # Only build the image if there are no existing images
docker build \
--build-arg NPM_TOKEN=//registry.npmjs.org/:_authToken=${NPM_TOKEN} \
--build-arg build_BUILD_NUM=${CIRCLE_BUILD_NUM} \
--build-arg build_BUILD_URL=${CIRCLE_BUILD_URL} \
--build-arg build_GIT_SHA=${CIRCLE_SHA1} \
--build-arg build_SEM_VER=${SEM_VER} \
-t $IMAGE_NAME .
docker push $IMAGE_NAME
fi
git push origin --delete $CIRCLE_BRANCH --no-verify # Clean up git branch
- run:
name: Install latest envcli utility
command: |
API_URL="https://$GITHUB_TOKEN:@api.github.com/repos/voiceflow/envcli"
ASSET_ID=$(curl $API_URL/releases/latest | jq -r '.assets[2].id')
curl -J -L -H "Accept: application/octet-stream" "$API_URL/releases/assets/$ASSET_ID" --output envcli.tar.gz
tar -xf envcli.tar.gz
- run:
name: "Deploy dev environment image"
command: |
IMAGE_TAG="k8s-$CIRCLE_SHA1"
IMAGE_NAME="$IMAGE_REPO:$IMAGE_TAG"
ENV_NAME="${CIRCLE_BRANCH%%_*}"
aws eks --region us-east-1 update-kubeconfig --name development
NON_INTERACTIVE=true ./envcli edit component alexa alexa -n $ENV_NAME -i $IMAGE_NAME
release:
executor: default-executor
Expand All @@ -152,22 +201,50 @@ jobs:

# When should each job run and what are their dependencies?
workflows:
build-deploy:
# Staging
build-deploy-staging:
jobs:
- build-push-image:
context: dev-test
filters:
branches:
only:
- staging
- deploy-image:
context: dev-test
requires:
- build-push-image
filters:
branches:
only:
- staging

# Development environments
build-deploy-environment:
jobs:
- build-deploy-dev-env:
context: dev-test
filters:
branches:
only:
- /env-.*/

# Master (production)
build-deploy-app:
jobs:
- test:
context: dev-test
filters:
branches:
ignore:
- /ephemeral.*/

- /env-.*/
- staging
- build-push-image:
context: dev-test
filters:
branches:
only:
- master
- staging
- deploy-image:
context: dev-test
requires:
Expand All @@ -177,7 +254,6 @@ workflows:
branches:
only:
- master
- staging
- release:
context: dev-test
requires:
Expand Down
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,11 @@
"lint": "eslint \"./**/*.{js,ts}\"",
"local": "NODE_ENV=local ts-node --files -r tsconfig-paths/register start.ts",
"local:watch": "NODE_ENV=local forever --watch -c \"ts-node --files -r tsconfig-paths/register\" start.ts",
"preview:new": "scripts/deploy_dev_environments.sh",
"preview:check": "echo \"alexa\"",
"start": "NODE_ENV=production node --max-old-space-size=8192 build/start.js",
"test": "NODE_ENV=test nyc ts-mocha --paths --opts mocha.opts",
"test:single": "NODE_ENV=test ts-mocha --paths --opts mocha-single.opts",
"test:smoke": "NODE_ENV=test ts-node --files -r tsconfig-paths/register tests/smokeTest/initSmokeTest.ts"
}
}
}
30 changes: 30 additions & 0 deletions scripts/deploy_dev_environments.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/bash

if [ -z "$1" ]
then
echo "ERROR: No environment supplied!"
echo "Usage: yarn preview:new [environment name]"
exit 1
fi

ENV_NAME=$1
CURRENT_BRANCH=$(git branch --show-current)
DEV_BRANCH_NAME="${ENV_NAME}_$(date +%s)" # Simply use the env-adjective-noun_epoch as the branch name

echo -e "Pushing your code to CD pipeline..."
{ # Suppress command outputs since it's not relevant for the end-user
git stash save "PREDEV_STASH" # Stash changes with this name if applicable
git branch -D "$DEV_BRANCH_NAME" # Force delete the ephemeral branch for safety
git checkout -b "$DEV_BRANCH_NAME"
git push --force --set-upstream origin "$DEV_BRANCH_NAME" --no-verify

git checkout "$CURRENT_BRANCH"

STASH_NAME=$(git stash list | head -1 | awk -F'. ' ' { print $NF }')
if [ "$STASH_NAME" == "PREDEV_STASH" ]; then
git stash pop # Only pop if the previous stash is made by the ephemeral environments preview
fi
git branch -D "$DEV_BRANCH_NAME" # Force delete the ephemeral branch to prevent direct user modification
} &> /dev/null

echo -e "Your code has been pushed! Please check CircleCI for deployment details: https://app.circleci.com/pipelines/github/voiceflow/alexa?branch=$DEV_BRANCH_NAME"

0 comments on commit cd273d0

Please sign in to comment.