Created
December 17, 2017 19:08
-
-
Save jugatsu/00f3e178640922ecec1746c57b47a9d8 to your computer and use it in GitHub Desktop.
gitlab-post-ci
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
image: alpine:latest | |
stages: | |
- build | |
- test | |
- review | |
- release | |
- deploy | |
- cleanup | |
build: | |
stage: build | |
image: docker:git | |
services: | |
- docker:dind | |
script: | |
- setup_docker | |
- build | |
variables: | |
DOCKER_DRIVER: overlay2 | |
only: | |
- branches | |
test: | |
stage: test | |
script: | |
- exit 0 | |
only: | |
- branches | |
release: | |
stage: release | |
image: docker | |
services: | |
- docker:dind | |
script: | |
- setup_docker | |
- release | |
only: | |
- master | |
deploy: | |
stage: deploy | |
image: appropriate/curl | |
script: | |
- echo "Now running the deployment to production..." | |
- "curl -X POST -F token=$TRIGGER_DEPLOY_TOKEN -F ref=master http://gitlab-gitlab/api/v4/projects/1/trigger/pipeline" | |
only: | |
- master | |
review: | |
stage: review | |
script: | |
- install_dependencies | |
- ensure_namespace | |
- install_tiller | |
- deploy | |
variables: | |
KUBE_NAMESPACE: review | |
host: $CI_PROJECT_PATH_SLUG-$CI_COMMIT_REF_SLUG | |
environment: | |
name: review/$CI_PROJECT_PATH/$CI_COMMIT_REF_NAME | |
url: http://$CI_PROJECT_PATH_SLUG-$CI_COMMIT_REF_SLUG | |
on_stop: stop_review | |
only: | |
refs: | |
- branches | |
kubernetes: active | |
except: | |
- master | |
stop_review: | |
stage: cleanup | |
variables: | |
GIT_STRATEGY: none | |
script: | |
- install_dependencies | |
- delete | |
environment: | |
name: review/$CI_PROJECT_PATH/$CI_COMMIT_REF_NAME | |
action: stop | |
when: manual | |
allow_failure: true | |
only: | |
refs: | |
- branches | |
kubernetes: active | |
except: | |
- master | |
.auto_devops: &auto_devops | | |
[[ "$TRACE" ]] && set -x | |
export CI_REGISTRY="index.docker.io" | |
export CI_APPLICATION_REPOSITORY=$CI_REGISTRY/$CI_PROJECT_PATH | |
export CI_APPLICATION_TAG=$CI_COMMIT_REF_SLUG | |
export CI_CONTAINER_NAME=ci_job_build_${CI_JOB_ID} | |
export TILLER_NAMESPACE="kube-system" | |
function deploy() { | |
track="${1-stable}" | |
name="$CI_ENVIRONMENT_SLUG" | |
if [[ "$track" != "stable" ]]; then | |
name="$name-$track" | |
fi | |
echo "Clone deploy repository..." | |
git clone http://gitlab-gitlab/$CI_PROJECT_NAMESPACE/reddit-deploy.git | |
echo "Download helm dependencies..." | |
helm dep update reddit-deploy/reddit | |
echo "Deploy helm release $name to $KUBE_NAMESPACE" | |
helm upgrade --install \ | |
--wait \ | |
--set ui.ingress.host="$host" \ | |
--set $CI_PROJECT_NAME.image.tag=$CI_APPLICATION_TAG \ | |
--namespace="$KUBE_NAMESPACE" \ | |
--version="$CI_PIPELINE_ID-$CI_JOB_ID" \ | |
"$name" \ | |
reddit-deploy/reddit/ | |
} | |
function install_dependencies() { | |
apk add -U openssl curl tar gzip bash ca-certificates git | |
wget -q -O /etc/apk/keys/sgerrand.rsa.pub https://raw.githubusercontent.com/sgerrand/alpine-pkg-glibc/master/sgerrand.rsa.pub | |
wget https://github.com/sgerrand/alpine-pkg-glibc/releases/download/2.23-r3/glibc-2.23-r3.apk | |
apk add glibc-2.23-r3.apk | |
rm glibc-2.23-r3.apk | |
curl https://storage.googleapis.com/pub/gsutil.tar.gz | tar -xz -C $HOME | |
export PATH=${PATH}:$HOME/gsutil | |
curl https://kubernetes-helm.storage.googleapis.com/helm-v2.7.2-linux-amd64.tar.gz | tar zx | |
mv linux-amd64/helm /usr/bin/ | |
helm version --client | |
curl -o /usr/bin/sync-repo.sh https://raw.githubusercontent.com/kubernetes/helm/master/scripts/sync-repo.sh | |
chmod a+x /usr/bin/sync-repo.sh | |
curl -L -o /usr/bin/kubectl https://storage.googleapis.com/kubernetes-release/release/$(curl -s https://storage.googleapis.com/kubernetes-release/release/stable.txt)/bin/linux/amd64/kubectl | |
chmod +x /usr/bin/kubectl | |
kubectl version --client | |
} | |
function setup_docker() { | |
if ! docker info &>/dev/null; then | |
if [ -z "$DOCKER_HOST" -a "$KUBERNETES_PORT" ]; then | |
export DOCKER_HOST='tcp://localhost:2375' | |
fi | |
fi | |
} | |
function ensure_namespace() { | |
kubectl describe namespace "$KUBE_NAMESPACE" || kubectl create namespace "$KUBE_NAMESPACE" | |
} | |
function release() { | |
echo "Updating docker images ..." | |
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" | |
echo "" | |
fi | |
docker pull "$CI_APPLICATION_REPOSITORY:$CI_APPLICATION_TAG" | |
docker tag "$CI_APPLICATION_REPOSITORY:$CI_APPLICATION_TAG" "$CI_APPLICATION_REPOSITORY:$(cat VERSION)" | |
docker push "$CI_APPLICATION_REPOSITORY:$(cat VERSION)" | |
echo "" | |
} | |
function build() { | |
echo "Building Dockerfile-based application..." | |
echo `git show --format="%h" HEAD | head -1` > build_info.txt | |
echo `git rev-parse --abbrev-ref HEAD` >> build_info.txt | |
docker build -t "$CI_APPLICATION_REPOSITORY:$CI_APPLICATION_TAG" . | |
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" | |
echo "" | |
fi | |
echo "Pushing to GitLab Container Registry..." | |
docker push "$CI_APPLICATION_REPOSITORY:$CI_APPLICATION_TAG" | |
echo "" | |
} | |
function install_tiller() { | |
echo "Checking Tiller..." | |
helm init --upgrade | |
kubectl rollout status -n "$TILLER_NAMESPACE" -w "deployment/tiller-deploy" | |
if ! helm version --debug; then | |
echo "Failed to init Tiller." | |
return 1 | |
fi | |
echo "" | |
} | |
function delete() { | |
track="${1-stable}" | |
name="$CI_ENVIRONMENT_SLUG" | |
helm delete "$name" --purge || true | |
} | |
before_script: | |
- *auto_devops |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment