<p>I have created context for local deployment.</p>

<pre><code>root@jenkins-linux-vm:/usr/lib# kubectl config get-contexts
CURRENT   NAME                          CLUSTER      AUTHINFO           NAMESPACE
          K8sCluster-CC-pre-release     kubernetes   kubernetes-admin   K8sCluster-CC-pre-release
          PR                            kubernetes   kubernetes-admin   PR
*         kubernetes-admin@kubernetes   kubernetes   kubernetes-admin
</code></pre>

<p>When my jenkins is running build i am getting below error, even though i have already created context and its pointing as well (*)for the same.</p>

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

<pre><code>tage('deployment') {
        container('kubectl') {
            withCredentials([kubeconfigFile(credentialsId: 'KUBERNETES_CLUSTER_CONFIG', variable: 'KUBECONFIG')]) {
            def kubectl
            echo 'Deployment Start'
              if(gitBranch == "future-master-fix") {
                 kubectl = "kubectl --kubeconfig=${KUBECONFIG} --context=K8sCluster-CC-pre-release"
                echo 'deploy to PRERELEASE!'
                sh "${kubectl} apply -f ./environment/pre-release -n=pre-release"
                echo 'Deployment End'
              }   
            }
        }
      }
</code></pre>

Gowthami Viswa
<p>When you configured <code>Jenkins</code> you probably used configuration based on <code>Kubeconfig</code> as is more popular. </p>

<p>Based on <a href="https://github.com/jenkinsci/kubernetes-cli-plugin" rel="nofollow noreferrer">Kubernetes CLI</a> </p>

<blockquote>
  <p>The plugin generates a kubeconfig file based on the parameters that were provided in the build. This file is stored in a temporary folder on the Jenkins executor and the exact path can be found in the KUBECONFIG environment variable. kubectl automatically picks up the path from this environment variable. Once the build is finished (or the pipeline block is exited), the temporary kubeconfig file is automatically removed.</p>
</blockquote>

<p>When you use <code>kubectl config get-contexts</code> you used your local config from <code>${HOME}/.kube/config</code>.</p>

<p>If you will check <code>kubectl config --help</code> you will get:</p>

<blockquote>
  <ol>
  <li>If the --kubeconfig flag is set, then only that file is loaded. The flag may only be set once
  and no merging takes place.</li>
  <li>If $KUBECONFIG environment variable is set, then it is used as a list of paths (normal path
  delimiting rules for your system). These paths are merged. When a value is modified, it is modified
  in the file that defines the stanza. When a value is created, it is created in the first file that
  exists. If no files in the chain exist, then it creates the last file in the list.</li>
  <li>Otherwise, ${HOME}/.kube/config is used and no merging takes place.</li>
  </ol>
</blockquote>

<p>It means that if you are using <code>Kubernetes</code> locally you are using option <strong>3</strong>. <br>
In <code>Jenkins</code> you are using option <strong>1</strong>, so each time you run <code>kubectl</code> with different config, which do not contain required <code>context</code>.</p>

<p>In this command: <code>kubectl = "kubectl --kubeconfig=${KUBECONFIG} --context=K8sCluster-CC-pre-release"</code> you are overwritting <code>kubeconfig</code>, so it cannot find this <code>context</code>.</p>

<p>Please remove <code>--kubeconfig=${KUBECONFIG}</code> from code and try again. It should work then.</p>
