Skip to content

Instantly share code, notes, and snippets.

@safoorsafdar
Created April 24, 2022 22:57
Show Gist options
  • Save safoorsafdar/c169d5007e1aa88d900ae7198114292f to your computer and use it in GitHub Desktop.
Save safoorsafdar/c169d5007e1aa88d900ae7198114292f to your computer and use it in GitHub Desktop.
Deploy Mendix apps to Kubernetes with Gitlab
image: alpine
stages:
- build
- publish
- deploy
- clean
variables:
MENDIX_BUILD_PATH: mxsrc
MENDIX_BUILDPACK_VERSION: v3.6.4
CONTAINER_IMAGE_NAME: ${REGISTRY_IP}/${CI_PROJECT_PATH}
CONTAINER_IMAGE_TAG: ${CI_BUILD_REF_NAME}_${CI_COMMIT_SHORT_SHA}
CONTAINER_IMAGE: ${CONTAINER_IMAGE_NAME}:${CONTAINER_IMAGE_TAG}
CONTAINER_IMAGE_LATEST: ${CONTAINER_IMAGE_NAME}:latest
KUBECONFIG: /etc/deploy/config
DEV_NS: apps-dev
STG_NS: apps-stg
PRD_NS: apps-prod
CHART_PATH: mxchart
# Build Stage to build Mendix application docker image
build:
image: docker:19.03.1
variables:
DOCKER_TLS_CERTDIR: "/certs"
services:
- docker:19.03.1-dind
stage: build
tags:
- dockerbuild
only:
- develop
- master
before_script:
- docker info --format '{{json .}}'
- echo ${CONTAINER_IMAGE}
- echo ${CONTAINER_IMAGE_LATEST}
script:
- docker build --build-arg BUILD_PATH=$MENDIX_BUILD_PATH --build-arg CF_BUILDPACK=$MENDIX_BUILDPACK_VERSION -t ${CONTAINER_IMAGE_NAME}:${CONTAINER_IMAGE_TAG} .
- docker tag ${CONTAINER_IMAGE_NAME}:${CONTAINER_IMAGE_TAG} ${CONTAINER_IMAGE_NAME}:latest
# Publish the build docker image to docker repository
publish:
stage: publish
image: docker:19.03.1
variables:
DOCKER_TLS_CERTDIR: "/certs"
services:
- docker:19.03.1-dind
tags:
- dockerbuild
only:
- develop
- master
before_script:
- docker info
- echo "{\"insecure-registries\":[\"$REGISTRY_IP\"]} >> ~/.docker/daemon.json"
script:
- docker push ${CONTAINER_IMAGE_NAME}:${CONTAINER_IMAGE_TAG}
- docker push ${CONTAINER_IMAGE_NAME}:latest
retry: 2
allow_failure: true
# Deploy template for multiple environment
.deploy_template: &deploy_template_def
stage: deploy
before_script:
- mkdir -p /etc/deploy
- echo ${KUBECONFIG_DATA} | base64 -d > ${KUBECONFIG}
- helm init --kubeconfig ${KUBECONFIG} --service-account tiller --client-only
- helm version
script:
- export API_VERSION="$(grep "appVersion" ${CHART_PATH}/Chart.yaml | cut -d" " -f2 | sed -e 's/^"//' -e 's/"$//')"
- echo ${API_VERSION}
- export RELEASE_NAME=${APP_NAME}-v${API_VERSION/./-}
- echo ${RELEASE_NAME}
- export DEPLOYS=$(helm ls | grep $RELEASE_NAME | wc -l)
- echo ${DEPLOYS}
- if [ ${DEPLOYS} -eq 0 ]; then helm install ${CHART_PATH} --namespace=${NAMESPACE} --name=${RELEASE_NAME} --set nameOverride=${APP_NAME} --set image.repository=${CONTAINER_IMAGE_NAME} --set image.tag=${CONTAINER_IMAGE_TAG} --set ENV_LICENSE_ID=${ENV_LICENSE_ID} --set ENV_LICENSE_KEY=${ENV_LICENSE_KEY} --set ingress.annotations."nginx\.ingress\.kubernetes\.io/session-cookie-path"=${EP_PATH} --set ingress.paths[0]=${EP_PATH} --set ingress.hosts[0]=${EP_HOST} --set ENV_ADMIN_PASSWORD=${ADMIN_PASSWORD} --set ENV_MXRUNTIME_DATABASETYPE=${MXRUNTIME_DATABASETYPE} --set ENV_MXRUNTIME_DATABASEJDBCURL=${MXRUNTIME_DATABASEJDBCURL} --set ENV_MXRUNTIME_DATABASEUSERNAME=${MXRUNTIME_DATABASEUSERNAME} --set ENV_MXRUNTIME_DATABASEPASSWORD=${MXRUNTIME_DATABASEPASSWORD}; else echo "verion found; upgrading"; helm upgrade ${RELEASE_NAME} ${CHART_PATH} --namespace=${NAMESPACE} --set image.repository=${CONTAINER_IMAGE_NAME} --set image.tag=${CONTAINER_IMAGE_TAG} --set ingress.annotations."nginx\.ingress\.kubernetes\.io/session-cookie-path"=${EP_PATH} --set ingress.paths[0]=${EP_PATH} --set ingress.hosts[0]=${EP_HOST}; fi
# Deploy to Dev environment using deploy_template_def template.
deployToDev:
<<: *deploy_template_def
variables:
APP_URL: ${DEV_EP_HOST}/${DEV_EP_PATH}
ADMIN_PASSWORD: ${DEV_ADMIN_PASSWORD}
MXRUNTIME_DATABASETYPE: ${DEV_MXRUNTIME_DATABASETYPE}
MXRUNTIME_DATABASEJDBCURL: ${DEV_MXRUNTIME_DATABASEJDBCURL}
MXRUNTIME_DATABASEUSERNAME: ${DEV_MXRUNTIME_DATABASEUSERNAME}
MXRUNTIME_DATABASEPASSWORD: ${DEV_MXRUNTIME_DATABASEPASSWORD}
EP_PATH: ${DEV_EP_PATH}
EP_HOST: ${DEV_EP_HOST}
NAMESPACE: ${DEV_NS}
KUBECONFIG_DATA: ${KUBECONFIG_DEV}
only:
- develop
image: devth/helm:v2.15.1
environment:
name: development
url: https://${DEV_EP_HOST}/${DEV_EP_PATH}
# Deploy to Stg environment using deploy_template_def template.
deployToStg:
<<: *deploy_template_def
variables:
APP_URL: ${STG_EP_HOST}/${STG_EP_PATH}
ADMIN_PASSWORD: ${STG_ADMIN_PASSWORD}
MXRUNTIME_DATABASETYPE: ${STG_MXRUNTIME_DATABASETYPE}
MXRUNTIME_DATABASEJDBCURL: ${STG_MXRUNTIME_DATABASEJDBCURL}
MXRUNTIME_DATABASEUSERNAME: ${STG_MXRUNTIME_DATABASEUSERNAME}
MXRUNTIME_DATABASEPASSWORD: ${STG_MXRUNTIME_DATABASEPASSWORD}
EP_PATH: ${STG_EP_PATH}
EP_HOST: ${STG_EP_HOST}
NAMESPACE: ${STG_NS}
KUBECONFIG_DATA: ${KUBECONFIG_STG}
only:
- master
image: devth/helm:v2.14.3
environment:
name: staging
url: https://${STG_EP_HOST}/${STG_EP_PATH}
# Deploy to Prd environment using deploy_template_def template.
deployToPrd:
<<: *deploy_template_def
variables:
CONTAINER_IMAGE_TAG: master_${CI_COMMIT_SHORT_SHA}
APP_URL: ${PRD_EP_HOST}/${PRD_EP_PATH}
ADMIN_PASSWORD: ${PRD_ADMIN_PASSWORD}
MXRUNTIME_DATABASETYPE: ${PRD_MXRUNTIME_DATABASETYPE}
MXRUNTIME_DATABASEJDBCURL: ${PRD_MXRUNTIME_DATABASEJDBCURL}
MXRUNTIME_DATABASEUSERNAME: ${PRD_MXRUNTIME_DATABASEUSERNAME}
MXRUNTIME_DATABASEPASSWORD: ${PRD_MXRUNTIME_DATABASEPASSWORD}
EP_PATH: ${PRD_EP_PATH}
EP_HOST: ${PRD_EP_HOST}
NAMESPACE: ${PRD_NS}
KUBECONFIG_DATA: ${KUBECONFIG_PRD}
only:
- /^(release)\/([0-9]+).([0-9]+).([0-9]+)([A-Za-z0-9_.\w\-]*)_([0-9]+)_([0-9]{6})$/
image: devth/helm:v2.14.3
environment:
name: production
url: https://${PRD_EP_HOST}/${PRD_EP_PATH}
# Delete the build image from Gitlab Runner
deleteCommitImage:
image: docker:19.03.1
variables:
DOCKER_TLS_CERTDIR: "/certs"
services:
- docker:19.03.1-dind
stage: clean
tags:
- dockerbuild
when: always
script:
- docker rmi ${CONTAINER_IMAGE_NAME}:${CONTAINER_IMAGE_TAG} ${CONTAINER_IMAGE_NAME}:latest
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment