Member-only story

How to overcome Jenkins pipeline inability to use file parameters

Asanka Vithanage
2 min readAug 7, 2020

--

Recently I was trying to implement a Jenkins pipeline which can take a file as a build parameter.

I was planning to use the Jenkins File parameter feature for this purpose.

Jenkins provides a File parameter which allows a build to accept a file, to be submitted by the user when scheduling a new build. The file will be placed inside the workspace at the known location after the check-out/update is done so that your build scripts can use this file.

But unfortunately, implementation was blocked due to the known limitation of the Jenkins side as reported on https://issues.jenkins-ci.org/browse/JENKINS-47333 and https://issues.jenkins-ci.org/browse/JENKINS-27413 tickets.

Basically file parameter doesn't work with Jenkins pipelines mode though it works on normal UI mode.

While searching for a solution, figured out following Groovy code can be used for file upload implementation.

// Get file using input step, will put it in build directory
print "=================Please upload your property file here ====================="
def inputFile = input message: 'Upload file', parameters: [file(name: 'global.properties')]
// Read contents and write to workspace
writeFile(file: 'global.properties', text: inputFile.readToString())
// Stash it for use in a different part of the pipeline
stash name: 'data', includes: 'global.properties'

But to make things worse, In my solution, I was using Jenkins master and slave setup. Jenkins slaves also dynamically created on a Kubernetes cluster as part of the pipeline script.

Yea, I certainely need to write on how I implemented Jenkins Master and dynamic jenkins slave setup on top of Kubernetes . Lot to write on that :D

Groovy code didn't execute on slave nodes since It required to access the workspace file system.

Hence I had to run the Groovy code on the Jenkins master node first and get the file uploaded to the master workspace. then copy that file to the Jenkins slave node, where actual build execution happens and the file is consumed.

After long research and trial and errors, got the following code implemented which takes to file uploaded to Jenkins master node and then copy that file to Jenkins slave node.

Once the user triggers the build, He will prompt for file upload in the middle of the build execution.

--

--

Asanka Vithanage
Asanka Vithanage

Written by Asanka Vithanage

Software Quality Assurance Professional, Problem Solver, SOA Tester, Automation Engineer, CI/CD Practitioner, DevOps enthusiast

Responses (2)

Write a response