title: Jenkins pipeline - How to iterate through a list
body: <p>I'm required to read values from a file in my pipeline.  I'm using <strong>split()</strong> which puts them into an <em>Array</em>.  I need to put them into an <em>Arraylist</em> so I'm using <strong>Arrays.asList()</strong>. The problem I'm having is I'm unable to use the <strong>size()</strong> or <strong>length()</strong> methods so I cannot make a for loop such as</p>
  
  <pre><code>for (ii = 0; ii &lt; var.length; ii++)
  </code></pre>
  
  <p>or</p>
  
  <pre><code>for (ii = 0; ii &lt; var.size; ii++)
  </code></pre>
  
  <p>because I get error: <strong>unclassified field java.util.Arrays$ArrayList length</strong></p>
  
  <p>So I tried to use a for each loop, but when I take some action (like ls command for example) in my finally block it only iterates 1 time.  But if I just run the command 'echo' it iterates for each element like it's supposed to.  Any advice on how to modify my code to get it to iterate for each element in the list when using any command?</p>
  
  <p>Works correctly....</p>
  
  <pre><code>node{
      wrap([$class: 'ConfigFileBuildWrapper', managedFiles: [[fileId: 'dest_hosts.txt', targetLocation: '', variable: 'DEST_HOST']]]) {
          HOST = Arrays.asList(readFile(env.DEST_HOST).split("\\r?\\n"))
          deploy(HOST)
      }
  }
  
  @NonCPS
  def deploy(host){
      for (String target : host){
          try {
              echo target
          }
          finally {
             echo target
          }
      }
  }
  </code></pre>
  
  <p>OUTPUT (iterates for each element):</p>
  
  <pre><code>[Pipeline] node
  Running on &lt;obfuscated&gt;
  [Pipeline] {
  [Pipeline] wrap
  provisoning config files...
  copy managed file [&lt;obfuscated&gt;] to file:/var/lib/jenkins/&lt;obfuscated&gt;
  [Pipeline] {
  [Pipeline] readFile
  [Pipeline] echo
  www.testhost.com
  [Pipeline] echo
  www.testhost.com
  [Pipeline] echo
  www.testhost2.com
  [Pipeline] echo
  www.testhost2.com
  [Pipeline] }
  Deleting 1 temporary files
  [Pipeline] // wrap
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  Finished: SUCCESS
  </code></pre>
  
  <p>But if I take some action such as <strong>'ls -l'</strong> it only iterates 1 time</p>
  
  <pre><code>node{
      wrap([$class: 'ConfigFileBuildWrapper', managedFiles: [[fileId: 'dest_hosts.txt', targetLocation: '', variable: 'DEST_HOST']]]) {
          HOST = Arrays.asList(readFile(env.DEST_HOST).split("\\r?\\n"))
          deploy(HOST)
      }
  }
  
  @NonCPS
  def deploy(host){
      for (String target : host){
          try {
              echo target
          }
          finally {
             sh 'ls -l'
          }
      }
  }
  </code></pre>
  
  <p>OUTPUT (only iterates 1 time):</p>
  
  <pre><code>[Pipeline] node
  Running on &lt;obfuscated&gt;
  [Pipeline] {
  [Pipeline] wrap
  provisoning config files...
  copy managed file [&lt;obfuscated&gt;] to file:/var/lib/jenkins/&lt;obfuscated&gt;
  [Pipeline] {
  [Pipeline] readFile
  [Pipeline] echo
  www.testhost.com
  [Pipeline] sh
  [sandbox%2Fpipeline-test-new1] Running shell script
  + ls -l
  total 8
  -rw-r--r-- 1 jenkins jenkins 10 Jun 17 16:07 someFile
  [Pipeline] }
  Deleting 1 temporary files
  [Pipeline] // wrap
  [Pipeline] }
  [Pipeline] // node
  [Pipeline] End of Pipeline
  Finished: SUCCESS
  </code></pre>