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 in Jenkins by anonymous
• 320 points
40 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 by Abhilash
• 320 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

+2 votes
0 answers

How to assign a groovy variable to a shell variable?

I have the following code snippet in ...READ MORE

Sep 11 in Jenkins by Abhilash
• 320 points
328 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
• 15,620 points
7,538 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 in Jenkins by Sirajul
• 31,180 points
252 views
+5 votes
4 answers

How can I run a Jenkinsfile from a particular folder?

Consider the below points: The Freestyle project ...READ MORE

answered Mar 26, 2018 in Jenkins by anonymous
3,652 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,380 points
181 views
0 votes
1 answer