Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export command in Jenkins pipeline

How to add an 'export' unix command in a Jenkins pipeline? I have a Jenkins 'stage' and 'steps' within it. What is the syntax for an export command. I need to set the environment variable 'PATH' using the export command.

like image 267
OnePlus Avatar asked Jun 05 '17 21:06

OnePlus


People also ask

How do I export Jenkins pipeline?

Create a project on JenkinsEnter function-export-example as name, select Pipeline , and click OK . In the newly created project, click Configure to set up. In the Pipeline section, select Pipeline script from SCM as Definition , Git as SCM , and your repo url as Repository URL . Click Save at the bottom.

What is env in Jenkins file?

What are Environment Variables in Jenkins? Environment variables are global key-value pairs Jenkins can access and inject into a project. Use Jenkins environment variables to avoid having to code the same values for each project. Other benefits of using Jenkins environment variables include improved security.


1 Answers

You can update the $PATH like this:

pipeline {
  agent { label 'docker' }
  stages {
    stage ('build') {
      steps {

        // JENKINSHOME is just a name to help readability
        withEnv(['PATH+JENKINSHOME=/home/jenkins/bin']) {
          echo "PATH is: $PATH"
        }
      }
    }
  }
}

When I run this the result is:

[Pipeline] echo
PATH is: /home/jenkins/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games

What the heck is this PATH+JENKINSHOME syntax? Quoting from a blog at designhammer.com:

This:

/my/additional/path:$PATH

is expressed as:

PATH+ANYSTRING=/my/additional/path.

ANYSTRING is just a name to help readability. If it doesn't help readability, in your view, you can omit it. So this is equivalent:

PATH+=/my/additional/path

The above (withEnv) allows you to update the $PATH for a specific part of your pipeline. To update the $PATH for the entire pipeline, you can't use the PATH+ANYSTRING syntax, but this works:

pipeline {
  agent { label 'docker' }
  environment {
    PATH = "/hot/new/bin:$PATH"
  }
  stages {
    stage ('build') {
      steps {
        echo "PATH is: $PATH"
      }
    }
  }
}

Produces output:

[Pipeline] echo
PATH is: /hot/new/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
like image 98
burnettk Avatar answered Oct 20 '22 03:10

burnettk