Run Jenkins ant build within special shell environment - ant

Our internal build system uses a shell script to setup the environment for building projects. Then the actual build tools (ant or make) can reference environment variables for configuring various things. In essence, it does:
$ /path/to/setup_env.sh .
[build env] $ ant compile
Note that the first command launches and initializes a new shell and expects all subsequent build operations to be performed in that shell.
Now I am trying to replicate the same within Jenkins. How do I run a shell script and then have the subsequent ant build step take place in the same environment?
The 'Execute Shell' built-in as well as the EnvInject plugin didn't help since they discard any changes to the environment before moving to the next build step.
I'd prefer not to modify the ant build file since the same should continue to work in the current internal build system.

This is a "solution" that worked out for us. The key idea is that the setup_env.sh script launches a new shell in which it exports a bunch of environment variables. What we needed was access to those variable definitions. So we did a three part Jenkins Build:
Step 1 - Execute Shell
Use the 'Execute Shell' Jenkins built-in to run our setup_env.sh script. Then feed the newly launched shell a simple python script that dumps the environment to a file.
/path/to/setup_env.sh . <<< 'python <<SC
print "Exporting env to buildenv.properties file"
import os
f = open("buildenv.properties", "w")
env = os.environ
for k in env:
f.write("%s=%s\n" % (k, env[k]))
f.close()
print "Done exporting env"
SC'
Step 2 - Inject Environment Variables
Now we use the EnvInject Plugin to inject environment variables from the file dumped in the previous step. The config here is simple, just specify the dumped properties file name as the Properties File Path field value.
Step 3 - Invoke Ant
Here, we kick off the normal ant build. Since the environment now contains all the required definitions, the build completes as normal.

Try EnvInject Plugin.

Related

Adding version number to Jenkins build artifact

I have been ordered to migrate a dotnet build from Bamboo to Jenkins. I used a Freestyle job to run a powershell script, using the PowerShell plugin and successfully built it. However I need to add version number to the built artifact. The Bamboo job uses:
~\.dotnet\tools\dotnet-lambda.exe package -pl $fullDir -f "netcoreapp3.1" -o Payment.${bamboo.majorVersion}.${bamboo.minorVersion}.${bamboo.revisionVersion}.${bamboo.buildNumber}.zip
I went into Jenkins Configuration and in Global Properties, created Environment variables named - buildNumber, majorVersion, minorVersion and revisionVersion, giving it values and in the Build part of the Freestyle job, I used:
~\.dotnet\tools\dotnet-lambda.exe package -pl $fullDir -f "netcoreapp3.1" -o Payment.${env.majorVersion}.${env.minorVersion}.${env.revisionVersion}.${env.buildNumber}.zip
However the name of the built artifact is: Payment.....zip
How can I pass the variable values?
Is there a way to auto increment the revisionNumber and buildNumber, instead of hard coding it?
I'm very new to both Bamboo and Jenkins. Any help would be extremely helpful.
Regards
Ramesh
Personally, I'd not configure this things globally as they seem job specific. Nevertheless,
Install the Environment Injector plugin. You now have two options:
General tab
[ X ] Prepare an environment for the run
Build Environment tab
[ X ] Inject environment variables to the build process
Set the "Properties Content" (that's an environment variable).
In your shell step( no need to preface with ${env....} ):
Execute Shell step:
#!sh -
echo ${FOO}.${BUILD_NUMBER}
echo ${LABEL}
Output:
[EnvInject] - Loading node environment variables.
[EnvInject] - Preparing an environment for the build.
[EnvInject] - Keeping Jenkins system variables.
[EnvInject] - Keeping Jenkins build variables.
[EnvInject] - Injecting contributions.
Building in workspace C:\Users\jenkins\.jenkins\workspace\Foo
[EnvInject] - Executing scripts and injecting environment variables after the SCM step.
[EnvInject] - Injecting as environment variables the properties content
FOO=bar
[EnvInject] - Variables injected successfully.
[Foo] $ sh - C:\Users\jenkins\AppData\Local\Temp\jenkins281351632631450693.sh
bar.8
Finished: SUCCESS
You'll also see at the bottom of the Execute Shell step a link to ${JENKINS_URL}/env-vars.html which lists variables available to shell scripts, which includes BUILD_NUMBER; use that in lieu of buildNumber.
The plugin also supports configuration same at both the Global and the Node level.
You can also have individual build steps to inject / change variables between job steps (we use this to set specific JAVA_HOME for SonarQube step).
You will also see an [Environment variables] button on the LH side of each build log to inspect what you ran with (see below).
If you add Build With Parameters plugin then you can be prompted to supply values for variables when triggering the job which can be consumed in the same fashion without re-configuring the job (it won't remember them, but you will see a [Parameters] button on the LH side of each build log to inspect what you ran with.
The Version Number plugin can provides greater flexibility, say you want to AutoIncrement and the "BUILD_NUMBER" option is too limiting, it offers a variable BUILDS_ALL_TIME, which can use the above defined variables or hard-coded constants to aggregate a version label and optionally control which it is incremented (eg: only increment on successful builds). eg:
[ X ] Prepare an environment for the run
Properties Content
FOO=bar
[ X ] Create a formatted version number
Environment Variable Name [ BUILD-${FOO}.${BUILDS_ALL_TIME} ]
Skip Builds worse than [ SUCCESS ]
Execute Shell step:
#!sh -
echo ${FOO}.${BUILD_NUMBER}
echo ${LABEL}
Output:
[Foo] $ sh - C:\Users\jenkins\AppData\Local\Temp\jenkins4160554383847615506.sh
bar.10
BUILD-bar.2

Accessing Jenkins-set environment variables from a Jenkins Plugin

If there is a Jenkins shell script build step, environment variables are set so that, for example, if you echo $WORKSPACE you see the current working directory. I am writing a custom plugin which also can execute shell commands but WORKSPACE is not set when this plugin executes.
I could see that Jenkins sets all those env variables prior to executing the shell commands the Jenkins project specifies so that they would not be already set for a custom plugin. If that is the case, it would seem like there is no way to access those env variables.
But if there is a way to obtain the values of the env variables that would be useful.

Jenkins - How to read the environment variables in groovy post build step

I am trying to read the environment variables in Groovy Postbuild step. I am able to read the values of parameters passed to builds but unable to read the values of parameters which are set in one of my Execute Windows batch command.
In one example of my Execute Windows batch command I do this:
SET custom_param=myValue
if I use ${custom_param} in other jenkins steps/jobs, it gets my value. So I am sure it has the value. I just can't get it in groovy script
I have tried followings to do so, none of them have worked:
manager.envVars['custom_param']
build.buildVariableResolver.resolve('custom_param')
build.getEnvironment(listener).get('custom_param')
Any help here would be great
(Assuming you're not running your script in groovy sandbox)
Try the bellow:
build = Thread.currentThread().executable
String jobName = build.project.getName()
job = Hudson.instance.getJob(jobName)
my_env_var = job.getLastBuild().getEnvironment()["YOUR_ENV_VAR"]
Groovy Post build step run as separate process. It has access to environment as normal JVM process.
You could use EnvInject plugin as a a build step. Subsequent steps in build will able to read this via normal environment access (System.env in your groovy script)
When you set some custom variables in your "Windows command batch" step, these variables are available only during this Jenkins step.
Once Jenkins move on the next step, your variables are lost...
If you want to set some variables permanently, you can try to use the SETX command:
What is the difference between setx and set in environment variables in Windows?

setting Environment from a file in Jenkins

My question is there any way to set up environment variables from a file during Jenkins build. I have a file called .xyz which has env variables.
I know there is a jenkins plugin but can we do it inside the execute shell box in jenkins??
I tried . .xyz but that doesn't work!
Thanks in advance
If you want to do it from inside Execute Shell step, you've got to write a shell script that will read the file line by line and add them to environment variables.
Assuming your properties file is of format:
var=value
A very basic version of the script would be:
while read line; do
export $line
done <your_props_file_name
The problem is, these variables will only be retained for the duration of that "Execute Shell" build step. Once you move to a different Build step or Post-build step (within the same job), they will be gone. This is Jenkins's cleanup by design.
That's why there is that EnvInject plugin, it takes care of maintaining the environment variables between the build steps.

Preserve environment variables in Jenkins between Windows build commands

I am trying to set up Jenkins to continually check out and build code and verify that the code is compilable.
Our build system works like this: we have several different .bat files that set up environment variables for different build configurations, and then we execute gmake to actually build the code.
When I created Jenkins job, in Build part of the job I set up two "Execute windows batch command" commands: one that calls the script to set up env. variables, and gmake to build it.
Problem is, when gmake step runs, all environment variables are forgotten. How can I prevent env. variables from being cleared?
Tx
What if you set it up to call only one bat file instead? That one file can then call the two you're currently calling with Jenkins.

Resources