File Parameter

ID: file-parameters

Introduction

Offers alternative types of file parameter that are compatible with Pipeline and do not suffer from the architectural flaws of the type built into Jenkins core.

See JENKINS-27413 and JENKINS-29289 for background.

Minimal usage

If you defined a Base64 file parameter named FILE in the GUI configuration for a Pipeline project, you can access it in a couple of ways:

node {
    sh 'echo $FILE | base64 -d'
    withFileParameter('FILE') {
        sh 'cat $FILE'
    }
}

That is: as a Base64-encoded environment variable; or via a temporary file with the decoded content.

A stashed file parameter can also be accessed in a couple of ways:

node {
    unstash 'FILE'
    sh 'cat FILE'
    withFileParameter('FILE') {
        sh 'cat $FILE'
    }
}

That is: as a stash of the same name with a single file of the same name; or, again, via a temporary file.

Usage in Declarative Pipeline

You can now declare and use file parameters via Declarative Pipeline syntax:

pipeline {
  agent any
  parameters {
    base64File 'small'
    stashedFile 'large'
  }
  stages {
    stage('Example') {
      steps {
        withFileParameter('small') {
          sh 'cat $small'
        }
        unstash 'large'
        sh 'cat large'
      }
    }
  }
}

Usage with input

You can use Base64 parameters for uploading small files in the middle of the build:

def fb64 = input message: 'upload', parameters: [base64File('file')]
node {
    withEnv(["fb64=$fb64"]) {
        sh 'echo $fb64 | base64 -d'
    }
}

Currently there is no mechanism for doing this with stashed files. Nor can you use the withFileParameter wrapper here.

Usage with build

You can use Base64 parameters for passing small files to downstream builds:

build job: 'downstream', parameters: [base64File(name: 'file', base64: Base64.encoder.encodeToString('hello'.bytes)))]

LICENSE

Licensed under MIT, see LICENSE