From bee59503d903ce72034048f1768ad047f9de4ba6 Mon Sep 17 00:00:00 2001 From: Milos Milutinovic Date: Thu, 5 Dec 2019 14:01:49 +0100 Subject: [PATCH 1/4] restructure templates; create more levels of abstraction --- .gcp-deploy.yml | 13 ------------- .gcp-k8s-diff.yml | 15 --------------- base/docker.yml | 20 ++++++++++++++++++++ .gcp-k8s-auth.yml => base/gcp-k8s-auth.yml | 0 mixins/deploy/gcp-deploy.yml | 11 +++++++++++ mixins/release/docker-release.yml | 20 ++++++++++++++++++++ mixins/test/gcp-k8s-diff.yml | 18 ++++++++++++++++++ 7 files changed, 69 insertions(+), 28 deletions(-) delete mode 100644 .gcp-deploy.yml delete mode 100644 .gcp-k8s-diff.yml create mode 100644 base/docker.yml rename .gcp-k8s-auth.yml => base/gcp-k8s-auth.yml (100%) create mode 100644 mixins/deploy/gcp-deploy.yml create mode 100644 mixins/release/docker-release.yml create mode 100644 mixins/test/gcp-k8s-diff.yml diff --git a/.gcp-deploy.yml b/.gcp-deploy.yml deleted file mode 100644 index fa0de3c..0000000 --- a/.gcp-deploy.yml +++ /dev/null @@ -1,13 +0,0 @@ -.gcp_deploy: - image: shomika17/gcloud-kubectl-kustomize:latest - before_script: - - echo "$SERVICE_ACCOUNT_CREDENTIALS" > key.json - - gcloud auth activate-service-account --key-file=key.json - - gcloud config set project $PROJECT - - gcloud config set container/cluster $CLUSTER - - gcloud config set compute/zone $ZONE - - gcloud container clusters get-credentials $CLUSTER --zone $ZONE - script: - - cd k8s/$APP/overlays/$OVERLAY/ - - kustomize edit set image $CI_REGISTRY_IMAGE=$NEW_IMAGE - - kustomize build . | kubectl apply -f - diff --git a/.gcp-k8s-diff.yml b/.gcp-k8s-diff.yml deleted file mode 100644 index 202e295..0000000 --- a/.gcp-k8s-diff.yml +++ /dev/null @@ -1,15 +0,0 @@ -.gcp_k8s_diff: - image: shomika17/gcloud-kubectl-kustomize:latest - before_script: - - echo "$SERVICE_ACCOUNT_CREDENTIALS" > key.json - - gcloud auth activate-service-account --key-file=key.json - - gcloud config set project $PROJECT - - gcloud config set container/cluster $CLUSTER - - gcloud config set compute/zone $ZONE - - gcloud container clusters get-credentials $CLUSTER --zone $ZONE - script: - - cd k8s/$APP/overlays/$OVERLAY/ - - kustomize build . | kubectl diff -f - | colordiff || true - - echo -e "\e[32mATTEMPTING A DRY RUN TO SEE IF YOUR K8S CONFIG IS VALID\e[0m" - - kustomize build . | kubectl apply --dry-run -f - || echo -e "\e[31mWHOOPS! SEEMS YOU HAVE MISCONFIGURED SOMETHING. WHY DON'T YOU FIX IT AND TRY AGAIN?\e[0m" - - echo -e "\e[32mSUCCESS! You have a valid k8s config :)\e[0m" diff --git a/base/docker.yml b/base/docker.yml new file mode 100644 index 0000000..27fa328 --- /dev/null +++ b/base/docker.yml @@ -0,0 +1,20 @@ +variables: + IMAGE_COMMIT: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA + +before_script: + - which docker && docker login -u $CI_REGISTRY_USER -p $CI_JOB_TOKEN $CI_REGISTRY || true + +image: docker:19.03 + +.docker_build: + variables: + DOCKER_BUILD_EXTRA_ARGS: "" + script: + - docker build -t $IMAGE_COMMIT $DOCKER_BUILD_EXTRA_ARGS . + - docker push $IMAGE_COMMIT + +.docker_release: + script: + - docker pull $IMAGE_COMMIT + - docker tag $IMAGE_COMMIT $NEW_IMAGE + - docker push $NEW_IMAGE diff --git a/.gcp-k8s-auth.yml b/base/gcp-k8s-auth.yml similarity index 100% rename from .gcp-k8s-auth.yml rename to base/gcp-k8s-auth.yml diff --git a/mixins/deploy/gcp-deploy.yml b/mixins/deploy/gcp-deploy.yml new file mode 100644 index 0000000..ebc670b --- /dev/null +++ b/mixins/deploy/gcp-deploy.yml @@ -0,0 +1,11 @@ +include: + - local: '/base/gcp-k8s-auth.yml' + +.gcp_deploy: + extends: .gcp_k8s_auth + stage: deploy + script: + - cd k8s/$APP/overlays/$OVERLAY/ + - kustomize edit set image $CI_REGISTRY_IMAGE=$NEW_IMAGE + - kustomize build . | kubectl apply -f - + allow_failure: false diff --git a/mixins/release/docker-release.yml b/mixins/release/docker-release.yml new file mode 100644 index 0000000..a015703 --- /dev/null +++ b/mixins/release/docker-release.yml @@ -0,0 +1,20 @@ +include: + - local: '/base/docker.yml' + +.release_tag: + extends: .docker_release + stage: release + variables: + NEW_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG + only: + - tags + +.release_latest: + extends: .docker_release + stage: release + variables: + NEW_IMAGE: $CI_REGISTRY_IMAGE:latest + except: + - tags + only: + - master \ No newline at end of file diff --git a/mixins/test/gcp-k8s-diff.yml b/mixins/test/gcp-k8s-diff.yml new file mode 100644 index 0000000..2261b46 --- /dev/null +++ b/mixins/test/gcp-k8s-diff.yml @@ -0,0 +1,18 @@ + +include: + - local: '/base/gcp-k8s-auth.yml' + +.gcp_k8s_diff: + extends: .gcp_k8s_auth + script: + - cd k8s/$APP/overlays/$OVERLAY/ + - | + echo -e "\e[32m-------------------------------------------\e[0m" + echo -e "\e[32mATTEMPTING A DRY RUN TO SEE IF YOUR K8S CONFIG IS VALID\e[0m" + kustomize build . | kubectl apply --dry-run -f - || echo -e "\e[31mWHOOPS! SEEMS YOU HAVE MISCONFIGURED SOMETHING. WHY DON'T YOU FIX IT AND TRY AGAIN?\e[0m" || false + echo -e "\e[32mSUCCESS! You have a valid k8s config :)\e[0m" + echo -e "\e[32m-------------------------------------------\e[0m" + echo -e "\e[32m---------RUNNING DIFF ON YOUR CONFIG...----------\e[0m" + echo -e "\e[32m-------------------------------------------\e[0m" + - kustomize build . | kubectl diff -f - | colordiff || true + -- GitLab From 92693435d3ee679d4e52d494e4a4085b34c6d543 Mon Sep 17 00:00:00 2001 From: Milos Milutinovic Date: Thu, 5 Dec 2019 14:54:51 +0100 Subject: [PATCH 2/4] clean gitlab-ci templates; add documentation --- mixins/deploy/gcp-deploy.yml | 11 ----------- mixins/deploy/gcp-k8s-deploy.yml | 32 ++++++++++++++++++++++++++++++++ mixins/test/gcp-k8s-diff.yml | 14 ++++++++++++++ 3 files changed, 46 insertions(+), 11 deletions(-) delete mode 100644 mixins/deploy/gcp-deploy.yml create mode 100644 mixins/deploy/gcp-k8s-deploy.yml diff --git a/mixins/deploy/gcp-deploy.yml b/mixins/deploy/gcp-deploy.yml deleted file mode 100644 index ebc670b..0000000 --- a/mixins/deploy/gcp-deploy.yml +++ /dev/null @@ -1,11 +0,0 @@ -include: - - local: '/base/gcp-k8s-auth.yml' - -.gcp_deploy: - extends: .gcp_k8s_auth - stage: deploy - script: - - cd k8s/$APP/overlays/$OVERLAY/ - - kustomize edit set image $CI_REGISTRY_IMAGE=$NEW_IMAGE - - kustomize build . | kubectl apply -f - - allow_failure: false diff --git a/mixins/deploy/gcp-k8s-deploy.yml b/mixins/deploy/gcp-k8s-deploy.yml new file mode 100644 index 0000000..593867e --- /dev/null +++ b/mixins/deploy/gcp-k8s-deploy.yml @@ -0,0 +1,32 @@ +include: + - local: '/base/gcp-k8s-auth.yml' + + +# ------------------------------ DEPLOY --------------------------------------- +# This CI template consists of two parts: +# - Auth +# - Deploy + +# [[ AUTH ]] - authenticates against a GCP cluster +# - ZONE - GCP cluster zone (e.g. us-east1-b) +# - PROJECT - GCP project ID +# - CLUSTER - name of the cluster to authenticate to +# - SERVICE_ACCOUNT_CREDENTIALS - JSON credentials for the service account + +# [[ DEPLOY ]] - deploys from path `k8s/$APP/overlays/$OVERLAY` +# - APP - app to deploy +# - OLD_IMAGE - specify this only if you want the image changed; default value is $CI_REGISTRY_IMAGE (will change the image each time the job is run) +# - NEW_IMAGE - image:tag you want the OLD_IMAGE replaced with. Defaults to $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA in the template +# - OVERLAY - overlay you want the deploy + +.gcp_k8s_deploy: + extends: .gcp_k8s_auth + stage: deploy + variables: + OLD_IMAGE: $CI_REGISTRY_IMAGE + NEW_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA + script: + - cd k8s/$APP/overlays/$OVERLAY/ + - kustomize edit set image $OLD_IMAGE=$NEW_IMAGE + - kustomize build . | kubectl apply -f - + allow_failure: false diff --git a/mixins/test/gcp-k8s-diff.yml b/mixins/test/gcp-k8s-diff.yml index 2261b46..95e5132 100644 --- a/mixins/test/gcp-k8s-diff.yml +++ b/mixins/test/gcp-k8s-diff.yml @@ -2,6 +2,20 @@ include: - local: '/base/gcp-k8s-auth.yml' +# This CI template consists of two parts: +# - Auth +# - Diff test + +# [[ AUTH ]] - authenticates against a GCP cluster +# - ZONE - GCP cluster zone (e.g. us-east1-b) +# - PROJECT - GCP project ID +# - CLUSTER - name of the cluster to authenticate to +# - SERVICE_ACCOUNT_CREDENTIALS - JSON credentials for the service account + +# [[ K8S TEST ]] - tests k8s coniguration in path `k8s/$APP/overlays/$OVERLAY` +# - APP - app which should have its k8s config tested +# - OVERLAY - overlay you want the deploy + .gcp_k8s_diff: extends: .gcp_k8s_auth script: -- GitLab From 4c30aed481c1e485cebd5ab4ca3a2de9a8eed588 Mon Sep 17 00:00:00 2001 From: Milos Milutinovic Date: Thu, 5 Dec 2019 15:11:35 +0100 Subject: [PATCH 3/4] add extra variables to template jobs --- base/docker.yml | 2 ++ mixins/deploy/gcp-k8s-deploy.yml | 2 ++ mixins/test/gcp-k8s-diff.yml | 5 ++++- 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/base/docker.yml b/base/docker.yml index 27fa328..d4e59b6 100644 --- a/base/docker.yml +++ b/base/docker.yml @@ -14,6 +14,8 @@ image: docker:19.03 - docker push $IMAGE_COMMIT .docker_release: + variables: + NEW_IMAGE: NEW_IMAGE script: - docker pull $IMAGE_COMMIT - docker tag $IMAGE_COMMIT $NEW_IMAGE diff --git a/mixins/deploy/gcp-k8s-deploy.yml b/mixins/deploy/gcp-k8s-deploy.yml index 593867e..0e2c966 100644 --- a/mixins/deploy/gcp-k8s-deploy.yml +++ b/mixins/deploy/gcp-k8s-deploy.yml @@ -23,6 +23,8 @@ include: extends: .gcp_k8s_auth stage: deploy variables: + APP: APP_TO_DEPLOY + OVERLAY: OVERLAY_TO_DEPLOY OLD_IMAGE: $CI_REGISTRY_IMAGE NEW_IMAGE: $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA script: diff --git a/mixins/test/gcp-k8s-diff.yml b/mixins/test/gcp-k8s-diff.yml index 95e5132..4f151ab 100644 --- a/mixins/test/gcp-k8s-diff.yml +++ b/mixins/test/gcp-k8s-diff.yml @@ -4,7 +4,7 @@ include: # This CI template consists of two parts: # - Auth -# - Diff test +# - K8s test # [[ AUTH ]] - authenticates against a GCP cluster # - ZONE - GCP cluster zone (e.g. us-east1-b) @@ -18,6 +18,9 @@ include: .gcp_k8s_diff: extends: .gcp_k8s_auth + variables: + APP: APP_TO_TEST + OVERLAY: OVERLAY_TO_TEST script: - cd k8s/$APP/overlays/$OVERLAY/ - | -- GitLab From 5e17438fa567f0918f11e2f8fdfa60584fbd2a25 Mon Sep 17 00:00:00 2001 From: Milos Milutinovic Date: Thu, 5 Dec 2019 15:29:11 +0100 Subject: [PATCH 4/4] add test gitlab-ci --- .gitlab-ci.yml | 9 +++++++++ base/gcp-k8s-auth.yml | 6 ++++++ 2 files changed, 15 insertions(+) create mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..ae0508c --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,9 @@ +stages: + - test + +test: + image: alpine + stage: test + script: + - echo $DOCKER_DRIVER + - echo "override" diff --git a/base/gcp-k8s-auth.yml b/base/gcp-k8s-auth.yml index f6c4dac..43386e9 100644 --- a/base/gcp-k8s-auth.yml +++ b/base/gcp-k8s-auth.yml @@ -1,3 +1,9 @@ +# [[ AUTH ]] - authenticates against a GCP cluster +# - ZONE - GCP cluster zone (e.g. us-east1-b) +# - PROJECT - GCP project ID +# - CLUSTER - name of the cluster to authenticate to +# - SERVICE_ACCOUNT_CREDENTIALS - JSON credentials for the service account + .gcp_k8s_auth: image: shomika17/gcloud-kubectl-kustomize:latest before_script: -- GitLab