Uploaded image for project: 'Jenkins'
  1. Jenkins
  2. JENKINS-46988

String.eachLine only reads first line

    Details

    • Similar Issues:

      Description

      As with JENKINS-26481 the eachLine-method on String only works for the first iteration.

      Using version 3.39

      We use a cleanup-pipeline to find all running docker-containers without a corresponding feature-branch (deleted after merge).

      stage('clean') {
       def branches = []
       // extract available branches from git
       sh (returnStdout: true, script: "ssh-agent bash -c 'ssh-add /var/lib/jenkins/.ssh/id_rsa &>/dev/null; git ls-remote --heads --refs ssh://git@myrepo/project.git' | cut -f 2")
        .eachLine { branches << it }
       // extract all containers (including stopped)
       def containers = []
       sh (returnStdout: true, script: "docker ps -a --format '{{.Names}}' --filter name=project")
        .trim()
        .eachLine { containers << it }
       println(containers) // <---- only prints first container
       println(branches) // <---- only prints first branch
      
       //stop containers for non existing branches...
       containers.each{ containername ->
        if(branches.findAll({branch -> branch.contains(containername)}).isEmpty()){
         println("trying to stop ${containername}")
         //sh ("docker stop ${containername} || true") // container might already be stopped
         println("removing ${containername}")
         //sh ("docker rm ${containername}")
        }
       }
      }

      As a workaround replace eachLine with split('\n').each { ... }

        Attachments

          Activity

          Hide
          rakeshnagarajan Rakesh Nagarajan added a comment -

          Workaround is working really fine

          As a workaround replace eachLine with split('\n').each { ... }

           

          Show
          rakeshnagarajan Rakesh Nagarajan added a comment - Workaround is working really fine As a workaround replace eachLine with split('\n').each { ... }  
          Hide
          jorhett Jo Rhett added a comment - - edited

          I don't feel that this issue is Minor, nor do I feel that the workaround is an acceptable solution. This basically means that every Jenkins user worldwide will burn some hours diagnosing this problem only to end up here.

          That this bug is more than 2 years old speaks to a significant lack of care.

          Show
          jorhett Jo Rhett added a comment - - edited I don't feel that this issue is Minor, nor do I feel that the workaround is an acceptable solution. This basically means that every Jenkins user worldwide will burn some hours diagnosing this problem only to end up here. That this bug is more than 2 years old speaks to a significant lack of care.
          Hide
          euphxenos Andrew Lawrence added a comment -

          I'd like to affirm the previous comment.  I just did exactly that – I spent a couple of hours trying to figure out why my code wasn't working, and ended up here when I realized it was a bug in the Jenkins groovy interpreter for scripted pipelines.

          Show
          euphxenos Andrew Lawrence added a comment - I'd like to affirm the previous comment.  I just did exactly that – I spent a couple of hours trying to figure out why my code wasn't working, and ended up here when I realized it was a bug in the Jenkins groovy interpreter for scripted pipelines.
          Hide
          flash1212 Dominique Thornton added a comment -

          Likewise. Just burned a bit of time on this. Moving to the workaround.

          Show
          flash1212 Dominique Thornton added a comment - Likewise. Just burned a bit of time on this. Moving to the workaround.
          Hide
          pkozuchowski Piotr Kożuchowski added a comment -

          I've just stumbled over this as well.

          Show
          pkozuchowski Piotr Kożuchowski added a comment - I've just stumbled over this as well.

            People

            • Assignee:
              Unassigned
              Reporter:
              lostiniceland Marc Schlegel
            • Votes:
              26 Vote for this issue
              Watchers:
              20 Start watching this issue

              Dates

              • Created:
                Updated: