Jenkins Pipeline Job with file parameter

前端 未结 8 1926
难免孤独
难免孤独 2020-12-01 10:33

I\'m putting together a Jenkins pipeline job which will take a file parameter. I can trigger the job and point it at a file however I can\'t find where the file has ended up

相关标签:
8条回答
  • 2020-12-01 11:08

    I found a solution in the form of a global library here: https://bitbucket.org/janvrany/jenkins-27413-workaround-library/src/default/

    It contains calls to inner methods of Jenkins which are deprecated (I guess). So I made my own version like this:

    import hudson.FilePath
    import hudson.model.ParametersAction
    import hudson.model.FileParameterValue
    import hudson.model.Executor
    
    def call(String name, String fname = null) {
        def paramsAction = currentBuild.rawBuild.getAction(ParametersAction.class);
    
        if (paramsAction == null) {
            error "unstashParam: No file parameter named '${name}'"
        }
    
        for (param in paramsAction.getParameters()) {
            if (param.getName().equals(name)) {
                if (! param instanceof FileParameterValue) {
                    error "unstashParam: not a file parameter: ${name}"
                }
                if (env['NODE_NAME'] == null) {
                    error "unstashParam: no node in current context"
                }
                if (env['WORKSPACE'] == null) {
                    error "unstashParam: no workspace in current context"
                }
                workspace = new FilePath(getComputer(env['NODE_NAME']), env['WORKSPACE'])
                filename = fname == null ? param.getOriginalFileName() : fname
                file = workspace.child(filename)
                file.copyFrom(param.getFile())
                return filename;
            }
        }
    }
    
    
    def getComputer(name){
    
        for(computer in Jenkins.getInstance().getComputers()){ 
            if(computer.getDisplayName() == name){
                return computer.getChannel()
            }
        }
    
        error "Cannot find computer for file parameter workaround"
    }
    

    You can insert it in a global library and then use it like:

    library "file-workaround"
    
    node {
        def file_in_workspace = unstashParam "myFile"
        sh "cat ${file_in_workspace}"
    }
    

    It's not pretty but it's working and as long as there is no official fix, it's my best shot.

    Update
    Turns out you might run into "No such file or directory". That's because nothing in the workaround triggers Jenkins to create the workspace directory. If that was triggered somewhere else in the pipeline good, otherwise you'll be scratching your head.
    You might wanna throw a

    touch "thisIsAFile"
    

    in there

    0 讨论(0)
  • 2020-12-01 11:11

    To handle an optional file parameter in pipeline (to handle the use case where no file should be accepted) you could use jenkinsci-unstashParam-library (add it in Jenkins>Manage Jenkins>Configure System>Global Pipeline Libraries https://github.com/janvrany/jenkinsci-unstashParam-library) with a try/catch in a script as this sample stage:

        stage('upload') {
                steps {
                    // delete workspace
                    cleanWs()
    
                    // handle file parameters in pipeline (JENKINS-27413)
                    script {
    
                        try {
                            // force workspace directory creation
                            sh "touch emptyFileToCreateWorkspace"
    
                            // https://stackoverflow.com/questions/59468464/fetching-uploaded-files-in-jenkins
                            def file_in_workspace = unstashParam 'MY_FILE.xlsx'
    
                            // https://unix.stackexchange.com/questions/125776/error-with-a-file-name-containing-parentheses
                            sh "mv '${file_in_workspace}' MY_FILE.xlsx"
                        }
                        catch (Exception e) {
                            echo e.getMessage()
                            echo "No file parameter, we will continue.."
                        }
                    }
                }
            }
    
    0 讨论(0)
提交回复
热议问题