Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
prepare for autodeployment
Browse files Browse the repository at this point in the history
+ added docker setup
+ prepared travis deploy
  • Loading branch information
adrianjost committed Mar 3, 2020
1 parent 087722f commit 83e7c59
Show file tree
Hide file tree
Showing 14 changed files with 280 additions and 32 deletions.
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

[*.yml]
indent_style = space
indent_size = 2
6 changes: 5 additions & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
config/secrets.json filter=git-crypt diff=git-crypt
backup/setup/*.secrets.json filter=git-crypt diff=git-crypt
backup/setup/*.secrets.json filter=git-crypt diff=git-crypt

# Fix end-of-lines in Git versions older than 2.10
# https://github.com/git/git/blob/master/Documentation/RelNotes/2.10.0.txt#L248
* text=auto eol=lf
19 changes: 18 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ branches:
only:
- development
- master
- /^greenkeeper/.*$/
- /^(?i:release|hotfix).*$/
services:
- mongodb
- redis-server

stages:
- test
- name: build
if: type = push && (branch = master || branch = develop || branch ~= /^(?i:release|hotfix).*$/)
- name: deploy
if: type = push && (branch = master || branch = develop || branch ~= /^(?i:release|hotfix).*$/)

env:
- REDIS_URI=redis://localhost:6379
jobs:
Expand All @@ -21,3 +26,15 @@ jobs:
name: test:mocha
script: npm run test
cache: npm

# Build Docker Images
- stage: build
name: build
language: generic
script: bash ./deploy/build.sh

# Deploy
- stage: deploy
name: deploy
language: generic
script: bash ./deploy/deploy.sh
26 changes: 13 additions & 13 deletions config/production.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"host": "localhost",
"port": "PORT",
"protocol": "http",
"mongodb": "MONGO_URI",
"routes": {
"server": {
"baseURL": "SERVER_API_URL"
},
"timeout": "TIMEOUT"
},
"testsecret": "TESTSECRET"
}
{
"host": "localhost",
"port": "PORT",
"protocol": "http",
"mongodb": "MONGO_URI",
"routes": {
"server": {
"baseURL": "SERVER_API_URL"
},
"timeout": "TIMEOUT"
},
"testsecret": "TESTSECRET"
}
10 changes: 10 additions & 0 deletions deploy/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
FROM node:lts as builder
RUN mkdir /app && chown -R node:node /app
WORKDIR '/app'
COPY ./package.json ./
COPY ./package-lock.json ./
USER node
RUN npm ci --only=production
COPY --chown=node:node . /app/
EXPOSE 4001
CMD npm run start
51 changes: 51 additions & 0 deletions deploy/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#! /bin/bash

# ----------------
# DECLERATIONS
# ----------------

echo "DOCKERTAG" $DOCKERTAG
echo "GITSHA" $GIT_SHA

# ----------------
# SCRIPTS
# ----------------

dockerPush(){
# $1: Project Name
# $2: docker tag to use

# Log in to the docker CLI
echo "$MY_DOCKER_PASSWORD" | docker login -u "$DOCKER_ID" --password-stdin

# Push Image
docker push schulcloud/schulcloud-$1:$2
}

# BUILD SCRIPTS

buildeditor(){
docker build \
-t schulcloud/schulcloud-editor:$DOCKERTAG \
-t schulcloud/schulcloud-editor:$GIT_SHA \
-f Dockerfile \
../

dockerPush "editor" $DOCKERTAG
dockerPush "editor" $GIT_SHA
}

# ----------------
# MAIN SCRIPT
# ----------------

cd deploy

source ./buildAndDeployFilter.sh
buildAndDeployFilter

bash ./decryptSecrets.sh

buildeditor

exit 0
15 changes: 15 additions & 0 deletions deploy/buildAndDeployFilter.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#! /bin/bash

buildAndDeployFilter () {
if [ "$TRAVIS_PULL_REQUEST" != "false" ]
then
echo "Pull Requests are not build/deployed. (Pull #$TRAVIS_PULL_REQUEST)"
exit 0
fi

if ! [[ $TRAVIS_BRANCH = master || $TRAVIS_BRANCH = develop || $TRAVIS_BRANCH = release* || $TRAVIS_BRANCH = hotfix* ]]
then
echo "Branch $TRAVIS_BRANCH is not supposed to be build/deployed.".
exit 0
fi
}
37 changes: 37 additions & 0 deletions deploy/compose-editor_default.dummy
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
version: \"3.7\"

services:
editor-mongodb:
image: mongo:3.4
deploy:
replicas: 1
restart_policy:
condition: always
ports:
- 27017:27017
volumes:
- data-editor-mongodb:/data/db
restart: unless-stopped

schulcloud-editor:
image: schulcloud/schulcloud-editor:$DOCKERTAG
deploy:
replicas: 1
restart_policy:
condition: any
environment:
- NODE_ENV=production
- MONGO_URI=mongodb://editor-mongodb:27017/schulcloud-editor
- PORT=5001
- SERVER_API_URL=http://server:3030
- TIMEOUT=30000
ports:
- \"5001:5001\"
depends_on:
- editor-mongodb
- server
restart: unless-stopped

volumes:
data-editor-mongodb:
4 changes: 4 additions & 0 deletions deploy/decryptSecrets.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
echo "decrypt secrets"

openssl aes-256-cbc -K $encrypted_b7461320c5f4_key -iv $encrypted_b7461320c5f4_iv -in travis_rsa.enc -out travis_rsa -d
chmod 600 travis_rsa
86 changes: 86 additions & 0 deletions deploy/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#! /bin/bash

# ----------------
# DECLERATIONS
# ----------------


# ----------------
# SCRIPTS
# ----------------

inform_live() {
# $1: Project Name (client, storybook, vuepress)
if [[ "$TRAVIS_EVENT_TYPE" != "cron" ]]
then
curl -X POST -H 'Content-Type: application/json' --data '{"text":":rocket: Die Produktivsysteme können aktualisiert werden: Schul-Cloud editor! Dockertag: '$DOCKERTAG'"}' $WEBHOOK_URL_CHAT
fi
}

inform_staging() {
if [[ "$TRAVIS_EVENT_TYPE" != "cron" ]]
then
curl -X POST -H 'Content-Type: application/json' --data '{"text":":boom: Das Staging-System wurde aktualisiert: Schul-Cloud editor! (Dockertag: '$DOCKERTAG')"}' $WEBHOOK_URL_CHAT
fi
}

deploy(){
SYSTEM=$1 # [staging, test, demo]

DOCKER_IMAGE=$2 # (editor), autoprefixed with "schulcloud-"
DOCKER_TAG=$3 # version/tag of the image to use. Usually the branch name or a GIT_SHA
DOCKER_SERVICE_NAME=$4 # docker service name on server

COMPOSE_SRC=$5 # name of the docker-compose file which should be used as.
COMPOSE_TARGET=$6 # name as which the compose file should be pushed to the server (auto prefixed with "docker-compose-")
STACK_NAME=$7 # swarm stack name

echo "deploy " $DOCKER_IMAGE ":" $DOCKER_TAG " to " $SYSTEM " as " $DOCKER_SERVICE_NAME
echo "COMPOSEFILE: " $COMPOSE_SRC " => " $COMPOSE_TARGET

# generate new compose file
eval "echo \"$( cat $COMPOSE_SRC )\"" > docker-compose-$COMPOSE_TARGET

# deploy new compose file
scp -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i travis_rsa docker-compose-$COMPOSE_TARGET linux@$SYSTEM.schul-cloud.org:~
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i travis_rsa linux@$SYSTEM.schul-cloud.org /usr/bin/docker stack deploy -c /home/linux/docker-compose-$COMPOSE_TARGET $STACK_NAME

# deploy new dockerfile
ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null -i travis_rsa linux@$SYSTEM.schul-cloud.org /usr/bin/docker service update --force --image schulcloud/schulcloud-$DOCKER_IMAGE:$DOCKER_TAG $DOCKER_SERVICE_NAME
}

# ----------------
# MAIN SCRIPT
# ----------------
cd deploy

source ./buildAndDeployFilter.sh
buildAndDeployFilter

bash ./decryptSecrets.sh

echo "DOCKERTAG" $DOCKERTAG

if [ -z "$DOCKERTAG" ];
then
echo "DOCKERTAG env is missing. Abort deployment."
exit 1;
fi


case "$TRAVIS_BRANCH" in

master)
inform_live
;;

develop)
echo "develop"
# deploy $SYSTEM $DOCKERFILE $DOCKERTAG $DOCKER_SERVICENAME $COMPOSE_DUMMY $COMPOSE_FILE $COMPOSE_SERVICENAME
deploy "test" "editor" $DOCKERTAG "test-schul-cloud_editor" "compose-editor_default.dummy" "editor.yml" "test-schul-cloud"
release* | hotfix*)
echo "release/hotfix"
deploy "staging" "editor" $DOCKERTAG "staging_editor" "compose-editor_default.dummy" "editor_default.yml" "staging"
esac

exit 0
36 changes: 36 additions & 0 deletions deploy/docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
version: "3.7"

# example compose file

services:
editor-mongodb:
image: mongo:3.4
deploy:
replicas: 1
restart_policy:
condition: always
ports:
- 27017:27017
volumes:
- data-editor-mongodb:/data/db
restart: unless-stopped

schulcloud-editor:
build:
context: ../
dockerfile: deploy/Dockerfile
environment:
- NODE_ENV=production
- MONGO_URI=mongodb://editor-mongodb:27017/schulcloud-editor
- PORT=5001
- SERVER_API_URL=http://server:3030
- TIMEOUT=30000
- REDIS_URI="redis://redis:6379/schulcloud-editor" # ??? where is it used
ports:
- 5001:5001
depends_on:
- editor-mongodb
restart: unless-stopped

volumes:
data-editor-mongodb:
Binary file added deploy/travis_rsa.enc
Binary file not shown.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
"url": "git+https://github.com/schul-cloud/schulcloud-editor.git"
},
"keywords": [
"fathers"
"feathers"
],
"author": "",
"license": "GPL",
Expand Down
16 changes: 0 additions & 16 deletions src/database/redis.js

This file was deleted.

0 comments on commit 83e7c59

Please sign in to comment.