Parameterized Jenkinsfile

If you work with multiple projects and use Jenkins to run automatic builds, tests or deployments, you can extract the Jenkins configuration and have it in your project repository. Doing that you can track any changes and have the configuration saved in file, with your project and outside the Jenkins server.

Any change in the process can now be done in one file.

Jenkins Shared Library

You need to have a Git Repository for this shared library and configure it in Jenkins. There is a section called Global Pipeline Libraries in the configuration page.

For Jenkins to be able to use the shared library, all the files need to be in a “vars” folder. More details on the Jenkins website:

Once this is set up you can start adding files. I will only talk about one file in this post but you will see that my example is calling other files.

You can create a mainJenkinsfile.groovy:

def call(def config) {
  try {
    stage('Build') {
      echo "-- Checking out SCM --"
      checkout scm
      echo "-- Restoring NuGet packages --"
      bat "${baseVariables.nuGetPath} restore ${config.src}/${config.projectName}.sln"
      echo '-- Building the solution'
      bat "\"${baseVariables.msbuildPath}\" ${config.src}/${config.projectName}.sln /p:Configuration=${config.environment} /t:Clean,Build"

    stage('Tests') {
      if (config.runTests == true) {
        echo "-- Running Tests --"
        bat "${baseVariables.nunit3Path} ${config.src}/${config.testProjectName}/bin/${config.environment}/${config.testProjectName}.dll"

    if (env.BRANCH_NAME == "master") {
      echo "-- Calling extra step to confirm the releases --"

    if ((env.BRANCH_NAME == "develop" || env.BRANCH_NAME == "master") {
      echo "-- Calling deployment steps --"

    catch (e) {
    // ABORTED shouldn't send an email. It comes from the masterBuildStep if the user doesn't validate the release
    if (currentBuild.result != 'ABORTED') {
      currentBuild.result = 'FAILURE'
      if (config.sendEmailToDev) {
        step([$class: 'Mailer', notifyEveryUnstableBuild: true, recipients: '', sendToIndividuals: true])
      throw e

This will use the configuration provided by another Jenkinsfile that you set up in your project repository.

Here the “masterBuildStep” and “deploymentSteps” are two other groovy files with a set of steps. The files are named as they are called in this Jenkinsfile and also start with def call(def config) {} so they can access all the variables.

With this main Jenkinsfile you will then be able to build all your applications.

Jenkinsfile for your application

Now that all the steps are configured in the shared library this Jenkinsfile is mainly to define the variable for your application.

You still need a Multibranch Pipeline project in Jenkins to build your application. Select Build Configuration to be “by Jenkinsfile” and add a Jenkinfiles in your repository.


def config = [
  src:"src",   //if you have a 'src' folder
  projectName:"MyProject",    //sln file should match project folder name
  environment:(env.BRANCH_NAME == "master") ? "live" : "dev"

node('master') {

One thought on “Parameterized Jenkinsfile

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s