4

I have a Jenkins Job which accepts a File as a parameter and using same file I would like to trigger downstream project. How do I do it ? It seems jenkins doesn't support passing files as parameter to downstream project. I am using 'Trigger/Call builds on other projects' to trigger a downstream project.

Jesse Glick
  • 24,539
  • 10
  • 90
  • 112
PRLobo
  • 41
  • 1
  • 1
  • 3

2 Answers2

6

Use the Parameterized Trigger Plugin but be aware of the following:

The File Parameter help reads:

The name of the submitted file is available in the environment variable whose name is the same as file location. For example, if you set the file location to be abc.zip [UPLOADED_FILE], then ${abc.zip} [${UPLOADED_FILE}] would give you the original file name passed from the browser (such as my.zip.) The name will not include the directory name portion.

[Strikethroughs and additions by me.]

Unfortunately this is wrong in more than one respect (with Jenkins v1.609.1):

  • The file name abc.zip becoming an environment variable name is bad.
  • If there is a directory name portion in the File Location field it is included in the variable's name.

Why is each of these bad?

Well, the latter is the opposite to the inline help and both might lead to unexpected results (and it did in my case), since '.' and '/' are not standardized characters in variable names according to IEEE Std 1003.1, 2013 Edition:

Environment variable names used by the utilities in the Shell and Utilities volume of POSIX.1-2008 consist solely of uppercase letters, digits, and the ( '_' ) [...]

See also Robert Gamble's answer to Allowed characters in linux environment variable names.

So, the answer is:

  • Don't use a path or an extension in File ParameterFile Location, use e.g. just UPLOADED_FILE
  • <Your upstream project> → ConfigureAdd post-build actionTrigger parameterized build on other projectsAdd parametersPredefined parametersParameters:

    KEY=value pairs, one per line (Java properties file format). [...]

    Current build parameters and/or environment variables can be used in form: ${PARAM} or $PARAM.

    Long story short. This passes the file's absolute name to your downstream project:

    ENV_VAR_IN_DOWNSTREAM_PROJECT=${WORKSPACE}/${UPLOADED_FILE}
    

UPDATE

I created a respective issue: [JENKINS-28996] Environment variable name created from File Parameter → File Location contains the "directory name portion" though stated differently in its inline help

Community
  • 1
  • 1
Gerold Broser
  • 14,080
  • 5
  • 48
  • 107
  • this doesnt allow files as parameters. – Michael Niemand Jun 15 '15 at 12:54
  • 1
    @MichaelNiemand How is Jenkins supposed to pass _files_ as in _container with content?_ It's not a file manager, after all. ;-) _File names_ on the other hand aren't a big deal. See the update to my answer. – Gerold Broser Jun 15 '15 at 15:28
  • @GeroldBroser so it is not possible to pass a file parameter from a job A run on jenkins master to a downstream job B run on a slave? – thunderbird Nov 09 '15 at 13:38
  • @thunderbird As I said: You can pass a _file name_ as parameter. You can't pass _a file with its content_ as parameter, of course. You can use a combination of a file name parameter and the [Copy To Slave Plugin](https://wiki.jenkins-ci.org/display/JENKINS/Copy+To+Slave+Plugin) in your case. – Gerold Broser Nov 09 '15 at 16:24
  • thanks.. i was also looking into copy to save and the download file from url plugins. – thunderbird Nov 09 '15 at 16:25
  • 1
    You can use the parameter factory *For every matching file, invoke one build* from the Parameterized Trigger Plugin. It pushes a specified file into the workspace of your downstream job. Check my response [here](http://stackoverflow.com/a/36157620/3623345) – Dominik Gebhart Mar 23 '16 at 20:05
0

This approach assumes you have the file in the current job's workspace.

pipeline
{
    agent any
    stages {
        stage('Pass file type param to build job') {
            steps {
                script {
                    def propertiesFilePath = "${env.WORKSPACE}/sample.properties"
                    build job: 'other-project',
                            parameters: [[$class: "FileParameterValue", name: "propertiesFile", file: new FileParameterValue.FileItemImpl(new File(propertiesFilePath))]]

                }
            }
        }
    }
}

Here the name of the downstream/child job is 'other-project' and the name of the file type parameter in this downstream/child job is 'propertiesFile'. The type FileParameterValue.FileItemImpl is defined in the class FileParameterValue and is internally used in jenkins to handle FileItem, also adding serialization support to the same.

  • I get `NoSuchFileException: foo/bar/blah/myfile.rpm` in the downstream job. If the file is serialized, I would expect it to work regardless of on which worker it runs. Any idea? – Michel Jung Feb 28 '22 at 07:38