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

Setting default directory when using container()

    • Icon: Bug Bug
    • Resolution: Unresolved
    • Icon: Minor Minor
    • kubernetes-plugin
    • None

      We use the kubernetes plugin with enterprise jenkins. As soon as developers refer to one of their containers from their pod template, the first thing they usually want to do is to cd into a certain directory. What's the best way to do this in order to avoid making them do a "cd" over and over again in their sh'es? The dir() function does not work unfortunately (perhaps it only works in the jenkins jnlp container?)

       

      Ideally we could do something like this:

       

      container(name: 'mycontainer', dir: "/home/app/foo") {
        ... 
      }
      

       

      or...

      container(name: 'mycontainer') {
        dir("/home/app/foo") {
          ...   
        }
      }
      

       

          [JENKINS-60396] Setting default directory when using container()

          The problem is that the temporary files are created in the default container. This is clearly visible with such a pipeline

          podTemplate(cloud: 'kubernetes', yaml: someYamlDefiningASecondContainer) {
          	node(POD_LABEL) {
          		sh 'echo "/tmp in default container:" && ls -al /tmp'
          		
          		container('second-container') {
          			try {
          				dir('/tmp/foo') {
          					sh 'echo ". in container(second-container)/dir(tmp/foo):" && ls -al .'
          				}
          			}
          			catch (e) {
          				echo "-> exception: $e"
          			}
          			sh 'echo "/tmp in container(second-container):" && ls -al /tmp'
          		}
          		sh 'echo "/tmp in default container:" && ls -al /tmp'
          	}
          }
          

          The output shows the /tmp/foo@tmp dir created in the default container rather than in the secondOne.

          + echo /tmp in default container:
          /tmp in default container:
          + ls -al /tmp
          total 16
          drwxrwxrwt 1 root root 4096 Jul 19 16:13 .
          drwxr-xr-x 1 root root 4096 Jul 19 16:13 ..
          drwxr-xr-x 2 jenkins jenkins 4096 Jul 19 16:13 hsperfdata_jenkins
          drwxr-xr-x 1 root root 4096 Jul 4 09:27 hsperfdata_root
          [Pipeline] container
          [Pipeline] {
          [Pipeline] dir
          Running in /tmp/foo
          [Pipeline] {
          [Pipeline] sh
          process apparently never started in /tmp/foo@tmp/durable-44033be5
          (running Jenkins temporarily with -Dorg.jenkinsci.plugins.durabletask.BourneShellScript.LAUNCH_DIAGNOSTICS=true might make the problem clearer)
          [Pipeline] }
          [Pipeline] // dir
          [Pipeline] echo
          -> exception: hudson.AbortException: script returned exit code -2
          [Pipeline] sh
          + echo /tmp in container(second-container):
          /tmp in container(second-container):
          + ls -al /tmp
          total 16
          drwxrwxrwt 1 root root 4096 Jul 19 04:35 .
          drwxr-xr-x 1 root root 4096 Jul 19 16:13 ..
          drwxr-xr-x 2 jenkins jenkins 4096 Jul 19 04:35 hsperfdata_jenkins
          drwxr-xr-x 1 root root 4096 Jul 4 09:27 hsperfdata_root
          [Pipeline] }
          [Pipeline] // container
          [Pipeline] sh
          + echo /tmp in default container:
          /tmp in default container:
          + ls -al /tmp
          total 24
          drwxrwxrwt 1 root root 4096 Jul 19 16:13 .
          drwxr-xr-x 1 root root 4096 Jul 19 16:13 ..
          drwxr-xr-x 2 jenkins jenkins 4096 Jul 19 16:13 foo
          drwxr-xr-x 2 jenkins jenkins 4096 Jul 19 16:18 foo@tmp
          drwxr-xr-x 2 jenkins jenkins 4096 Jul 19 16:13 hsperfdata_jenkins
          drwxr-xr-x 1 root root 4096 Jul 4 09:27 hsperfdata_root
          

          Looking a bit at the sources, it seems to me that the cause of the problem is that hudson.FilePath used in org.jenkinsci.plugins.workflow.steps.PushdStep holds information about the agent but not about the container causing org.jenkinsci.plugins.durabletask.BourneShellScript to create its temporary files at the wrong place and consequently not able to execute the script.

          Marc Guillemot added a comment - The problem is that the temporary files are created in the default container. This is clearly visible with such a pipeline podTemplate(cloud: 'kubernetes' , yaml: someYamlDefiningASecondContainer) { node(POD_LABEL) { sh 'echo "/tmp in default container:" && ls -al /tmp' container( 'second-container' ) { try { dir( '/tmp/foo' ) { sh 'echo ". in container(second-container)/dir(tmp/foo):" && ls -al .' } } catch (e) { echo "-> exception: $e" } sh 'echo "/tmp in container(second-container):" && ls -al /tmp' } sh 'echo "/tmp in default container:" && ls -al /tmp' } } The output shows the /tmp/foo@tmp dir created in the default container rather than in the secondOne . + echo /tmp in default container: /tmp in default container: + ls -al /tmp total 16 drwxrwxrwt 1 root root 4096 Jul 19 16:13 . drwxr-xr-x 1 root root 4096 Jul 19 16:13 .. drwxr-xr-x 2 jenkins jenkins 4096 Jul 19 16:13 hsperfdata_jenkins drwxr-xr-x 1 root root 4096 Jul 4 09:27 hsperfdata_root [Pipeline] container [Pipeline] { [Pipeline] dir Running in /tmp/foo [Pipeline] { [Pipeline] sh process apparently never started in /tmp/foo@tmp/durable-44033be5 (running Jenkins temporarily with -Dorg.jenkinsci.plugins.durabletask.BourneShellScript.LAUNCH_DIAGNOSTICS= true might make the problem clearer) [Pipeline] } [Pipeline] // dir [Pipeline] echo -> exception: hudson.AbortException: script returned exit code -2 [Pipeline] sh + echo /tmp in container(second-container): /tmp in container(second-container): + ls -al /tmp total 16 drwxrwxrwt 1 root root 4096 Jul 19 04:35 . drwxr-xr-x 1 root root 4096 Jul 19 16:13 .. drwxr-xr-x 2 jenkins jenkins 4096 Jul 19 04:35 hsperfdata_jenkins drwxr-xr-x 1 root root 4096 Jul 4 09:27 hsperfdata_root [Pipeline] } [Pipeline] // container [Pipeline] sh + echo /tmp in default container: /tmp in default container: + ls -al /tmp total 24 drwxrwxrwt 1 root root 4096 Jul 19 16:13 . drwxr-xr-x 1 root root 4096 Jul 19 16:13 .. drwxr-xr-x 2 jenkins jenkins 4096 Jul 19 16:13 foo drwxr-xr-x 2 jenkins jenkins 4096 Jul 19 16:18 foo@tmp drwxr-xr-x 2 jenkins jenkins 4096 Jul 19 16:13 hsperfdata_jenkins drwxr-xr-x 1 root root 4096 Jul 4 09:27 hsperfdata_root Looking a bit at the sources, it seems to me that the cause of the problem is that hudson.FilePath used in org.jenkinsci.plugins.workflow.steps.PushdStep holds information about the agent but not about the container causing org.jenkinsci.plugins.durabletask.BourneShellScript to create its temporary files at the wrong place and consequently not able to execute the script.

            Unassigned Unassigned
            piratejohnny Jon B
            Votes:
            1 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: