Jenkins deployment to K8 cluster

1/7/2020

I'm trying to deploy my docker image into kubernetes cluster in first time, i have two nodes master and worker nodes both are up and running status.

I have created namesapace pre-release environment to deploy my changes on it, during jenkins build, i am getting below error.

+ kubectl '--kubeconfig=****' '--context=K8sCluster-CC-pre-release' apply -f ./environment/pre-release '-n=pre-release'
error: context "K8sCluster-CC-pre-release" does not exist

Jenkins stage

stage('deployment') {
        container('kubectl') {

            withCredentials([kubeconfigFile(credentialsId: 'KUBERNETES_CLUSTER_CONFIG', variable: 'KUBECONFIG')]) {
            def kubectl
                if(gitBranch == "future-master-fix") {
                kubectl = "kubectl --kubeconfig=${KUBECONFIG} --context=K8sCluster-CC-pre-release"
                sh "${kubectl} apply -f ./environment/pre-release -n=pre-release"
                }   
            }
        }
      }
kubectl config set-context K8sCluster-CC-pre-release --namespace=K8sCluster-CC-pre-release --user=kubernetes-admin --cluster=kubernetes
Context "K8sCluster-CC-pre-release" created.
CURRENT   NAME                          CLUSTER      AUTHINFO           NAMESPACE
*         K8sCluster-CC-pre-release     kubernetes   kubernetes-admin   K8sCluster-CC-pre-release

can you please someone help me on this ?

-- Anonymuss
jenkins
kubernetes

1 Answer

1/7/2020

The reason is K8sCluster-CC-pre-release context does not exist in your kubeconfig file. You can run kubectl config view -o jsonpath='{.current-context}' to check current context and use that context.

-- Arghya Sadhu
Source: StackOverflow