I would like to integrate our Jenkins and Kubernetes clusters which works different servers.I have 2 cluster per stage
and production
. I already create a 2 name spaces on stage cluster to divide development
and stage
. I divide my values.yaml such as below.
So according to the GIT_BRANCH value; I would like to set namespace variable and deploy via helm install
command. At this circumstances,
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.
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}"
}
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
for example using your code above
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}"
"""
}
}
}
}
}