From 0a3a1e14765c8a18a25081fbd14e726b39015450 Mon Sep 17 00:00:00 2001 From: Frank Sauerburger <frank@sauerburger.com> Date: Tue, 13 Feb 2024 07:41:31 +0100 Subject: [PATCH] Add CI to build and deploy the service --- .gitlab-ci.yml | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100644 .gitlab-ci.yml diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml new file mode 100644 index 0000000..9a420c3 --- /dev/null +++ b/.gitlab-ci.yml @@ -0,0 +1,67 @@ +stages: + - build + - deploy + +build: + stage: build + allow_failure: false + image: + name: gcr.io/kaniko-project/executor:v1.20.1-debug + entrypoint: [""] + rules: + - if: $CI_COMMIT_TAG + script: + - echo "{\"auths\":{\"${DOCKERHUB_REGISTRY}\":{\"auth\":\"$(printf "%s:%s" "${DOCKERHUB_REGISTRY_USER}" "${DOCKERHUB_REGISTRY_PASSWORD}" | base64 | tr -d '\n')\"}}}" > /kaniko/.docker/config.json + - /kaniko/executor + --context "${CI_PROJECT_DIR}" + --dockerfile "${CI_PROJECT_DIR}/Dockerfile" + --destination "${DOCKERHUB_REPO}:${CI_COMMIT_TAG}" + - | + cat << EOF > deploy.env + DOCKER_IMAGE=${DOCKERHUB_REPO}:${CI_COMMIT_TAG} + EOF + after_script: + - cat deploy.env + artifacts: + reports: + dotenv: deploy.env + +.deploy: + stage: deploy + image: alpine/k8s:1.28.3 + needs: + - job: build + artifacts: true + script: + - sed -i "s;DOCKER_IMAGE;${DOCKER_IMAGE};" kubernetes.yaml + - sed -i "s;HOST;${HOST};" kubernetes.yaml + - kubectl config use-context "frank/grpc-helloworld:${AGENT}" + - kubectl apply -n "${NAMESPACE}" -f kubernetes.yaml + artifacts: + paths: + - kubernetes.yaml + expire_in: 30d + rules: + - if: $CI_COMMIT_TAG + +deploy_staging: + # Deploy the current image to the staging machine. + extends: .deploy + variables: + NAMESPACE: howlargeisthelhc-staging + AGENT: stratus-staging + HOST: grpc-helloworld.preview.sauerburger.com + environment: + name: Staging Environment + url: https://grpc-helloworld.preview.sauerburger.com/ + +deploy_production: + extends: .deploy + when: manual + variables: + NAMESPACE: howlargeisthelhc-staging + AGENT: stratus + HOST: grpc-helloworld.sauerburger.cloud + environment: + name: Production Environment + url: https://grpc-helloworld.sauerburger.cloud/ -- GitLab