NashTech Insights

file parameter plugin in jenkins

Prashant
Prashant
Table of Contents
woman using macbook sitting on white couch
jenkins

What is Cloudbees CI ?

To deliver CI at scale, CloudBees CI is a fully featured, cloud-native capability that can be hosted on-premises or in the public cloud. It gives all your development teams using Jenkins a shared, centrally controlled, self-service experience. On contemporary cloud infrastructure, CloudBees CI is made to function with Kubernetes. For on-premise installations, CloudBees CI has been created on conventional platforms.

File Parameter plugin in Jenkins

Provides an alternative set of file parameters that work with Pipeline, unlike the type built into Jenkins core.

Tier 3: Community

Plugin ID: file-parameters

Minimum Jenkins required: 2.361.4

Last released: 1 month, 12 days ago

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 – as a Base64-encoded environment variable:

node {

    sh 'echo $FILE | base64 -d'

}

Or via a temporary file with the decoded content:

node {

    withFileParameter('FILE') {

        sh 'cat $FILE'

    }

}

Stashed file parameter

A stashed file parameter can also be accessed in a couple of ways – as a stash of the same name with a single file of the same name:

node {

    unstash 'FILE'

    sh 'cat FILE'

}

Or via a temporary file:

node {

    withFileParameter('FILE') {

        sh 'cat $FILE'

    }

}

Accessing original upload filename

Original filename will be stored in evironment in <parameter_name>_FILENAME variable – assuming parameter is named FILE, snippet below will give you file with original filename in current workspace:

node {

  unstash 'FILE'

  sh 'mv FILE $FILE_FILENAME'

}

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)))]

Usage with HTTP API

You can pass file parameters to the HTTP API (in the Jenkins UI, this HTTP API is also referred to as “REST API”):

Curl example:

curl -u $auth -F FILE=@/tmp/f $jenkins/job/myjob/buildWithParameters

Javascript example:

const file = fileInput.files[0]; // a File object

const body = new FormData();

body.append(‘FILE’, file); // will come through to the job as the named file parameter ‘FILE’

const request = new Request(`${jobUrl}buildWithParameters`, { method: ‘POST’, body });

fetch(request); // omitted, API token and other credentials

Conclusion:

File parameter plugin offers substitutes for the Jenkins core-built type of file parameter that are compatible with Pipeline and are free of its architectural shortcomings.

Reference :

  1. https://plugins.jenkins.io/file-parameters/
  2. https://docs.cloudbees.com/docs/cloudbees-ci/latest/plugin-management/plugin-management-decision-tree
Prashant

Prashant

Leave a Comment

Your email address will not be published. Required fields are marked *

Suggested Article

%d bloggers like this: