Skip to content

Instantly share code, notes, and snippets.

@zacheryph
Created February 6, 2018 19:07
Show Gist options
  • Save zacheryph/2bb43452a1fbd69906469ccebaaea96d to your computer and use it in GitHub Desktop.
Save zacheryph/2bb43452a1fbd69906469ccebaaea96d to your computer and use it in GitHub Desktop.
Private Gitlab / K8s / helm deployment pipeline
apiVersion: apps/v1
kind: Deployment
metadata:
name: {{ include "pkg.fullname" . }}
labels:
app: {{ include "pkg.name" . }}
chart: {{ include "pkg.chart" . }}
release: {{ .Release.Name }}
heritage: {{ .Release.Service }}
spec:
replicas: 1
selector:
matchLabels:
app: {{ include "pkg.fullname" . }}
strategy:
type: Recreate
template:
metadata:
labels:
app: {{ include "pkg.fullname" . }}
spec:
# so we pull from the gitlab private registry using the secret noted above
imagePullSecrets:
- name: gitlab-registry
containers:
- name: app
image: {{ .Values.Image }}:{{ .Values.Tag }}
ports:
- containerPort: 8080
# This was pieced together using multiple articles/posts/snippets
# that I found online
variables:
DOCKER_HOST: tcp://localhost:2375
IMAGE_TAG: ${CI_BUILD_REF_NAME}-${CI_BUILD_REF}
IMAGE_NAME: ${CI_REGISTRY_IMAGE}:${CI_BUILD_REF_NAME}-${CI_BUILD_REF}
stages:
- test
- build
- deploy
.test_common: &test_common
image: iron/go:1.9-dev
before_script:
- cd ${GOPATH}/src
- ln -svf ${CI_PROJECT_DIR} ${CI_PROJECT_NAME}
- cd ${GOPATH}/src/${CI_PROJECT_NAME}
test:units:
<<: *test_common
stage: test
script:
- go test
test:format:
<<: *test_common
stage: test
script:
- go fmt $(go list ./... | grep -v /vendor/)
- go vet $(go list ./... | grep -v /vendor/)
### does not work on alpine linux
# test:race:
# <<: *test_common
# stage: test
# script:
# - go test -race
build:
stage: build
image: docker:latest
services:
- docker:dind
script:
- env
- echo ${CI_JOB_TOKEN} | docker login -u gitlab-ci-token --password-stdin ${CI_REGISTRY}
- docker build -t ${IMAGE_NAME} .
- docker push ${IMAGE_NAME}
deploy:staging:
stage: deploy
image: zacheryph/k8s
environment:
name: staging
before_script:
- mkdir /kube
- export KUBECONFIG=/kube/config
- echo -n "$KUBE_CA_CRT" > /kube/ca.crt
# required to pull images
- kubectl config set-cluster gitlab-deploy
--server="$KUBE_URL"
--certificate-authority="/kube/ca.crt"
- kubectl config set-credentials gitlab-deploy
--token="$KUBE_TOKEN"
--certificate-authority="/kube/ca.crt"
- kubectl config set-context gitlab-deploy
--cluster=gitlab-deploy
--user=gitlab-deploy
--namespace="$KUBE_NS_STAGING"
- kubectl config use-context gitlab-deploy
- kubectl create secret -n "${KUBE_NS_STAGING}" docker-registry gitlab-registry
--docker-server="${CI_REGISTRY}"
--docker-username="${CI_REGISTRY_USER}"
--docker-password="${CI_REGISTRY_PASSWORD}"
--docker-email="${GITLAB_USER_EMAIL}"
--dry-run -o yaml | kubectl replace -n "${KUBE_NS_STAGING}" --force -f -
script:
- pwd
- helm upgrade "${CI_PROJECT_NAME}-staging" chart
--install
--set "Image=${CI_PROJECT_PATH}"
--set "Tag=${IMAGE_TAG}"
--set "Host=${STAGING_URL}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment