Jenkins: setting env variable from shell script
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.
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, 2022Comments
-
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 about 11 yearsI'm already looking at this plugin, but I can't find an example that suits my need.
-
Vargan about 11 yearsIt'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 about 11 yearsUnfortunately 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 about 11 yearsWhy not set the variable outside and then run your script?
-
Luciano Fiandesio about 11 yearsBecause 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 about 11 yearsOk, so maybe you need a parametrized build and then export the value: wiki.jenkins-ci.org/display/JENKINS/Parameterized+Build
-
Vargan about 11 years
-
CSchulz about 11 yearsI had the same problem, it is bad that there is no other solution, yet.
-
NotAgain says Reinstate Monica over 9 yearsIf 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.