helm-charts icon indicating copy to clipboard operation
helm-charts copied to clipboard

Not able to use for loop for helm based application deployment in jenkins declarative pipeline

Open kavita1205 opened this issue 2 years ago • 0 comments

Describe the bug

I have created jenkins declarative pipeline, which is using helm for application deployment. I am using for loop in my declarative pipeline but it is not working.

I tried using both the approaches , a) mentioning loop inside scripts, b) using for loop outside script section but still my jenkins job is not able to deploy (not working).

Can someone help me here how can I use for loop when I need to deploy for multiple helm applications.

pipeline {
    agent {
        // Build global agent definition
            label 'DOCKER-LINUX'
    }   
    parameters {
        // Build paramaters
        string(defaultValue: 'jenkins-k8s-credentials',
               description: 'Enter the kubeconfig jenkins credentialID, this set the k8s cluster:', name: 'kubeconfigFile')
        string(defaultValue: 'penni-build-access-token',
               description: 'Enter the SCM jenkins credentialID', name: 'scmCredentials')
        string(defaultValue: '333d2bd2-5c8e-4ff7',
               description: 'Enter name of the helm private respository Jenkins credentials', name: 'helmCredentials')
        string(defaultValue: 'ML42_minion_Deployment',
               description: 'Enter the Jenkins credenitals id that contains the age key used to de-crypt the helm chart secrets. This needs to be the same key used to encrypted', name: 'ageKey')
        choice(choices: ['','lv-dev', 'lv-prod'], 
               description: 'Enter the NAMESPACE', name: 'NAMESPACE')
        choice(choices: ['','dev', 'prod'], 
               description: 'Enter the ENVIRONMENT', name: 'ENVIRONMENT')
        string(defaultValue: '1.44.1', 
               description: 'Enter the mincpu_file_to deploy', name: 'APP_VERSION')
        string(defaultValue: '02,03,04', 
               description: 'Enter the mincpu_file_to deploy in case of production deployment', name: 'MINCPU_FILE')
   }
    options {
        // Build options
        buildDiscarder(logRotator(numToKeepStr: '3'))
        disableConcurrentBuilds()
        timeout(time: 30, unit: 'MINUTES')
    }
     stage("Approval Production Stage") {
            agent {
                // Build stage agent definition
                dockerfile {
                    label 'DOCKER-LINUX'
                    args '-u root --dns 10.10.16.*** --dns 10.10.**.*** --dns-search corp.com'
                    reuseNode true
                }
            }
            // Check your branch is production
            when {
                expression { 
                    "${ENVIRONMENT}" == 'prod'
                } 
            }
            // Whole for approval
            steps {
                input message: 'Deploy to Prod?', submitter: 'tom@*****.com'
            }
        }
        stage("Deploy to Production for Minions") {
            agent {
                // Build stage agent definition
                dockerfile {
                    label 'DOCKER-LINUX'
                    args '-u root --dns 10.10.**** --dns 10.10.***** --dns-search corp.com'
                    reuseNode true
                }
            }
            when {
                expression { 
                    "${ENVIRONMENT}" == 'prod'
                } 
            }
            steps {

                // Add repo
                withCredentials([usernamePassword(credentialsId: "${params.helmCredentials}", passwordVariable: 'KEY', usernameVariable: 'USER'),
                                 file(credentialsId: "${params.ageKey}", variable: 'ageKey'),
                                 file(credentialsId: "${params.kubeconfigFile}", variable: 'KUBECONFIG')]) {
                    sh ('helm repo add ****-helm-repository https://artifacts.corp.com/artifactory/x****-helm-repository --username ${USER} --password ${KEY} --pass-credentials')
                    sh ('helm repo update')
                    
                    // Copy the Age key
                    sh ('cp \$ageKey /tmp/.config/sops/age/keys.txt')
                    
                    // create namespace
                    sh ('kubectl create namespace ml42-lv-\$ENVIRONMENT > /dev/null 2>&1 || true ')
             script {
                    env.CHARTV =  input message: 'What version do you want to deploy',
                                    parameters: [string(defaultValue: '',
                                    description: 'Enter the version of the helm chart you want to deploy',
                                    name: 'Helm Chart Version to Deploy')]

                  MINCPU_FILE.each(){

                     //decrypt secret key and deploy for mlcpu        
                    sh ('echo KUBECONFIG=\$KUBECONFIG helm secrets upgrade --install ml42cpuminions-\$MINCPU_FILE helm-repository/minions --set appVersion="${APP_VERSION}" --set docker.tag="{APP_VERSION}" -f minions/env/\$ENVIRONMENT/mlcpu/$MINCPU_FILE.yaml \
                        -f minions/env/\$ENVIRONMENT/values.yaml -f minions/secrets.yaml -f minions/env/\$ENVIRONMENT/secrets.yaml -n lv-\$ENVIRONMENT --version "${CHARTV}" --wait --timeout 20m0s --reuse-values ')

                      }              
                        
                    }
                }
            }
        }
       
    }
    post {
        always {
            cleanWs deleteDirs: true, notFailBuild: true
        }
    }  
}

Version of Helm and Kubernetes

- Helm: 
- Kubernetes:

Chart version

jenkins-2.346

What happened?

1.
2.
3.
...

What you expected to happen?

No response

How to reproduce it

No response

Anything else we need to know?

No response

kavita1205 avatar Nov 10 '22 13:11 kavita1205