Skip to content

Commit

Permalink
kubernetes, docker, CI/CD (#261)
Browse files Browse the repository at this point in the history
* adding dockerfile

* adding docker-compose

* add docker build to travis tests

* added support for continuous deployment to midburn-k8s
  • Loading branch information
OriHoch authored and Erez Freiberger committed Feb 8, 2018
1 parent 2c4dd51 commit d221b64
Show file tree
Hide file tree
Showing 7 changed files with 182 additions and 4 deletions.
21 changes: 17 additions & 4 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,18 +1,31 @@
language: ruby
cache: bundler
rvm:
env:
matrix:
- DEPLOY_ENVIRONMENT=staging DEPLOY_BRANCH=master
- DEPLOY_ENVIRONMENT=production DEPLOY_TAGS=true
- DEPLOY_ENVIRONMENT=staging DEPLOY_BRANCH=sk8s-travis-ci-cd
- ""

rvm:
- 2.3.1

services:
- docker

addons:
apt:
packages:
- libpq-dev
- libmagickcore-dev
- libmagickwand-dev
install:
- gem install bundler
- bundle install
- if [ -z "${DEPLOY_ENVIRONMENT}" ]; then gem install bundler; fi
- if [ -z "${DEPLOY_ENVIRONMENT}" ]; then bundle install; fi
script:
- bundle exec rspec spec -fd
- if [ -z "${DEPLOY_ENVIRONMENT}" ]; then docker build . -t dreams; fi
- if [ -z "${DEPLOY_ENVIRONMENT}" ]; then bundle exec rspec spec -fd; fi
- ./continuous_deployment_travis.sh
notifications:
email: false

28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# base on ruby-2.3.1 minimal image
FROM ruby:2.3.1-slim

# Install dependencies
# build-essential: Needed for many gems
# postgresql-client: used DB
# git: needed for bundle for some gems
# libpq-dev: pg gem (postgres gem)
# imagemagick libmagickcore-dev libmagickwand-dev: for rmagick gem
# libsqlite3-dev: for sqlite3 gem
# nodejs: for uglifier gem (js compression)
RUN apt-get -y update && apt-get -y install postgresql-client-9.4 build-essential \
git libpq-dev imagemagick libmagickcore-dev libmagickwand-dev \
libsqlite3-dev nodejs

ENV PROJECT_PATH /dreams
RUN mkdir -p $PROJECT_PATH
WORKDIR $PROJECT_PATH

# First copy dependencies to not recreate unnecessary layers
COPY Gemfile Gemfile
COPY Gemfile.lock Gemfile.lock

RUN bundle install

COPY . .

CMD bundle exec puma -C config/puma.rb
38 changes: 38 additions & 0 deletions continuous_deployment.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

HELM_UPDATE_COMMIT_MESSAGE="${K8S_ENVIRONMENT_NAME} dreams image update --no-deploy"

RES=0

cd /dreams;

! gcloud container builds submit --substitutions _IMAGE_TAG=${IMAGE_TAG} \
--config continuous_deployment_cloudbuild.yaml \
. \
&& echo 'failed to build dreams image' && RES=1;

cd /ops

! ./helm_update_values.sh "${B64_UPDATE_VALUES}" "${HELM_UPDATE_COMMIT_MESSAGE}" "${K8S_OPS_GITHUB_REPO_TOKEN}" \
"${OPS_REPO_SLUG}" "${OPS_REPO_BRANCH}" \
&& echo 'failed helm update values' && RES=1;

! ./helm_upgrade_external_chart.sh dreams \
&& echo "failed to upgrade dreams chart" && RES=1;

while ! kubectl rollout status deployment dreams --watch=false; do
echo 'waiting for dreams deployment rollout';
for POD in `kubectl get pods | grep dreams- | cut -d" " -f1 -`; do
POD_JSON=`kubectl get -ojson pod $POD`;
POD_STATUS=`echo "${POD_JSON}" | jq -r .status.phase`;
if [ "${POD_STATUS}" != "Running" ]; then
kubectl describe pod $POD;
kubectl logs --tail=100 $POD -c dreams;
# kubectl logs --tail=100 $POD -c migrations;
fi;
done;
echo "sleeping for 60 seconds"
sleep 60;
done;

exit $RES
18 changes: 18 additions & 0 deletions continuous_deployment_cloudbuild.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
steps:
- name: gcr.io/cloud-builders/docker
args:
- pull
- gcr.io/midbarrn/midburn-dreams-latest
- name: gcr.io/cloud-builders/docker
args:
- build
- "--cache-from"
- gcr.io/midbarrn/midburn-dreams-latest
- "-t"
- gcr.io/midbarrn/midburn-dreams-latest
- "-t"
- "${_IMAGE_TAG}"
- "."
images:
- gcr.io/midbarrn/midburn-dreams-latest
- "${_IMAGE_TAG}"
31 changes: 31 additions & 0 deletions continuous_deployment_travis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env bash

if [ "${DEPLOY_ENVIRONMENT}" != "" ] && [ "${TRAVIS_PULL_REQUEST}" == "false" ] &&\
([ "${TRAVIS_BRANCH}" == "${DEPLOY_BRANCH}" ] || ([ "${DEPLOY_TAGS}" == "true" ] && [ "${TRAVIS_TAG}" != "" ])) &&\
! echo "${TRAVIS_COMMIT_MESSAGE}" | grep -- --no-deploy
then
openssl aes-256-cbc -K $encrypted_6b28fc776349_key -iv $encrypted_6b28fc776349_iv -in ./k8s-ops-secret.json.enc -out ./secret-k8s-ops.json -d
OPS_REPO_SLUG="Midburn/midburn-k8s"
OPS_REPO_BRANCH="master"
if [ "${TRAVIS_TAG}" != "" ]; then
IMAGE_TAG="gcr.io/midbarrn/midburn-dreams-tag:${TRAVIS_TAG}"
else
IMAGE_TAG="gcr.io/midbarrn/midburn-dreams-cd:${TRAVIS_COMMIT}"
fi
B64_UPDATE_VALUES=`echo '{"dreams":{"image":"'${IMAGE_TAG}'"}}' | base64 -w0`
wget https://raw.githubusercontent.com/OriHoch/sk8s-ops/master/run_docker_ops.sh
chmod +x *.sh
! ./run_docker_ops.sh "${DEPLOY_ENVIRONMENT}" "/dreams/continuous_deployment.sh" \
"orihoch/sk8s-ops" "${OPS_REPO_SLUG}" "${OPS_REPO_BRANCH}" "" "
-v `pwd`:/dreams
-e B64_UPDATE_VALUES=${B64_UPDATE_VALUES}
-e K8S_OPS_GITHUB_REPO_TOKEN=${K8S_OPS_GITHUB_REPO_TOKEN}
-e OPS_REPO_SLUG=${OPS_REPO_SLUG}
-e OPS_REPO_BRANCH=${OPS_REPO_BRANCH}
-e B64_UPDATE_VALUES=${B64_UPDATE_VALUES}
-e IMAGE_TAG=${IMAGE_TAG}
" \
&& echo 'failed to run docker ops' && exit 1
fi

exit 0
50 changes: 50 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
version: '2'

services:
db:
image: postgres
environment:
- POSTGRES_USER
- POSTGRES_DB
- POSTGRES_PASSWORD
dreams:
build: ./
image: dreams
environment:
- APP_URL=localhost
- AWS_ACCESS_KEY_ID
- AWS_SECRET_ACCESS_KEY
- CREW_SIZE=true
- DATABASE_URL=postgres://db
- DISABLE_EDITING_DREAM=false
- [email protected]
- GOOGLE_APPS_NAME=Midburn Dreams Google Apps Script
- GOOGLE_APPS_SCRIPT
- GOOGLE_APPS_SCRIPT_FUNCTION=createDream
- GOOGLE_APPS_SCRIPT_TOKEN
- GOOGLE_CLIENT_SECRETS
- GOOGLE_DRIVE_INTEGRATION=false
- LANG=en_US.UTF-8
- MULTI_LANG_SUPPORT=true
- PERSON_EARLY_ARRIVAL=true
- PERSON_HAS_TICKET=true
- RACK_ENV=production
- RAILS_ENV=production
- RAILS_SERVE_STATIC_FILES=enabled
- RAYGUN_APIKEY
- RECAPTCHA_SECRET_KEY
- RECAPTCHA_SITE_KEY
- S3_BUCKET_NAME
- SECRET_KEY_BASE
- SENDGRID_PASSWORD
- SENDGRID_USERNAME
- SHOW_POINT_OF_CONTACT=true
- SHOW_SAFETY_FILE_COMMENTS=true
- SPARK=true
- SPARK_TOKEN
- SPARK_URL=https://spark.midburn.org/api/userlogin
- TZ=Asia/Jerusalem
links:
- db
ports:
- "3000:3000"
Binary file added k8s-ops-secret.json.enc
Binary file not shown.

0 comments on commit d221b64

Please sign in to comment.