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}"
}
How Does Helm Work? Helm and Kubernetes work like a client/server application. The Helm client pushes resources to the Kubernetes cluster. The server-side depends on the version: Helm 2 uses Tiller while Helm 3 got rid of Tiller and entirely relies on the Kubernetes API.
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}"
"""
}
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With