即使文件存在,Jenkins管道“ Content Replace”插件也会出现“找不到文件”错误

时间:2019-02-04 11:50:58

标签: jenkins jenkins-pipeline

我正在第一次尝试Jenkins管道,似乎无法弄清楚这里出了什么问题。我从Subversion中签出了源代码,然后打算替换其中一个签出文件中的文件内容。

我收到以下错误(使用Windows Jenkins从站) java.io.FileNotFoundException:文件'D:\ JenkinsRoot \ workspace \ TestJob2 \ lib \ database.cfg'不存在

我可以看到该文件存在于从服务器上。

这是示例代码,

pipeline {
agent { label 'mynode' }
stages {
    stage('Test') {
        steps {
            checkout([$class: 'SubversionSCM', 
                additionalCredentials: [[...]], 
                excludedCommitMessages: '', 
                excludedRegions: '', 
                excludedRevprop: '', 
                excludedUsers: '', 
                filterChangelog: false, 
                ignoreDirPropChanges: false, 
                includedRegions: '', 
                locations: [[
                    credentialsId: '...', 
                    depthOption: 'infinity', 
                    ignoreExternalsOption: true, 
                    local: '.', 
                    remote: '...']], 
                workspaceUpdater: [$class: 'CheckoutUpdater']])
            sleep 5
            contentReplace(configs: [
                fileContentReplaceConfig(
                    configs: [
                        fileContentReplaceItemConfig(
                            matchCount: 1, 
                            replace: 'PSTG_USER=${PSTG_USER}', 
                            search: '^PSTG_USER=.*')], 
                        fileEncoding: 'UTF-8', 
                        filePath: 'lib/database.cfg')
        }
        }
    }
}

在上面的执行中,检出正常进行,我添加了sleep以确保我不会太早更新文件。但是,我仍然收到文件未找到错误。有指针吗?

文件在Windows从站上使用以下命令存在,

D:\>dir D:\JenkinsRoot\workspace\TestJob2\lib\database.cfg
 Volume in drive D has no label.
 Volume Serial Number is 3268-CE51

 Directory of D:\JenkinsRoot\workspace\TestJob2\lib

02/04/2019  06:24 AM               175 database.cfg
               1 File(s)            175 bytes
               0 Dir(s)  101,310,660,608 bytes free

1 个答案:

答案 0 :(得分:0)

以下代码对我有用,

pipeline {
    agent none
    stages {
        stage("Test") {
            steps {
                node('mynode'){
                    checkout(...)
                    script {
                        String out = readFile('lib/database.cfg').trim()
                        //This prints the original text
                        print out
                        out = out.replaceFirst(/DB_USER=.*/, "DB_USER=$DB_USER")
                        //This prints the replaced text
                        print out
                        writeFile(file: 'lib/database2.cfg', text: out, encoding: 'UTF-8')
                    }
                    //This prints the replaced text from the file
                    bat "type lib\\database2.cfg"
                }
            }
        }
    }
}