0
votes

I have Jenkins pipeline declarative code where i am getting error. Please check following pipeline declarative code and help. Error which i am getting pasting on following lines. I am working on End to End Automation for build, test, Deploy. I am writing pipeline groovy DSL code to do the job. Following steps involves.

1- checkout bitbucket branch

2- Deploy source code

3- If deployment will success i want to raised PR on bitbucket from source branch to merge into master.

4- Lastly when PR will merge final deployment will trigger to higher env.

 pipeline {      
   agent {label "dockercon2"}
   stages {                
        stage('checkout_stage'){
      steps {
         sh "id && whoami"
          checkout([$class: 'GitSCM', 
            branches: [[name: "*/${env.SOURCE_BRANCH}"]], 
              doGenerateSubmoduleConfigurations: false, 
                extensions: [[$class: 'CleanBeforeCheckout'], 
                  [$class: 'PerBuildTag'], 
                     [$class: 'IgnoreNotifyCommit'], 
                       [$class: 'PreBuildMerge', 
                          options: [mergeRemote: 'origin', mergeTarget: "${gitTargetbranch()}"]]], 
                            submoduleCfg: [], 
                               userRemoteConfigs: [[credentialsId: 'https_gitlab_clone_credential', name: 'origin', refspec: '+refs/heads/testing_pipeline:refs/remotes/origin/testing_pipeline', url: "${gitUrl()}"]]])
                    git branch: "${gitSourcebranch()}", credentialsId: 'https_gitlab_clone_credential', url: "${gitUrl()}"
                    withCredentials([usernamePassword(credentialsId: 'https_gitlab_clone_credential', passwordVariable: 'bb_pass', usernameVariable: 'bb_user')]){                        
                  sh "figlet 'wazzup'"
                  sh "figlet 'i am on'"
                  sh "git config credential.username ${UserName()}"
                  sh "git config credential.helper ${Gitlab_Pass()}" 
                  //sh "git config --global user.email ${UserEmail()}"
                  sh "git config --global user.name ${UserName()}"
                  //sh "set +x /bin/sh -c \"echo ${ansibleVaultCredentials()}>vault_passwd.txt\""
                  sh "set +x; echo ${ansibleVaultCredentials()} >> vault_passwd.txt"
                  sh "echo pass"
                  sh "cat vault_passwd.txt"
                  sh "ansible-vault decrypt --vault-password-file vault_passwd.txt test.sh && sh test.sh"   
               }// closing withCredential
        }// steps of checkout_stage

stage('creating_PR') {

   steps {
       when {
            step (currentBuild.result == "SUCCESS")  }

              triggers{
               bitbucketServer(bitbucketpr(projectPath: "${gitUrl()}",
                cron:'H/15 * * * *',
                credentialsId:'bitbucket_glone_cred',
                username:"${bb_user}",
                password:"${bb_pass}",
                repositoryOwner:'Atul',
                repositoryName:'valutstorage',
                branchesFilter:"${gitSourcebranch()}",
                branchesFilterBySCMIncludes:false,
                ciKey:'jenkins',
                ciName:'jenkins',
                ciSkipPhrases:'',
                checkDestinationCommit:false,
                approveIfSuccess:false,
                cancelOutdatedJobs:true,
                commentTrigger:''))
             }
         }
       }
     }
    } // Main stages block closer
   } // Pipeline closer


 ----------------Error i am getting ------------


 org.codehaus.groovy.control.MultipleCompilationErrorsException: 

 startup failed:

WorkflowScript: 49: Unknown stage section "stage". Starting with 

version 0.5, steps in a stage must be in a ‘steps’ block. @ line 49, 

column 5.

  stage('checkoutStage'){
   ^

1 error

at 

org.codehaus.groovy.control.ErrorCollector.failIfErrors(ErrorCollector.java:310)

1

1 Answers

0
votes

It's probably be caused by mismatched parentheses. You do not close the "checkout_stage" block correctly. Add a closing bracket before stage('creating_PR') and remove the last closing bracket ("Pipeline closer"). That should fix your pipeline structure.

pipeline {      
   agent { label "dockercon2"}
   stages {                
        stage('checkout_stage'){
            steps {
                sh "id && whoami"
                checkout([$class: 'GitSCM', 
                    branches: [[name: "*/${env.SOURCE_BRANCH}"]], 
                    doGenerateSubmoduleConfigurations: false, 
                        extensions: [[$class: 'CleanBeforeCheckout'], 
                        [$class: 'PerBuildTag'], 
                            [$class: 'IgnoreNotifyCommit'], 
                            [$class: 'PreBuildMerge', 
                                options: [mergeRemote: 'origin', mergeTarget: "${gitTargetbranch()}"]]], 
                                    submoduleCfg: [], 
                                    userRemoteConfigs: [[credentialsId: 'https_gitlab_clone_credential', name: 'origin', refspec: '+refs/heads/testing_pipeline:refs/remotes/origin/testing_pipeline', url: "${gitUrl()}"]]])
                    git branch: "${gitSourcebranch()}", credentialsId: 'https_gitlab_clone_credential', url: "${gitUrl()}"
                    withCredentials([usernamePassword(credentialsId: 'https_gitlab_clone_credential', passwordVariable: 'bb_pass', usernameVariable: 'bb_user')])
                    {                        
                        sh "figlet 'wazzup'"
                        sh "figlet 'i am on'"
                        sh "git config credential.username ${UserName()}"
                        sh "git config credential.helper ${Gitlab_Pass()}" 
                        //sh "git config --global user.email ${UserEmail()}"
                        sh "git config --global user.name ${UserName()}"
                        //sh "set +x /bin/sh -c \"echo ${ansibleVaultCredentials()}>vault_passwd.txt\""
                        sh "set +x; echo ${ansibleVaultCredentials()} >> vault_passwd.txt"
                        sh "echo pass"
                        sh "cat vault_passwd.txt"
                        sh "ansible-vault decrypt --vault-password-file vault_passwd.txt test.sh && sh test.sh"   
                    }// closing withCredential
            }// steps of checkout_stage
        }// EDITED: Closing the checkout_stage block

        stage('creating_PR') {
            steps {
                when {
                    step (currentBuild.result == "SUCCESS")  
                }
                triggers {
                bitbucketServer(bitbucketpr(projectPath: "${gitUrl()}",
                    cron:'H/15 * * * *',
                    credentialsId:'bitbucket_glone_cred',
                    username:"${bb_user}",
                    password:"${bb_pass}",
                    repositoryOwner:'Atul',
                    repositoryName:'valutstorage',
                    branchesFilter:"${gitSourcebranch()}",
                    branchesFilterBySCMIncludes:false,
                    ciKey:'jenkins',
                    ciName:'jenkins',
                    ciSkipPhrases:'',
                    checkDestinationCommit:false,
                    approveIfSuccess:false,
                    cancelOutdatedJobs:true,
                    commentTrigger:''))
                }
            }
        }
    } // Main stages block closer
} // Pipeline closer