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

时间:2019-09-20 11:40:15

标签: jenkins fastlane

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

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

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

那么,为什么詹金斯不使用sh 'cd ios'命令更改目录?我该如何应对?预先谢谢你。

这是我的剧本

pipeline {

代理任何人

工具{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'
    }
}

} }

3 个答案:

答案 0 :(得分:3)

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

如果您想在特定目录中执行某些工作,则可以使用step

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

以下示例

pipeline {
    agent any

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

产生输出

[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

答案 1 :(得分:1)

使用以下格式运行命令,那就是如何在Jenkins文件中运行任何Shell脚本。

// Shell格式: sh“”“  #!/ bin / bash  你的命令 “”“

示例:

const countries = ['US', 'FR', 'IT']
const area = [100, 105, 110, 115, 120, 125, 130]

let merge = countries.flatMap((c, i) => area.map(a => c + a))

console.log(merge)

答案 2 :(得分:0)

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

如果您需要更改目录,则您的脚本应类似于:

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

您的输出将是:

enter image description here

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

相关问题