user2547836
user2547836

Reputation: 89

How to upload a file to jenkins pipeline job as build parameter

I want to upload file as a build parameter as apart of jenkins pipeline job, I tried File parameter but, it is not uploading file. there is nothing in console logs, what am I missing?

configure looks like this,

enter image description here

enter image description here

Upvotes: 1

Views: 7797

Answers (1)

Romain DEQUIDT
Romain DEQUIDT

Reputation: 908

fileParamis not directly reachable from job, because it's not uploaded into the workspace. Furthermore, there is another difference between jobs built on master or on slaves (i.e. when job is built on slaves, file mush be copied through channel)

Here is a sample of dsl to create job that upload file and send it through e-mail:

Pipeline parameters definition:

def job = pipelineJob('MAIL_WITH_UPLOADED_ATTACHMENT') {
    parameters {
        ...
        fileParam('ATTACHMENT', 'Fichier à uploader et attacher à l\'e-mail')
        ...
    }
}

Declarative pipeline DSL:

def createFilePath(path) {
    if (env['NODE_NAME'] == null) {
        error "envvar NODE_NAME is not set, probably not inside an node {} or running an older version of Jenkins!";
    } else if (env['NODE_NAME'].equals("master")) {
        return new hudson.FilePath(null, path)
    } else {
        return new hudson.FilePath(jenkins.model.Jenkins.instance.getComputer(env['NODE_NAME']).getChannel(), path)
    }
}

def getFileParamFromWorkspace(fileParamName) {
    def paramsAction = currentBuild.rawBuild.getAction(ParametersAction.class);
    if (paramsAction != null) {
        for (param in paramsAction.getParameters()) {
            if (param instanceof FileParameterValue) {
                def fileParameterValue = (FileParameterValue)param
                if (fileParamName.equals(fileParameterValue.getName())) {
                    def fileItem = fileParameterValue.getFile()
                    if (fileItem instanceof org.apache.commons.fileupload.disk.DiskFileItem) {
                        def diskFileItem = (org.apache.commons.fileupload.disk.DiskFileItem)fileParameterValue.getFile()
                        def filePath = createFilePath(env.WORKSPACE + '/fileparam/' + fileItem.getName())
                        def destFolder = filePath.getParent()
                        destFolder.mkdirs()
                        filePath.copyFrom(diskFileItem)
                        return 'fileparam/' + fileItem.getName()
                    }
                }
            }
        }
    }
    return ''
}

pipeline {
    stages {
        stage ('Mail with uploaded attachment') {
            steps {
                script {
                    def filename = getFileParamFromWorkspace('ATTACHMENT')
                    emailext to: "${params.RECIPIENT_EMAIL}",
                        subject: "${params.SUBJECT} (via ${env.JOB_NAME} ${currentBuild.currentResult})",
                        body: """${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}
                        |
                        | More info at: ${env.BUILD_URL}""".stripMargin(),
                        attachmentsPattern: filename
                }
            }
        }
    }
}

Upvotes: 1

Related Questions