Unable to extract elements from a ArrayList and assign to a variable

+1 vote

I am writing a declarative pipeline Jenkinsfile as shown below:

pipeline  {   

agent any   

    stages  { 

         stage('Clone sources')  {

             steps  {

                script {
 
                   //def filename = "ADVPS-1_ARBtoken.yaml"    

                   def filelist = getChangedFilesList() // List of filenames that have changed

                   echo "${filelist}"

                    //need to write a logic here to fetch the filename from the filelist output [ADVPS-1_ARBtoken.yaml]

                  //def filename = filelist.replaceAll("[\\[\\]]", "") as String[]

                  //sh 'filename=$(echo "${filelist}" | sed -e 's/\[//' -e 's/\]//')'

                  echo "${filename}"

                  env.StoryId = filename.split("_")[0]     // JiraId will store the value that you could use at various stages  

                  echo "${env.StoryId}"

                  }

             }

        }

    }

  }

}


@NonCPS

def getChangedFilesList() {


       changedFiles = []

       for (changeLogSet in currentBuild.changeSets) { 

               for (entry in changeLogSet.getItems()) { // for each commit in the detected changes

                      for (file in entry.getAffectedFiles()) {

                             changedFiles.add(file.getPath()) // add changed file to list

                      }

               }

       }

       return changedFiles


}

Here, I have written a function viz., getChangedFilesList() that will return the ArrayList containing a list of files that have been changed per commit. Since, the returned value is an ArrayList, the variable ‘fileList’ returns in the list syntax i.e., [JenkinsFile] if JenkinsFile was changed for that commit. Now, I am trying to use an another variable ‘filename’ which I would ideally want to store the value JenkinsFile without Square brackets. I tried certain solutions (I have commented the same) but it does not work. Can you please help as to how this can be achieved?

Sep 2, 2019 in Jenkins by anonymous
• 360 points
242 views

1 answer to this question.

+2 votes

I got the solution for this:

pipeline  {   

agent any   

    stages  { 

         stage('Clone sources')  {

             steps  {

                script {
   
                   def filelist = getChangedFilesList() // List of filenames that have changed

                   def filename = filelist.find{item->item.contains("yaml")} //Returns the list of files having the yaml file extension from the filelist ArrayList

                   echo "${filename}" //<filename>.yaml

                   env.StoryId = filename.split("_")[0]     // JiraId will store the value that is used at various stages  

                   echo "${env.StoryId}"

                  }

             }

        }

    }

  }

}


@NonCPS

def getChangedFilesList() {


       changedFiles = []

       for (changeLogSet in currentBuild.changeSets) { 

               for (entry in changeLogSet.getItems()) { // for each commit in the detected changes

                      for (file in entry.getAffectedFiles()) {

                             changedFiles.add(file.getPath()) // add changed file to list

                      }

               }

       }

       return changedFiles


}
answered Sep 5, 2019 by Abhilash
• 360 points
Indeed the method of  retrieving the filename with yaml extension was a smart move. Great job @Abhilash!
Thanks Sirajul! And also thanks for your guidance for my previous query as well.

Related Questions In Jenkins

+3 votes
1 answer

How to assign a groovy variable to a shell variable?

I have tried the following in my ...READ MORE

answered Dec 3, 2019 in Jenkins by Raveendiran
• 860 points
10,561 views
+4 votes
7 answers

How to set the pipeline name and description for a Jenkinsfile?

You can rename pipeline jobs in jenkins ...READ MORE

answered Oct 11, 2018 in Jenkins by Hannah
• 18,030 points
12,559 views
+2 votes
5 answers
+1 vote
1 answer

How to extract a substring of a string in a declarative pipeline code?

This would have been more easier using ...READ MORE

answered Aug 16, 2019 in Jenkins by Sirajul
• 50,480 points
3,988 views
+13 votes
2 answers

Git management technique when there are multiple customers and need multiple customization?

Consider this - In 'extended' Git-Flow, (Git-Multi-Flow, ...READ MORE

answered Mar 26, 2018 in DevOps & Agile by DragonLord999
• 8,400 points
423 views
0 votes
1 answer