Jenkins doesn't change directory through sh 'cd directory' command Jenkins doesn't change directory through sh 'cd directory' command jenkins jenkins

Jenkins doesn't change directory through sh 'cd directory' command


Just for the record because it is more descriptive and you are using descriptive pipelines ;)

If you want to do some work in specific directory, there is a step for that:

stage('Test') {  steps {      dir('ios') { // or absolute path      sh '/usr/local/bin/fastlane build_and_push'    }  }}

The following example

pipeline {    agent any    stages {        stage('mkdir') {            steps {              sh'mkdir ios && touch ios/HelloWorld.txt'              }        }        stage('test') {            steps {              dir('ios') {                  sh'ls -la'              }            }        }    }}

produces the outut

[Pipeline] stage[Pipeline] { (mkdir)[Pipeline] sh+ mkdir ios 6073 touch ios/HelloWorld.txt[Pipeline] }[Pipeline] // stage[Pipeline] stage[Pipeline] { (test)[Pipeline] dirRunning in /stuff/bob/workspace/test-1/ios[Pipeline] {[Pipeline] sh+ ls -latotal 12drwxrwxr-x 3 bob bob 4096 Sep 20 13:34 .drwxrwxr-x 6 bob bob 4096 Sep 20 13:34 ..drwxrwxr-x 2 bob bob 4096 Sep 20 13:34 HelloWorld.txt[Pipeline] }[Pipeline] // dir[Pipeline] }[Pipeline] // stage[Pipeline] }[Pipeline] // node[Pipeline] End of Pipeline


This because of all Jenkins commands run in directory [Jenkins home]/workspace/[your pipeline name] (I hope you use pipeline).

If you have some need to change directory then your script should be like:

node {    stage("Test") {        sh script:'''          #!/bin/bash          echo "This is start $(pwd)"          mkdir hello          cd ./hello          echo "This is $(pwd)"        '''    }}

And your output will be:

enter image description here

Second sh command will start in workspace directory.


Run your commands using below format, thats how any shell script can be run through in Jenkins file.

// Shell format:sh """ #!/bin/bash your command"""

Example:

    sh """     #!/bin/bash    cd /Users/igor/.jenkins/workspace/MobileAppsPipeline/ios    ls -l    /usr/local/bin/fastlane build_and_push    """