-
Bug
-
Resolution: Unresolved
-
Minor
-
None
As with the eachLine-method on String only works for the first iteration.JENKINS-26481
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 { ... }
[JENKINS-46988] String.eachLine only reads first line
Description |
Original:
As with Using version 3.39 We use a cleanup-pipeline to find all running docker-containers without a corresponding feature-branch (deleted after merge). {code:java} 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}") } } }{code} |
New:
As with - Using version 3.39 We use a cleanup-pipeline to find all running docker-containers without a corresponding feature-branch (deleted after merge). {code:java} 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}") } } }{code} As a workaround replace _eachLine_ with _split('\n').each \{ ... }_ |