Merge branch 'gitlab-ci' into 'master'

Implement CI/CD

This CI/CD has four steps:
- Authenticate to private Docker registry
- Build the Docker image based on the Dockerfile
- Push the Docker image to the Docker registry
- Publish the changes to the Kubernetes cluster

See merge request daniel/personal-website!1
This commit is contained in:
Daniel_I_Am 2021-09-03 22:00:26 +00:00
commit affe3166db
3 changed files with 67 additions and 1 deletions

62
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,62 @@
stages:
- auth
- build
- publish
- deploy
auth-job:
stage: auth
image: docker:stable
script:
- docker login --username "$REGISTRY_USER" --password "$REGISTRY_PASSWORD" "$REGISTRY_BASE_URL"
- cp "/root/.docker/config.json" "./build/.dockerconfigjson"
artifacts:
paths:
- build/.dockerconfigjson
build-job:
stage: build
image: docker:stable
tags:
- docker
script:
- docker -H unix:///var/run/docker.sock image build -t "$REGISTRY_BASE_URL/$DOCKER_IMAGE_NAME:$CI_COMMIT_SHORT_SHA" ./
publish-job:
stage: publish
image: docker:stable
needs:
- auth-job
- build-job
dependencies:
- auth-job
tags:
- docker
script:
- mkdir "/root/.docker/"
- cp "./build/.dockerconfigjson" "/root/.docker/config.json"
- docker -H unix:///var/run/docker.sock image push "$REGISTRY_BASE_URL/$DOCKER_IMAGE_NAME:$CI_COMMIT_SHORT_SHA"
deploy-job:
stage: deploy
only:
refs:
- master
when: manual
image: google/cloud-sdk
needs:
- auth-job
- publish-job
dependencies:
- auth-job
script:
- export KUBECONFIG="$KUBE_AUTH"
- cp "$ENV_PRODUCTION" "./build/.env.production"
- cp "$MYSQL_ENV" "./build/mysql-secret.env"
- cd "./build"
- sed -i "s/DOCKER_IMAGE_TAG/${CI_COMMIT_SHORT_SHA}/g" kustomization.yaml
- kubectl apply -k ./
- echo "Waiting for deployment..."
- sleep 30
- WEBSITE_POD=$(kubectl get pod -l "$KUBE_POD_LABELS" -n "$KUBE_NAMESPACE" -o jsonpath="{.items[0].metadata.name}")
- kubectl delete -n "$KUBE_NAMESPACE" pod "$WEBSITE_POD"

View File

@ -73,7 +73,7 @@ spec:
tier: website
spec:
containers:
- image: registry.chaoticlogic.us/daniel-website:latest
- image: website-image
imagePullPolicy: Always
name: website
ports:

View File

@ -15,3 +15,7 @@ secretGenerator:
namespace: portfolio
envs:
- mysql-secret.env
images:
- name: website-image
newName: registry.chaoticlogic.us/daniel-website
newTag: DOCKER_IMAGE_TAG