首页 > 解决方案 > 如何让 Jenkins 从远程根目录执行管道步骤?

问题描述

我在 Jenkins 中创建了一个简单的管道。我的代理的远程根目录设置为我的项目根路径。但是当我测试时,我在构建期间所处的位置(例如,通过定义类似的步骤sh 'pwd'),我看到目录,我的步骤是从$WORKSPACE目录(/path_to_remote_root_directory_of_the_agent/workspace/jenkins_project_title)执行的。这意味着,我既不能开始我的单元测试sh 'vendor/bin/phpunit ./test/Unit',也不能开始我通常从项目根文件夹运行的其他任务。

我很确定,我只是错误地配置了一些东西,在正常情况下,像这样的脚本

pipeline {
    agent {
        label 'devvm-slave-01'
    }

    stages {
        stage('Prepare') {
            steps {
                sh 'composer install'
                ...
            }
        }
        ...
        stage('Checkstyle') {
            steps {
                sh 'vendor/bin/phpcs --report=checkstyle --report-file=`pwd`/build/logs/checkstyle.xml --standard=PSR2 --extensions=php --ignore=autoload.php --ignore=vendor/ . || exit 0'
                checkstyle pattern: 'build/logs/checkstyle.xml'
            }
        }
    }
}

按预期工作,没有任何粗略的路径解决方法。

我做错了什么以及如何让它正常工作?

标签: jenkinspathjenkins-pipelinefilepath

解决方案


从“詹金斯手册”章节“管道语法”的“代理”部分:

参数

节点

agent { node { label 'labelName' } }行为与 agent 相同{ label 'labelName' },但 node 允许其他选项(例如customWorkspace)。

因此,解决方案是使用node及其customWorkspace选项:

pipeline {
    agent {
        node {
            label 'devvm-slave-01'
            customWorkspace '/path/to/my/project'
        }
    }
    ...
}

推荐阅读