1
votes

I am trying to build and deploy nodejs app using gitlab ci/cd and kubernates cluster. the build pass successfully while the deployment failed. Meanwhile I added Kubernates cluster to gitlab (API url, CA certificate and service token) and the error that I got for running kubectl within the deploy due to issue related to KUBECONFIG and the below is gitlab-ci.yml that I am using

    stages:
      - build
      - deploy

    services:
      - docker:dind


    build_app:  
      stage: build
      image: docker:git
      only:
        - master
        - develop

      script:
        - docker login -u gitlab-ci-token -p ${CI_BUILD_TOKEN} ${CI_REGISTRY}
        - docker build -t ${CI_REGISTRY}/${CI_PROJECT_PATH} . 
        - docker tag ${CI_REGISTRY}/${CI_PROJECT_PATH} ${CI_REGISTRY}/${CI_PROJECT_PATH}:${CI_COMMIT_SHORT_SHA}
        - docker push ${CI_REGISTRY}/${CI_PROJECT_PATH}:${CI_COMMIT_SHORT_SHA}

    variables:
      DOCKER_HOST: tcp://docker:2375/
    deploy:
      stage: deploy
      image:
        name: bitnami/kubectl:latest
        entrypoint: [""]

      script:
        - USER_TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token)
        - CERTIFICATE_AUTHORITY_DATA=$(cat /var/run/secrets/kubernetes.io/serviceaccount/ca.crt | base64 -i -w0 -)
        - kubectl config set-cluster k8s --server="https://kubernetes.default.svc"
        - kubectl config set clusters.k8s.certificate-authority-data ${CERTIFICATE_AUTHORITY_DATA}
        - kubectl config set-credentials gitlab --token="${USER_TOKEN}"
        - kubectl config set-context default --cluster=k8s --user=gitlab
        - kubectl config use-context default
        - kubectl set image deployment test-flight web=${CI_REGISTRY}/${CI_PROJECT_PATH}:${CI_COMMIT_SHORT_SHA} -n test-flight-dev

$ USER_TOKEN=$(cat /var/run/secrets/kubernetes.io/serviceaccount/token) cat: /var/run/secrets/kubernetes.io/serviceaccount/token: No such file or directory

Update: Creating Environment and attach it to the stage solve the issue of identifying the cluster which the deployment will be, and so the cluster can get the action to apply the command

1
Hi minafarouk, welcome to SO. The whole purpose of adding a kubernetes cluster to GitLab is to get the work of configuring the cluster connectivity out of the repo's .gitlab-ci.yml; you have to pick one or the other -- either the job manages the k8s connectivity, or gitlab configures it for the job and by the time the script: runs, the KUBECONFIG already points to a legal file created by gitlab. So, which is it: you want to configure it, or you want gitlab to configure it for you? - mdaniel
Related to that, you are trying to access the Pod's injected ServiceAccountToken but the stage, itself, does not run in a Pod unless you have separately configured gitlab-runner to run in-cluster - mdaniel
Hi, what cluster infrastructure are you running Your cluster on? What k8s version do You have? - Piotr Malec
@mdaniel if I used gitlab configuration only and used the script only to push built image kubectl set image deployment test-flight ...etc I got the error error: Missing or incomplete configuration info. Please point to an existing, complete config file: 1. Via the command-line flag --kubeconfig 2. Via the KUBECONFIG environment variable 3. In your home directory as ~/.kube/config - minafarouk
@PiotrMalec DigitalOcean Kubernates cluster and the version is v1.13.4 - minafarouk

1 Answers

0
votes

Creating Environment and attach it to the stage solve the issue of identifying the cluster which the deployment will be, and so the cluster can get the action to apply the command environment: name: production