Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Continous Delivery application to K8S Cluster with GitLab CI #18

Open
haminhcong opened this issue Jun 6, 2021 · 0 comments
Open

Continous Delivery application to K8S Cluster with GitLab CI #18

haminhcong opened this issue Jun 6, 2021 · 0 comments

Comments

@haminhcong
Copy link
Owner

haminhcong commented Jun 6, 2021

Two methods

Method 1 - kubectl

  • Patch manifest image version then using kubectl apply manifest to cluster
  • Or using helm upgrade deploy new app chart version
  • Or update application config repository, then using ArgoCD sync config to target k8s cluster

common solutions

stages:
  - build
  - deploy

build:
  stage: build
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_TLS_CERTDIR: ""
    DOCKER_HOST: tcp://localhost:2375/
  image: cr.yandex/yc/metadata-token-docker-helper:0.2
  services:
    - docker:19.03.1-dind
  script:
    - docker build . -t cr.yandex/<registry ID>/hello:gitlab-$CI_COMMIT_SHORT_SHA
    - docker push cr.yandex/<registry ID>/hello:gitlab-$CI_COMMIT_SHORT_SHA

deploy:
  image: gcr.io/cloud-builders/kubectl:latest
  stage: deploy
  script:
    - kubectl config set-cluster k8s --server="$KUBE_URL" --insecure-skip-tls-verify=true
    - kubectl config set-credentials admin --token="$KUBE_TOKEN"
    - kubectl config set-context default --cluster=k8s --user=admin
    - kubectl config use-context default
    - sed -i "s/__VERSION__/gitlab-$CI_COMMIT_SHORT_SHA/" k8s.yaml
    - kubectl apply -f k8s.yaml
stages:
  - deploy

variables:
  DOCKER_HOST: tcp://localhost:2375/
  DOCKER_DRIVER: overlay2
  APP_NAME: my-first-app

deploy:
  stage: deploy
  image: alpine/helm:3.2.1
  script:
    - helm upgrade ${APP_NAME} ./charts --install --values=./charts/values.yaml --namespace ${APP_NAME}
  rules:
    - if: $CI_COMMIT_BRANCH == 'master'
      when: always
# https://www.arctiq.ca/our-blog/2021/1/18/cicd-pipelines-using-gitlab-ci-argo-cd-with-anthos-config-management/
deploy_dev:
  stage: deploy_prod
  environment:
    name: dev
  image: daniyalj/alpine-envsub:v1
  before_script:
     - git config --global user.email "[email protected]"
     - git config --global user.name "Mr GitLab"
     - mkdir -p ~/.ssh
     - echo "$GIT_SSH_PRIV_KEY" > ~/.ssh/id_rsa
     - chmod 400 ~/.ssh/id_rsa
     - ssh-keyscan gitlab.com >> ~/.ssh/known_hosts
  script:
    - git clone -b ${ENV_BRANCH} [email protected]:daniyalj/${ENV_GIT_REPO}.git
    - cd ${ENV_GIT_REPO}
    - git checkout -b ${CI_ENVIRONMENT_NAME}-${CI_PIPELINE_ID}
    - envsubst < pipeline-staging/spring-store.yaml > config-management/namespaces/argocd/spring-store-app-${CI_ENVIRONMENT_NAME}.yaml
    - git add . && git commit -m "deploy ${CI_ENVIRONMENT_NAME}-${CI_PIPELINE_ID}" && git push -o merge_request.label="Deploy" -o merge_request.create -o merge_request.target=${ENV_BRANCH} --force origin ${CI_ENVIRONMENT_NAME}-${CI_PIPELINE_ID}
  only:
  - master

Method 2 - GitLab Kubernetes Agent

References

@haminhcong haminhcong changed the title Continous Delivery application to K8S Cluster with GitLab Continous Delivery application to K8S Cluster with GitLab CI Jun 6, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant