<p>I would like to integrate our Jenkins and Kubernetes clusters which works different servers.I have 2 cluster per <code>stage</code> and <code>production</code>. I already create a 2 name spaces on stage cluster to divide <code>development</code> and <code>stage</code>. I divide my values.yaml such as below. </p>

<ul>
<li>valeus.dev.yaml</li>
<li>values.stage.yaml</li>
<li>values.prod.yaml</li>
</ul>

<p>So according to the GIT_BRANCH value; I would like to set namespace variable and deploy via <code>helm install</code> command. At this circumstances,</p>

<p>My question is, what is the best way to connect 2 cluster in Jenkinsfile for this condition cause for dev and test namespace I need to one cluster , for production I need to deploy another cluster.</p>

<pre><code> stage('deploy') {
   steps {
      script {
        if (env.GIT_BRANCH == "origin/master") {
            def namepsace="dev"
            sh "helm upgrade --install -f values.dev.yaml --namespace ${namespace}"
        } else if (env.GIT_BRANCH =="origin/test"){
            def namepsace="stage"
            sh "helm upgrade --install -f values.stage.yaml --namespace ${namespace}"

        } else { 
            def namepsace="prod"
            sh "helm upgrade --install -f values.prod.yaml --namespace ${namespace}"
        }
</code></pre>

semural
<p>you will need to create the Jenkins secrets to add both kubeconfig files for your k8s Clusters, and in the if statement you load the kubeconfig for your environment</p>

<p>for example using your code above</p>

<pre><code>stage('deploy') {
  steps {
    script {
      if (env.GIT_BRANCH == "origin/master") {
        def namepsace="dev"
        withCredentials([file(credentialsId: 'kubeconfig-dev', variable: 'config')]) {
          sh """
          export KUBECONFIG=\${config}
          helm upgrade --install -f values.dev.yaml --namespace ${namespace}"
          """
        }
      } else if (env.GIT_BRANCH =="origin/test"){
        def namepsace="stage"
        withCredentials([file(credentialsId: 'kubeconfig-stage', variable: 'config')]) {
          sh """
          export KUBECONFIG=\${config}
          helm upgrade --install -f values.dev.yaml --namespace ${namespace}"
          """
        }
      } else {
        def namepsace="prod"
        withCredentials([file(credentialsId: 'kubeconfig-prod', variable: 'config')]) {
          sh """
          export KUBECONFIG=\${config}
          helm upgrade --install -f values.dev.yaml --namespace ${namespace}"
          """
        }
      }
    }
  }
}
</code></pre>
