2
votes

i have a Deploy stage in jenkins like below.

 stage('Deploy service to K8S') {
        steps {
              withDockerContainer(image: "gcr.io/google.com/cloudsdktool/cloud-sdk", toolName: 'latest'){
                withCredentials([file(credentialsId: 'jenkins_secret', variable: 'GC_KEY')]) {
                  sh("HOME=$WORKSPACE gcloud --quiet auth activate-service-account --key-file=${GC_KEY}")
                  sh("HOME=$WORKSPACE gcloud container clusters get-credentials test --zone us-central1-c --project ${PROJECT_ID}")
                  sh("kubectl get pods")

               }
            }
        }
    } 

Despite of Jenkins logs show me that autentication was succedd. But kubectl get pods commands fail with this error : The connection to the server localhost:8080 was refused - did you specify the right host or port?

What can be the issue there ?

P.S I can run it inside docker manually.

1
Ok, i added HOME=$WORKSPACE in front of "kubetl command" and now it works. But i wonder why jenkins cant generate kubeconfig file inside container.Amir Damirov
The problem here is that kubectl is looking in the wrong location for your kubeconfig. You could most easily solve this with an env map, but there are multiple ways in general to solve this.Matt Schuchard

1 Answers

1
votes

I was running into such an issue recently and this helped: GitHub Issue on kubectl

It is basically failing because of a wrong kubeconfig. Please check that you have no old ~/.kube/configin your filesystem.

If this does also not work please try to give the kube api server explicitely as it seems to try to connect to localhost instead of to your kubectl api server:

kubectl config set-cluster some-cluster --server=http://master.example.com:8080