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
• 380 points
10,822 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
• 380 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

+5 votes
2 answers

How to assign a groovy variable to a shell variable?

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

answered Dec 4, 2019 in Jenkins by Raveendiran
• 980 points
89,937 views
0 votes
1 answer
+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,520 points
36,384 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
• 59,230 points
47,769 views
+15 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 27, 2018 in DevOps & Agile by DragonLord999
• 8,450 points
4,041 views
+2 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP