unable to use zsh/bash in Jenkins Execute Shell - ios

I am trying to run iOS builds in a remote Mac mini connected as an agent to Jenkins master running on Linux server. I have all the dependencies installed and env variables set up via .zshrc(zsh is the default shell in catalina and above).
When I run scripts(npm install, pod install and so on) through Execute Shell option in Jenkins Freestyle project, it throws npm command not found/ pod command not found. Even when I shebang with /bin/zsh at the start of script, it doesn't work.
Is there a way to switch shell to zsh/bash or import all configurations into sh in the jenkins build process?

Jenkins leverage the 'rc' file when it connects to the slave (via ssh) and load the env variable (This also means when you will change anything in the rc file you need to disconnect and reconnect the slave to load the changes). Now it depends on the default SHELL set for the User you used to connect the slave. You need to check that and align the env variable accordingly in the right rc file.
In the current situation, if you don't want to change anything you could also do a
source ~/.zshrc
at the start of your script. This will load the env vars defined in that file
One more way is to set the env variable for a node is under "Node Properties" in node configuration.

Related

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.

Env variable is not set correctly after update Jenkins version

I have Jenkins V2.7 with a freestyle project running on Windows 7,
One of the build steps is “Execute Windows batch command”, in this step I run some .exe which do some stuff and also set environment variable TEST to Z:\DATA
Recently I have updated Jenkins and plugins to V2.144 and now when I run the job the TEST variable is set to Z:DATA instead of Z:\DATA
Any idea what can cause this issue ?

Can Jenkins source .bashrc of associated user?

My Jenkins runs inside Tomcat which runs under user buildman, therefore all the jobs run under that user (in CentOS). Some of my jobs depend on environment variables, which I set in .bashrc. However, the Jenkins jobs do not see any of the variables set in that file even though that script is supposed to be sourced for non-login shells, such as what I would think Jenkins should be (?).
The workaround is simple: I just copy and paste all the variables from my .bashrc into the build command script in Jenkins. But that is not very elegant. Is there any way to get Jenkins to source the .bashrc of the user it runs under so that it gets its usual configuration without having to set it separately in each job?
Jenkins creates a temporary sh-script for each script section (at least when using a "classical" project - for the Pipeline approach I'm not sure). This temporary script is executed with sh. On most Linux systems this is a symlink to bash, this SO-post gives some insights.
Also according to the man-pages of bash invoking bash with sh "tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well."
This means the .bashrc is not interpreted at all. However you can try to source the bashrc for each shell-invocation...
So, I tried a few things and the only solutions that seems to work are:
have a shell script in your repo that uses bash
write a file, chmod it via sh and then run it
In both case, there needs to be an executable file with content like:
#!/usr/bin/env bash
...
Using sh """ bash -c "...." """" doesn't seem to work
When my Jenkins agent launches by SSH on redhat linux, I see it does print environment variables defined in .bashrc.
My problem with not seeing changes to .bashrc was because I needed to relaunch the agent, so it picked up the change.
I have found a command that works for me
In .profile, .bashrc, etc.:
export MY_BASH_VAR=123
In Execute Shell:
VAR=$(bash -c "source ~/.profile && echo \$MY_BASH_VAR")
echo $VAR
Will print 123 in the output console when the job builds

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.

Run Jenkins ant build within special shell environment

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.

Resources