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?