Search code examples
jenkinsparameterscontinuous-integrationjenkins-groovyjenkins-declarative-pipeline

How to use file parameter in Jenkins declarative pipeline?


I am currently using the following code to upload file in Jenkins declarative pipeline and read the content from file. But the file is not stored in the Jenkins workspace or anywhere. So, whenever I run pipelines, it shows file not found error.

I tried other ways which are available on Internet but did not get output. Can anyone suggest a proper way to upload file in Jenkins and read the data from it?

pipeline {
  agent any
    parameters {
        file(name: 'yamlFile', description: 'Upload file test')
    }
stages {
 stage ("Checkout demo repo") {
   steps {
      script{
                echo "${WORKSPACE}"
                def configVal = readYaml file: yamlFile

             }
          }
       }
   }
}


Solution

  • I never had luck using the default File Input with a declarative Pipeline. Instead, I used the File Parameters plugin. Here is an example.

    pipeline {
      agent any
      parameters {
        base64File 'yamlFile'
      }
      stages {
        stage('Example') {
          steps {
            withFileParameter('yamlFile') {
              def configVal = readYaml file: yamlFile
            }
          }
        }
      }
    }