Search code examples
groovyjenkins-pipeline

Getting "unexpected token: String" in Jenkins pipeline


I am trying to upload package(zip file) by downloading from my instance and uploading to artifactory, but when I run the jenkins job for the same, I get below error :

*WorkflowScript: 32: unexpected token: String @ line 32, column 34. for(String package : packages) {*

Below is my code:

pipeline {
     agent none

parameters {
    string(name: 'PACKAGE_NAME', defaultValue: '', description: 'please provide aem package name')
}

environment {
    ARTIFACTORY_CREDENTIALS = credentials('artifactory')
    JIRA_JENKINS = credentials('ops-jira')
}

stages {
    stage('upload package') {
        agent {
         label 'ec2'
        }
        
        steps {
            script {
                withCredentials([usernamePassword(credentialsId: '<my cred id>', passwordVariable: 'admin_pw', usernameVariable: 'admin_user')]) {
                    try {
                        def packages = ''
                        if(PACKAGE_NAME.contains(",")) {
                            echo "Package contains multiple files"
                            packages = PACKAGE_NAME.split(',')
                        }else {
                            echo "Package contains single file"
                            packages = PACKAGE_NAME
                        }
                         for(String package : packages) {
                            echo "Package name - ${package}"
                            
                            sh "curl -O -v -k -u ${admin_user}:${admin_pw} https://<myinstancename>/etc/packages/my_packages/${package} > ${package}_downloaded_logs"
                            archiveArtifacts artifacts: '*.zip', followSymlinks: false
                            sh "curl -X PUT -u '${ARTIFACTORY_CREDENTIALS_USR}":"${ARTIFACTORY_CREDENTIALS_PSW}' -T ${package} 'https://<myartifactoryurl>/${package}/${BUILD_NUMBER}/'"
                            
                            echo "[MNR] - Deployment to Package completed"
                            
                        }
                    }catch(Exception e) {
                        echo "Error in deployment - ${e}"
                        currentBuild.result = 'STABLE'
                    }
                }
            }
        }
    }
 }

}

Please help


Solution

  • package is a reserved word in Groovy. You should rename your variable something else in the for loop.

    Source: https://docs.groovy-lang.org/latest/html/documentation/#_keywords