Jenkins: setting env variable from shell script

14,591

Solution 1

I ended up using a file to propagate data between builds. The first build creates a file containing the information I need to propagate (host name of the newly created EC2 instance). The file looks like:

host.name=ec2.123.3345.amazon.com

I use the EnvInject plugin to read the file and "inject" the properties that are then available in the next build (I'm using the Build Flow Plugin to orchestrate builds).

Solution 2

There is a plugin that you can install for inject your variable: EnvInject Plugin

If I understood your problem, I think this simple plugin is what you need.

Share:
14,591
Luciano Fiandesio
Author by

Luciano Fiandesio

I am a software architect with 25+ years of professional experience. As a software development generalist with broad industry experience, I am best utilized in a senior engineer or Tech Lead role. I have significant experience building and launching products/applications from the ground up, and I have had primary responsibilities across the software development spectrum, including system architecture, backend, frontend, and Devops. I’m the co-author of the book “Groovy 2 Cookbook”, published by Packt in 2013. Industry conference speaker (Devoxx, Codemotion, DevConFu, various JUG in Europe)

Updated on June 23, 2022

Comments

  • Luciano Fiandesio
    Luciano Fiandesio almost 2 years

    I have a bash script that I execute from a Jenkins job, using "execute shell". The script starts an EC2 instance and sets an host variable containing the host name of the new instance.

    I would like to set the host name of the new instance (script variable: host ) to a Jenkins environment variable so that I can pass it down to a downstream job (possibly using the Build Flow plugin).

    Any idea how to do so?

    Thanks

  • Luciano Fiandesio
    Luciano Fiandesio about 11 years
    I'm already looking at this plugin, but I can't find an example that suits my need.
  • Vargan
    Vargan about 11 years
    It's very simple: you need to add as first build step a new "inject environment variable", and specify your path, or if you prefer directly the variable: wiki.jenkins-ci.org/display/JENKINS/EnvInject+Plugin
  • Luciano Fiandesio
    Luciano Fiandesio about 11 years
    Unfortunately it's a bit more complex than that. If you read my original post carefully, the requirement is that I need to run a shell script which internally sets a variable "host". This variable should be somehow set as environment variable in Jenkins
  • Vargan
    Vargan about 11 years
    Why not set the variable outside and then run your script?
  • Luciano Fiandesio
    Luciano Fiandesio about 11 years
    Because the variable is created INSIDE the script. When you start a new EC2 instance a new host name is generated every time and I need to "extract" this host name and pass it to the next job.
  • Vargan
    Vargan about 11 years
    Ok, so maybe you need a parametrized build and then export the value: wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build
  • Vargan
    Vargan about 11 years
  • CSchulz
    CSchulz about 11 years
    I had the same problem, it is bad that there is no other solution, yet.
  • NotAgain says Reinstate Monica
    NotAgain says Reinstate Monica over 9 years
    If you are using NANT then it is possible to inject the environment variable using EnvInject plugin and set it to some default value. Then in the NANT script use setenv task to set it to the correct value.