Jenkins Pipeline Job with file parameter

前端 未结 8 1959
难免孤独
难免孤独 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: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.."
                        }
                    }
                }
            }
    

提交回复
热议问题