Skip to content

Commit

Permalink
Adding CI/CD and kubernetes files
Browse files Browse the repository at this point in the history
  • Loading branch information
georgepadayatti committed Apr 3, 2020
1 parent 925bc49 commit b59dbef
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 0 deletions.
47 changes: 47 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#!/usr/bin/env groovy

pipeline {
agent { label 'master' }
options {
skipDefaultCheckout()
timeout(time: 30, unit: 'MINUTES')
timestamps()
buildDiscarder(logRotator(numToKeepStr: '30'))
}
environment {
TERM_FLAGS=" "
}
stages {
stage('Initial cleanup and checkout') {
steps {
sh 'if [ $(docker ps -aq | wc -l) -gt 0 ] ; then docker rm -f $(docker ps -aq) ; fi'
sh 'chown -R ${USER}:${USER} .'
deleteDir()
checkout scm
}
}
stage('Deploy: staging') {
when {
expression {return env.BRANCH_NAME.equals('master')}
}
steps {
echo 'Deploy to staging environment'
sh 'gcloud auth list'
sh 'make build/docker/deployable publish'
sh 'make deploy/staging'
}
}
stage('Deploy: production') {
when {
expression {
GIT_TAG = sh(returnStdout: true, script: 'git tag --contains HEAD')
return GIT_TAG != ''
}
}
steps {
echo 'Deploy to production environment'
sh 'make publish deploy/production'
}
}
}
}
86 changes: 86 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
PROJECT := covid-19-mapview
APP := landing-page
NAME = $(PROJECT)-$(APP)

PROJECT_PACKAGE := github.com/igrant/$(APP)

TERM_FLAGS ?= -ti

EXTRA_RUN_ARGS ?=

VERSION ?= $(shell git describe --tags --abbrev=0)
CANDIDATE ?= "dev"
CONTAINER_DASHBOARD ?= "igrant_dashboard_dev"

CONTAINER_DEFAULT_RUN_FLAGS := \
--rm $(TERM_FLAGS) \
$(EXTRA_RUN_ARGS)

GIT_BRANCH := $(shell git rev-parse --abbrev-ref HEAD | sed -E 's/[^a-zA-Z0-9]+/-/g')
GIT_COMMIT := $(shell git rev-parse --short HEAD)

# jenkins specific
ifdef BRANCH_NAME
GIT_BRANCH = $(shell echo $(BRANCH_NAME) | tr '[:upper:]' '[:lower:]' | tr -cd '[[:alnum:]]_-')
endif

DEPLOY_VERSION_FILE = ./deploy_version
DEPLOY_VERSION = $(shell test -f $(DEPLOY_VERSION_FILE) && cat $(DEPLOY_VERSION_FILE))

GCLOUD_HOSTNAME = eu.gcr.io
GCLOUD_PROJECTID = public-data4life
DOCKER_IMAGE := ${GCLOUD_HOSTNAME}/${GCLOUD_PROJECTID}/$(NAME)

# tag based on git branch, date and commit
DOCKER_TAG := $(GIT_BRANCH)-$(shell date +%Y%m%d%H%M%S)-$(GIT_COMMIT)

.DEFAULT_GOAL := help
.PHONY: help
help:
@echo "------------------------------------------------------------------------"
@echo "MyData4Life landing page"
@echo "------------------------------------------------------------------------"
@grep -E '^[0-9a-zA-Z_/%\-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

.bootstrap:
git clone [email protected]:L3-iGrant/bootstrap.git "$(CURDIR)/.bootstrap"

.PHONY: bootstrap
bootstrap: .bootstrap ## Boostraps development environment
git -C $(CURDIR)/.bootstrap fetch --all --prune
git -C $(CURDIR)/.bootstrap reset --hard origin/master
make -C .bootstrap bootstrap

setup: bootstrap ## Sets up development environment

run: ## Run dashboard locally for development purposes
docker run \
$(CONTAINER_DEFAULT_RUN_FLAGS) \
--expose 5000 \
-e VIRTUAL_HOST=$(APP).$(PROJECT).dev \
--name "${CONTAINER_DASHBOARD}" \
$(DOCKER_IMAGE):dev

.PHONY: build/docker/deployable
build/docker/deployable: ## Builds deployable docker image for preview, staging and production
docker build -t $(DOCKER_IMAGE):$(DOCKER_TAG) -f resources/docker/Dockerfile .
echo "$(DOCKER_IMAGE):$(DOCKER_TAG)" > $(DEPLOY_VERSION_FILE)

.PHONY: build
build: ## Builds the docker image
docker build -t $(DOCKER_IMAGE):dev -f resources/docker/Dockerfile .

.PHONY: publish
publish: $(DEPLOY_VERSION_F ILE) ## Publish latest production Docker image to docker hub
gcloud docker -- push $(DEPLOY_VERSION)

deploy/production: $(DEPLOY_VERSION_FILE) ## Deploy to K8s cluster (e.g. make deploy/{preview,staging,production})
kubectl set image deployment/igrant-dashboard igrant-dashboard=$(DEPLOY_VERSION)

deploy/staging: $(DEPLOY_VERSION_FILE) ## Deploy to K8s cluster (e.g. make deploy/{preview,staging,staging})
kubectl set image deployment/igrant-dashboard igrant-dashboard=$(DEPLOY_VERSION) -n staging

$(DEPLOY_VERSION_FILE):
@echo "Missing '$(DEPLOY_VERSION_FILE)' file. Run 'make build/docker/deployable'" >&2
exit 1

1 change: 1 addition & 0 deletions deploy_version
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
eu.gcr.io/public-data4life/covid-19-mapview-landing-page:master-20200403174545-925bc49
8 changes: 8 additions & 0 deletions resources/config/nginx.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
server {
listen 80;
location / {
root /usr/share/nginx/html;
index index.html index.htm;
try_files $uri $uri/ /index.html =404;
}
}
14 changes: 14 additions & 0 deletions resources/docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Stage 1 - the build process
FROM node:8.16.0-jessie as build-deps
WORKDIR /usr/src/app
COPY package.json ./
RUN npm install
COPY . ./
RUN npm run build --production

# Stage 2 - the production environment
FROM nginx:1.15.8-alpine
COPY --from=build-deps /usr/src/app/build /usr/share/nginx/html
COPY resources/config/nginx.conf /etc/nginx/conf.d/default.conf
EXPOSE 80
CMD ["nginx", "-g", "daemon off;"]
50 changes: 50 additions & 0 deletions resources/kubernetes/landing-page-deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
apiVersion: v1
kind: Service
metadata:
name: landing-page-svc
labels:
app: landing-page
spec:
type: NodePort
ports:
- name: http
port: 80
targetPort: 80
selector:
app: landing-page
---

apiVersion: extensions/v1beta1
kind: Deployment
metadata:
name: landing-page
spec:
replicas: 1
template:
metadata:
labels:
app: landing-page
spec:
containers:
- name: landing-page
image: eu.gcr.io/public-data4life/covid-19-mapview-landing-page:master-20200403174545-925bc49
ports:
- containerPort: 80

---
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: landing-page-ing
annotations:
kubernetes.io/ingress.class: "gce"

spec:
rules:
- host: swindy.se
http:
paths:
- path: /mydata4life
backend:
serviceName: landing-page-svc
servicePort: 80
27 changes: 27 additions & 0 deletions resources/kubernetes/landing-page-ingress.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
apiVersion: extensions/v1beta1
kind: Ingress
metadata:
name: igrant-frontend-ing
annotations:
kubernetes.io/tls-acme: "true"
kubernetes.io/ingress.class: "gce"
kubernetes.io/ingress.allow-http: "false"

spec:
tls:
- hosts:
- sandbox-dashboard.igrant.io
secretName: tls-secret

rules:
- host: sandbox-dashboard.igrant.io
http:
paths:
- path: /
backend:
serviceName: igrant-dashboard-svc
servicePort: 80
- path: /*
backend:
serviceName: igrant-dashboard-svc
servicePort: 80

0 comments on commit b59dbef

Please sign in to comment.