You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Jenkins文件参数未存入工作区问题求助及排查建议

问题

编写了一个Jenkins流水线脚本,可传递Python脚本声明的参数,但配置的file类型参数(USERS_XML)未存储到Jenkins工作区,执行testlink.py脚本时出现文件找不到的错误,求助排查原因及解决建议。

流水线脚本:

pipeline
{
    options {
        buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '5'))
    }
    
    agent {
        label "ubuntu-python"
    }
    parameters {
        file(
            name: 'USERS_XML',
            description: 'parse user.xml'
        )
    }
        // Global shell environment
    environment {
        HOME="${WORKSPACE}"
    }
    stages{
        stage('Checkout'){
            steps{
                checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'git', url: 'https://bitbucket.com/bitbucket/scm/~user/samplecode.git']]])
            }
        }
        stage('Python modules'){
            steps{
                sh """
                python3 --version
                pip3 --version
               
                #Install required modules
                pip3 install requests
                pip3 install TestLink-API-Python-client
                pip3 install openpyxl
                """
            }
        }
        stage('Get Testlink Users'){
            steps{
                sh label: 'Display testlink users', 
                script: '''
                
                python3 -u testlink.py "${USERS_XML}"
                    
                '''
            }
        }
    }
    post{
        success {
            archiveArtifacts allowEmptyArchive: true, artifacts: '**/*.xlsx', caseSensitive: false, onlyIfSuccessful: true
        }
    }
}

错误信息:

+ python3 -u testlink.py   tl-server devKey users.xml
------- User List for tl-server -------
Traceback (most recent call last):
  File "testlink.py", line 120, in <module>
    serverList()
  File "testlink.py", line 28, in serverList
    tree = ET.parse(""+USERS_XML+"") 
  File "/usr/lib/python3.8/xml/etree/ElementTree.py", line 1202, in parse
    tree.parse(source, parser)
  File "/usr/lib/python3.8/xml/etree/ElementTree.py", line 584, in parse
    source = open(source, "rb")
FileNotFoundError: [Errno 2] No such file or directory: 'users.xml'

原因分析与解决建议

核心原因

Jenkins的file类型参数默认仅将上传文件的文件名赋值给参数变量,不会自动把文件复制到当前工作区。你传递给Python脚本的${USERS_XML}只是文件名,而该文件实际存放在Jenkins的临时目录${WORKSPACE}/@tmp下,所以Python在工作区找不到文件导致报错。

解决方法

  • 方法1:复制临时文件到工作区
    在调用Python脚本前,先把上传的文件从临时目录复制到当前工作区,修改Get Testlink Users阶段的脚本:

    stage('Get Testlink Users'){
        steps{
            sh label: 'Display testlink users', 
            script: '''
            # 复制上传文件到工作区
            cp "${WORKSPACE}/@tmp/${USERS_XML}" "${WORKSPACE}/"
            python3 -u testlink.py "${USERS_XML}"
            '''
        }
    }
    
  • 方法2:直接传递完整临时路径给Python
    无需复制文件,直接把文件的完整临时路径传给Python脚本,同时确保Python脚本使用传入的完整路径而非仅文件名:

    stage('Get Testlink Users'){
        steps{
            sh label: 'Display testlink users', 
            script: '''
            python3 -u testlink.py "${WORKSPACE}/@tmp/${USERS_XML}"
            '''
        }
    }
    
  • 可选:添加文件检查步骤
    可以在脚本中加入文件检查,帮助确认文件位置和状态:

    sh '''
    echo "上传文件临时路径: ${WORKSPACE}/@tmp/${USERS_XML}"
    ls -l "${WORKSPACE}/@tmp/${USERS_XML}"
    '''
    

内容的提问来源于stack exchange,提问作者ckmb

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 15:29:52