Reputation: 621
I have a simple input file with two words in each line and parsing in Jenkinsfile (groovy), but seeing a issue,
pipeline {
agent any
stages {
stage('Test-1') {
steps {
echo 'Building..'
script {
for (x in 1..5) {
println x //0,1,2,3,4,5
}
}
}
}
stage('Test-2 ') {
steps {
script {
File file = new File("my_file.txt")
def line, noOfLines = 0;
file.withReader { reader ->
while ((line = reader.readLine()) != null) {
println "${line}"
noOfLines++
}
}
}
}
}
stage('Test-3') {
steps {
script {
def whole_file_data = readFile(file: 'my_file.txt')
whole_file_data.eachLine { String line ->
def (p1, p2) = line.tokenize()
println("line:${line}")
println("p1:${p1} & p2:${p2}")
}
}
}
}
}
}
The Test-1 works fine for testing, The Test-2 gives me error as
Scripts not permitted to use new java.io.File java.lang.String. Administrators can decide whether to approve or reject this signature.
The Test-3 stage prints ONLY FIRST Line and gives Warning as,
[Pipeline] readFile
expected to call java.lang.String.eachLine but wound up catching org.jenkinsci.plugins.workflow.cps.CpsClosure2.call; see: https://jenkins.io/redirect/pipeline-cps-method-mismatches/
line:1111 one
[Pipeline] echo
My Jenkins version is Jenkins 2.249.1 What is causing issue? p1:1111 & p2:One
Upvotes: 2
Views: 4405
Reputation: 28774
Test 1:
This is a shell script iterator. I am unsure how it relates to the other two stages, but it is fine as is.
Test 2:
This uses the File
class in a Pipeline to read a file on the filesystem. This has both security issues requiring admin approval pending sandbox mode enablement, and also only works on the Jenkins master given the usage in the question. Indeed, you encounter the security issue, and this usage should be avoided for all of these reasons.
Test 3:
This uses the readFile
pipeline step method in the Pipeline. This is best practices, and should be greatly preferred over the usage in Test 2. The String content in the file is iterated, and the line is printed with correct usage. The problem is with the line:
def (p1, p2) = line.tokenize()
where the Tuple (p1, p2)
is assigned to the Array return from tokenize
(and implicitly recast). This lambda iterator scope tuple is causing a Cps
issue with your pipeline as you can see in the error. You can fix this either by re-assigning to null
at the end of your script
block:
p1 = null
p2 = null
or by not even using the temporary variables in the first place:
println("p1:${line.tokenize()[0]} & p2:${line.tokenize()[1]}")
However, a safer alternative to both of these would probably be to iterate over the tokenized line instead, and print it without newline characters to ensure expected formatting.
Upvotes: 1