Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'
    }
}

} }

like image 794
Igor Vlasuyk Avatar asked Sep 20 '19 11:09

Igor Vlasuyk


People also ask

How do I change the directory in Jenkins execute shell?

Oh in Jenkins job under build tab, there is an option where you can add a build and one of the options is "Execute shell script on remote host using ssh" . . After clicking that option, you will be required to put the ssh under the "SSH site" and the command that you want to execute under the "Command" Field . .

How do I change the directory in Jenkins pipeline?

dir : Change current directory Change current directory. Any step inside the dir block will use this directory as current and any relative path will use it as base path. The relative path of the directory in the workspace to use as a new working directory.

What is sh command in Jenkins?

On Linux, BSD, and Mac OS (Unix-like) systems, the sh step is used to execute a shell command in a Pipeline. Jenkinsfile (Declarative Pipeline) pipeline { agent any stages { stage('Build') { steps { sh 'echo "Hello World"' sh ''' echo "Multiline shell steps works too" ls -lah ''' } } } }


2 Answers

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
like image 172
Sascha Kolberg Avatar answered Sep 18 '22 05:09

Sascha Kolberg


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.

like image 30
ozlevka Avatar answered Sep 21 '22 05:09

ozlevka