Igor Vlasuyk
Igor Vlasuyk

Reputation: 574

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

I use mac. I have ios and android projects on react-native and created fastlane scripts for each project. Now I want to automate builds with Jenkins in pipeline, so I have Jenkins file. In work space of Jenkins I have to go to ios folder, and execute fastlane script.

But the problem is that Jenkins doesn't change directory with command sh 'cd ios'. I can see it because I execute pwd command before and after change directory command.

I tried to use symbolic link, run command in my current process with the "dot" command (like sh '. cd ios'), tried to use full path to ios folder. But all of this did not bring success :(

So, why Jenkins doesn't change the directory with sh 'cd ios' command? How can I cope with it ? Thank you in advance.

Here is my script

pipeline {

agent any

tools {nodejs "Jenkins_NodeJS"}

stages {

stage('Pulling git repo'){
  steps{
    git(
      url: 'url_to_git_repo',
      credentialsId: 'jenkins_private_key2',
      branch: 'new_code'
    )
  }
}

stage('Prepare') {
    steps{
      sh 'npm install -g yarn'
      sh 'yarn install'
    }
}

stage('Building') {
    steps{
      sh 'cd /Users/igor/.jenkins/workspace/MobileAppsPipeline/ios'
      sh 'ls -l'
      sh '/usr/local/bin/fastlane build_and_push'
    }
}

} }

Upvotes: 16

Views: 32611

Answers (3)

Sascha Kolberg
Sascha Kolberg

Reputation: 7162

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] dir
Running in /stuff/bob/workspace/test-1/ios
[Pipeline] {
[Pipeline] sh
+ ls -la
total 12
drwxrwxr-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

Upvotes: 26

Sumit
Sumit

Reputation: 944

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
    """ 

Upvotes: 2

ozlevka
ozlevka

Reputation: 2146

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.

Upvotes: 9

Related Questions