<p>i have a Deploy stage in jenkins like below. </p>

<pre><code> 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")

               }
            }
        }
    } 
</code></pre>

<p>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?</p>

<p>What can be the issue there ? </p>

<p>P.S I can run it inside docker manually.</p>

<p>I was running into such an issue recently and this helped:
<a href="https://github.com/kubernetes/kubernetes/issues/23726" rel="nofollow noreferrer">GitHub Issue on kubectl</a></p>

<p>It is basically failing because of a wrong kubeconfig.
Please check that you have no old <code>~/.kube/config</code>in your filesystem. </p>

<p>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:</p>

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