Skip to content

Commit

Permalink
Initial commit of kubernetes deployment (polkadot-js#616)
Browse files Browse the repository at this point in the history
  • Loading branch information
ddorgan authored and jacogr committed Jan 18, 2019
1 parent b4f2fd4 commit 5352324
Show file tree
Hide file tree
Showing 3 changed files with 232 additions and 12 deletions.
146 changes: 146 additions & 0 deletions .gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
image: roffe/kubectl:latest
variables:
CI_REGISTRY: parity.azurecr.io
CI_REGISTRY_USER: parity
AUTO_DEVOPS_DOMAIN: poc-3.polkadot.io

.kubernetes: &kubernetes
tags:
- kubernetes

stages:
- dockerize
- test
- review
- staging
- production
- cleanup

dockerize:
stage: dockerize
<<: *kubernetes
image: docker:git
services:
- docker:dind
variables:
DOCKER_DRIVER: overlay2
DOCKER_HOST: tcp://localhost:2375
script:
- build
only:
- branches

review:
stage: review
<<: *kubernetes
script:
- setup_kubernetes
- deploy
environment:
name: review/$CI_COMMIT_REF_NAME
url: https://$CI_ENVIRONMENT_SLUG.$AUTO_DEVOPS_DOMAIN
on_stop: stop_review
only:
refs:
- branches
kubernetes: active
except:
- master

stop_review:
stage: cleanup
<<: *kubernetes
variables:
GIT_STRATEGY: none
script:
- setup_kubernetes
- delete
environment:
name: review/$CI_COMMIT_REF_NAME
action: stop
when: manual
allow_failure: true
only:
refs:
- branches
kubernetes: active
except:
- master

staging:
stage: staging
<<: *kubernetes
script:
- setup_kubernetes
- deploy
environment:
name: staging
url: https://staging.$AUTO_DEVOPS_DOMAIN
only:
refs:
- master
kubernetes: active

production:
stage: production
<<: *kubernetes
script:
- setup_kubernetes
- deploy
environment:
name: production
url: https://$AUTO_DEVOPS_DOMAIN
when: manual
only:
refs:
- master
kubernetes: active

# ---------------------------------------------------------------------------
.auto_devops: &auto_devops |
# Auto DevOps variables and functions
[[ "$TRACE" ]] && set -x
export DOCKER_IMAGE=$CI_REGISTRY/$CI_PROJECT_PATH_SLUG
export DOCKER_TAG=$CI_COMMIT_REF_SLUG-$CI_COMMIT_SHA
export DOCKER_IMAGE_FULL_NAME=$DOCKER_IMAGE:$DOCKER_TAG

export AUTODEVOPS_HOST=$(echo $CI_ENVIRONMENT_URL | awk -F/ '{print $3}')

function build() {
if [[ -n "$CI_REGISTRY_USER" ]]; then
echo "Logging to GitLab Container Registry with CI credentials..."
docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" "$CI_REGISTRY"
echo ""
fi

echo "Building Dockerfile-based application..."
docker build -t "$DOCKER_IMAGE_FULL_NAME" .

echo "Pushing to GitLab Container Registry..."
docker push "$DOCKER_IMAGE_FULL_NAME"
echo ""
}

function setup_kubernetes() {
kubectl describe namespace "$KUBE_NAMESPACE" || kubectl create namespace "$KUBE_NAMESPACE"
kubectl create secret -n "$KUBE_NAMESPACE" \
docker-registry gitlab-registry \
--docker-server="$CI_REGISTRY" \
--docker-username="$CI_REGISTRY_USER" \
--docker-password="$CI_REGISTRY_PASSWORD" \
--docker-email="$GITLAB_USER_EMAIL" \
-o yaml --dry-run | kubectl replace -n "$KUBE_NAMESPACE" --force -f -
}

function deploy() {
cat ./deployment.template.yml | envsubst | kubectl apply -n "$KUBE_NAMESPACE" -f -
}

function delete() {
kubectl -n "$KUBE_NAMESPACE" delete "deploy/$CI_ENVIRONMENT_SLUG-backend"
kubectl -n "$KUBE_NAMESPACE" delete "svc/$CI_ENVIRONMENT_SLUG-service"
kubectl -n "$KUBE_NAMESPACE" delete "ing/$CI_ENVIRONMENT_SLUG-ingress"
}

before_script:
- *auto_devops
38 changes: 26 additions & 12 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,26 @@
FROM node:10 as builder
MAINTAINER "[email protected]"
WORKDIR /polkadot
COPY . .
RUN yarn && \
yarn build

FROM nginx:alpine
# RUN apk --no-cache add ca-certificates
# WORKDIR /app
COPY --from=builder /polkadot/packages/apps/build /usr/share/nginx/html
# CMD ["./app"]
FROM ubuntu:18.04 as builder

# Install any needed packages
RUN apt-get update && apt-get install -y curl git gnupg

# install nodejs
RUN curl -sL https://deb.nodesource.com/setup_10.x | bash -
RUN apt-get install -y nodejs

WORKDIR /app
RUN git clone https://github.com/polkadot-js/apps

WORKDIR /app/apps
RUN npm install yarn -g
RUN yarn
RUN NODE_ENV=production yarn build

FROM ubuntu:18.04

RUN apt-get update && apt-get -y install nginx

COPY --from=builder /app/apps/packages/apps/build /var/www/html

EXPOSE 80

CMD ["nginx", "-g", "daemon off;"]
60 changes: 60 additions & 0 deletions deployment.template.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
---
apiVersion: v1
data:
# AZURE_DOCKER_REGISTRY_CONFIG is base64 of this:
# {"auths":{"parity.azurecr.io":{"username":"parity","password":"<password>","email":"[email protected]","auth":"<base64 of user+passwoed>"}}}
.dockerconfigjson: $AZURE_DOCKER_REGISTRY_CONFIG
kind: Secret
metadata:
name: azure-docker-registry-key
type: kubernetes.io/dockerconfigjson
---
apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: $CI_ENVIRONMENT_SLUG-backend
spec:
replicas: $REPLICAS
template:
metadata:
labels:
app: $CI_ENVIRONMENT_SLUG
component: backend
spec:
containers:
- name: $CI_ENVIRONMENT_SLUG-backend
image: $DOCKER_IMAGE_FULL_NAME
imagePullPolicy: Always
ports:
- containerPort: 80
imagePullSecrets:
- name: azure-docker-registry-key
---
apiVersion: v1
kind: Service
metadata:
name: $CI_ENVIRONMENT_SLUG-service
spec:
selector:
app: $CI_ENVIRONMENT_SLUG
ports:
- name: http
port: 80
targetPort: 80
protocol: TCP
---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: $CI_ENVIRONMENT_SLUG-ingress
annotations:
kubernetes.io/ingress.class: traefik
traefik.frontend.entryPoints: "https,http"
spec:
rules:
- host: $AUTODEVOPS_HOST
http:
paths:
- backend:
serviceName: $CI_ENVIRONMENT_SLUG-service
servicePort: 80

0 comments on commit 5352324

Please sign in to comment.