Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to invoke Inject environment variables to the build process plugin in jenkinsFIle jenkins 2.x with pipeline

I'm trying to migrate my project from jenkins 1 to jenkins 2.x using pipeline as code or Jenkinsfile.
But I don't see any option in snippet generator to generate environment injector plugin into a script in Jenkinsfile.

Anyone can help?

like image 509
Pandu Siregar Avatar asked Sep 29 '16 04:09

Pandu Siregar


People also ask

How do you inject environment variables to the build process?

EnvInject We can install and use the EnvInject plugin to inject environment variables during the build startup. In the build configuration window, we select the “Inject environment variables” option in the “Add build step” combo box. We can then add the required environment variables in the properties content text box.

How do you call an environment variable in Jenkinsfile?

Jenkins Environment Variable is a global variable exposed through the env variable and used anywhere in the Jenkins file. Any value stored in the env variable gets stored as a String type. Environment Variables can be set either at the pipeline top level, at the specific stage level, or inside the script block.


2 Answers

I'm assuming that you want to read properties from a specific file and inject them as environment variables?

If so, this is a solution:

  • Create the file that will contain the environment properties

    • You create some properties file called project.properties with following content:

      PROJECT_VERSION='1.4.34'

Then, on your pipeline code, you've to add the following code in order to be able to read the file and inject read variables as environment variables:

node { load "${WORKSPACE}\\project.properties" // assuming that props file is in Jenkins Job's workspace echo "PROJECT VERSION: ${PROJECT_VERSION}" }

  • First line read and inject variable PROJECT_VERSION as environment variable
  • Second line is just to print read variable to make sure that everything worked seamlessly

Result:

enter image description here

like image 197
Cristian Gonçalves Avatar answered Oct 07 '22 07:10

Cristian Gonçalves


Wanted to just comment on your question, but my lack of reputation is hindering me.

The list of supported steps is here: https://jenkins.io/doc/pipeline/steps/

In general, you can use other plugins by using their Java style invocation.

i.e.

step([$class: 'classname', parametername: 'value'])
like image 31
tphuoc Avatar answered Oct 07 '22 08:10

tphuoc