Jenkins Pipeline Job with file parameter

∥☆過路亽.° 提交于 2019-11-28 08:13:51

There is currently an issue with pipeline and file parameter (https://issues.jenkins-ci.org/browse/JENKINS-27413).

Christoph Forster

Solved it the following way:

node {
    deleteDir()
    stage("upload") {
        def inputFile = input message: 'Upload file', parameters: [file(name: 'data.zip')]
        new hudson.FilePath(new File("$workspace/data.zip")).copyFrom(inputFile)
        inputFile.delete()
    }
    stage("checkout") {
        echo fileExists('data.zip').toString()
    }
}

I know the solution is not that beautiful because the pipeline gets interrupted for the upload but it works.

Further the "copyFrom" is necessary, because the input stores the "data.zip" in the jobs directory and not in the workspace (don't know why)

Found a WA (Strictly for text based file input) We can use Jenkins multi-line string parameter and ask user to paste file contents to it. And in our pipeline, write contents of this parameter using pipeline step writeFile, as :

stage('File Param WA') {
          writeFile file: 'demo.yaml', text: params.DEMO_YAML
}

I tried using the solution provided by @Christoph Forster , but the input File was not getting copied anywhere in the workspace . So I used the workaround as provided in https://bitbucket.org/janvrany/jenkins-27413-workaround-library/src/6b7dada8ea37?at=default

The library provides a new library - unstashParam - that saves the file build parameter into a workspace. Works fine with text and yaml file .

I also tried using the solution by @Christoph Forster but I received a script security error when Groovy Sandbox is enable

org.jenkinsci.plugins.scriptsecurity.sandbox.RejectedAccessException: Scripts not permitted to use new hudson.FilePath java.io.File

However, it seems we can skip the file copying and deleting actions (and bypass the Groovy sandbox restriction) by simply requiring that the file is uploaded to the job workspace. Just add the workspace variable to the file name as follows:

stage("upload") {
def inputFile = input message: 'Upload file', parameters: [file(name: "$workspace/data.zip")]
        }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!