我使用mac。我有ios和android项目的反应-本机和创建快速通道脚本为每个项目。现在我想使用Jenkins进行自动化构建,所以我有了Jenkins文件。在Jenkins的工作空间中,我必须转到ios
文件夹,并执行快速通道脚本。
但问题是Jenkins不会使用sh 'cd ios'
命令更改目录。我可以看到它,因为我在更改目录命令之前和之后执行了pwd
命令。
我尝试使用符号链接,在当前进程中使用“点”命令(如sh '. cd ios'
)运行命令,尝试使用ios文件夹的完整路径。但所有这些都没有带来成功:
那么,为什么Jenkins不使用sh 'cd ios'
命令更改目录呢?我该如何应对呢?提前谢谢你。
这是我的脚本
pipeline {
座席any
工具{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'
}
}
}}
发布于 2019-09-20 20:23:39
这是因为所有Jenkins命令都运行在Jenkins home/workspace/your pipeline name目录中。
如果您需要更改目录,则您的脚本应如下所示:
node {
stage("Test") {
sh script:'''
#!/bin/bash
echo "This is start $(pwd)"
mkdir hello
cd ./hello
echo "This is $(pwd)"
'''
}
}
您的输出将是:
第二个sh
命令将在工作区目录中启动。
发布于 2019-09-20 21:37:00
只是为了记录,因为它更具描述性,并且您正在使用描述性管道;)
如果您想在特定目录中执行某些工作,可以使用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
发布于 2019-09-22 14:39:35
使用下面的格式运行您的命令,这就是在Jenkins文件中运行任何shell脚本的方式。
// Shell格式: sh“#!/bin/bash您的命令”
示例:
sh """
#!/bin/bash
cd /Users/igor/.jenkins/workspace/MobileAppsPipeline/ios
ls -l
/usr/local/bin/fastlane build_and_push
"""
https://stackoverflow.com/questions/58027675
复制相似问题