Jenkins 不会通过 sh 'cd directory' 命令更改目录

Igo*_*uyk 6 jenkins fastlane

我用的mac。我在 react-native 上有 ios 和 android 项目,并为每个项目创建了 fastlane 脚本。现在我想在管道中使用 Jenkins 自动构建,所以我有 Jenkins 文件。在 Jenkins 的工作空间中,我必须转到ios文件夹,然后执行 fastlane 脚本。

但问题是 Jenkins 不会使用 command 更改目录sh 'cd ios'。我可以看到它,因为我pwd在更改目录命令之前和之后执行命令。

我尝试使用符号链接,在当前进程中使用“点”命令(如sh '. cd ios')运行命令,尝试使用 ios 文件夹的完整路径。但这一切都没有带来成功:(

那么,为什么 Jenkins 不使用sh 'cd ios'命令更改目录?我该如何应对?先感谢您。

这是我的脚本

pipeline {
Run Code Online (Sandbox Code Playgroud)

代理任何

工具 {nodejs“Jenkins_NodeJS”}

阶段{

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'
    }
}
Run Code Online (Sandbox Code Playgroud)

} }

Sas*_*erg 13

仅作记录,因为它更具描述性,并且您正在使用描述性管道;)

如果你想在特定目录中做一些工作,有一个step

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

下面的例子

pipeline {
    agent any

    stages {
        stage('mkdir') {
            steps {
              sh'mkdir ios && touch ios/HelloWorld.txt'  
            }
        }
        stage('test') {
            steps {
              dir('ios') {
                  sh'ls -la'
              }
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

产生输出

[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
Run Code Online (Sandbox Code Playgroud)


ozl*_*vka 8

这是因为所有 Jenkins 命令都在目录 [Jenkins home]/workspace/[您的管道名称] 中运行(我希望您使用管道)。

如果您需要更改目录,那么您的脚本应该如下所示:

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

你的输出将是:

在此输入图像描述

第二个sh命令将在工作区目录中启动。