Why the environment variables shown by shell and jenkins different? - jenkins

I found the environment variables shown by shell and jenkins is different. When I see $PATH by echo, it shows as follows;
# cat /etc/passwd | grep jenkins
jenkins:x:998:997:Jenkins Continuous Integration Server:/var/lib/jenkins:/bin/bash
# su jenkins
bash-4.2$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin
However when I exec "echo $PATH" on Jenkins by (Build -> Execute shell), console log shows as follows;
[workspace] $ /bin/sh -xe /tmp/hudson6923847933544830986.sh
+ echo /sbin:/usr/sbin:/bin:/usr/bin
/sbin:/usr/sbin:/bin:/usr/bin
Finished: SUCCESS
Not only $PATH but many other variables are also different, but $PATH is important to execute command and I don't understand why they are not the same. Do you have any idea?

Maybe your user of shell and user who is building the job are not same.
run $ whoami command in your shell and jenkins (Build -> Execute shell).
You can also set PATH variable in Build -> Execute shell section or jenkins -> Manage Jenkins -> configure System -> Global properties section. check the Environment variables then Name: PATH, value: $PATH:/usr/local/sbin.

Related

How to open another cmd terminal via jenkins service

I want to open another cmd terminal GUI via jenkins service, but I dont know how to do it.
in test.bat file, this file will run hello.bat in another cmd terminal
echo "testing ..."
start /min cmd /c hello.bat
in hello.bat file
echo "I see you 1"
echo "I see you 2"
echo "I see you 3"
ping 127.0.0.1 -n 8 > nul
I run test.bat manually and it works as expected, it trigger the test.bat in another cmd terminal.
But when I try to run test.bat from jenkins, I cannot see another cmd terminal GUI that running hello.bat
Below is the screenshot to prove it is running successful
but there is no another cmd terminal GUI is pop up during jenkins run in the jenkins agent PC
Below is my jenkins service setting, I tested with both Log on option, one is logon as user account, one as "Local System account" and tick the "allow service to interreact with desktop", both fail the expectation.
Please guide me on this issue, thank you !
You can use the Start a program build step in Jenkins and specify cmd.exe as the program to run.
Or you can use the bat step and specify cmd.exe as the command to run if you are using Jenkins pipelines as below:
pipeline {
stages {
stage('Open Command Prompt') {
steps {
bat 'cmd.exe'
bat 'echo Hello from the new command prompt window!'
}
}
}
}
you can read more about the bat command here:
https://www.jenkins.io/doc/pipeline/steps/workflow-durable-task-step/#bat-execute-windows-batch-command

Jenkins docker container simply hangs and never executes steps

I'm trying to run a Python image in Jenkins to perform a series of unit tests with pytest, but I'm getting some strange behavior with Docker.
My Jenkinsfile pipeline is
agent {
docker { image 'python:3.6-jessie' }
}
stages {
stage('Run tests') {
steps {
withCredentials([
string(credentialsId: 'a-secret', variable: 'A_SECRET')
{
sh label: "Install dependencies", script: 'pip install -r requirements.txt'
sh label: 'Execute tests', script: "pytest mytests.py"
}
}
}
}
However, when I run the pipeline, Docker appears to be executing a very long instruction (with significantly more -e environment variables than I defined as credentials?), followed by cat.
The build then simply hangs and never finishes:
Jenkins does not seem to be running inside a container
$ docker run -t -d -u 996:994
-w /var/lib/jenkins/workspace/myproject
-v /var/lib/jenkins/workspace/myproject:/var/lib/jenkins/workspace/myproject:rw,z
-v /var/lib/jenkins/workspace/myproject#tmp:/var/lib/jenkins/workspace/myproject#tmp:rw,z
-e ******** -e ******** python:3.6-jessie cat
When I SSH into my instance and run docker ps, I see
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
240d00459d92 python:3.6-jessie "cat" About a minute ago Up About a minute kind_wright
Why is Jenkins running cat? Why does Jenkins say I am not running inside a container, when it has clearly created a container for me? And most importantly, why are my pip install -r requirements and other steps not executing?
I finally figured this out. If you have empty global environment variables in your Jenkins configuration, it appears that you'll get a malformed docker run command since Jenkins will write the command, with your empty string environment variable, as docker run -e some_env_var=some_value -e = ...
This will cause the container to simply hang.
A telltale sign that this is happening is you'll get the error message:
invalid argument "=" for "-e, --env" flag: invalid environment variable: =
This is initially difficult to diagnose since Jenkins (rightfully) hides your actual credentials with ***, so the empty environment strings do not show up as empty.
You need to check your Jenkins global configuration and make sure you don't have any empty environment variables accidentally defined:
If these exist, you need to delete them and rerun.

Environment variable in GitBash not set

I'm trying to set the environment variable within my mingw gitbash (windows7-x64) via a small bash script. But it doesn't get set, only if I execute it manually.
contents of dev.bash
schwat#AACarrier MINGW64 ~/Documents/test
$ cat dev.bsh
#!/usr/bin/env sh
export KUBECONFIG="/c/Users/schwat/Documents/test/.kube/dev.kubecfg"
kubectl config set-context dev --cluster=kubernetes --namespace=dev --user=admin
kubectl config use-context dev
echo "Connected to ENV:DEV"
executed dev.bsh and echo of $KUBECONFIG
schwat#AACarrier MINGW64 ~/Documents/test
$ ./dev.bsh
Context "dev" modified.
Switched to context "dev".
Connected to ENV:DEV
schwat#AACarrier MINGW64 ~/Documents/test
$ echo $KUBECONFIG
exporting KUBECONFIG manually and echo of $KUBECONFIG
schwat#AACarrier MINGW64 ~/Documents/test
$ export KUBECONFIG="/c/Users/schwat/Documents/test/.kube/dev.kubecfg"
schwat#AACarrier MINGW64 ~/Documents/test
$ echo $KUBECONFIG
/c/Users/schwat/Documents/test/.kube/dev.kubecfg
Any idea what's wrong here? (not a duplicate of: Set an environment variable in git bash)
I see two main points in your script:
First you are using sh instead of bash in your script
#!/usr/bin/env sh
#!/usr/bin/env bash
And the second point that I see is related to the understanding of the export in a script.
When you execute a script a new process is created so the variables you create and export is available to this new process and to all possibles sub-process, and not to the parent process, in this case the shell which you call your script.
So, you variable is probably being create but when the script finish it is destroyed and you cannot see it anymore.
Hope it helps!

How to set environment variables in Codenvy terminal

I'm using Codenvy to install golang and as part of the process I'm setting environment variables. I can set the environment variables just fine during the docker build process, but when I start the resulting Codenvy terminal the environment variables aren't set. How can I have the environment variables that are set in the dockerfile be present in the resulting terminal?
As an example if I take this dockerfile:
FROM codenvy/python34
ENV GOPATH /tmp/application/gopath
ENV PATH $GOPATH:$GOPATH/bin:$PATH
CMD echo $PATH && sleep 1h
...then in the docker build output I see
[STDOUT] /tmp/application/gopath:/tmp/application/gopath/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
...but when I open the terminal and look at the $PATH I see...
user#6ec34a856f91:~$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/games
The answer was sent to me from the Codenvy Google Group...you need to add lines to your /home/user/.bashrc file. This gets run when your terminal starts.
RUN echo "export GOPATH=$GOPATH" >> /home/user/.bashrc
RUN echo "export PATH=$GOPATH/bin:$PATH" >> /home/user/.bashrc

How can I start a bash login shell in jenkins pipeline (formerly known as workflow)?

I am just starting to convert my Jenkins jobs into the new Jenkins Pipeline(workflow) tool, and I'm having trouble getting the sh command to use a bash login shell.
I've tried
sh '''
#!/bin/bash -l
echo $0
'''
but the echo $0 command is always being executed in an interactive shell, rather then a bash login shell.
#izzekil is right!!!! Thank you so much!
So to elaborate a little bit about what is going on. I used sh with ''' , which indicates a multiple line script. HOWEVER, the resulting shell script that gets dumped on to the jenkins node will be one line down, rather then the first line. So I was able to fix this with this
sh '''#!/bin/bash -l
echo $0
# more stuff I needed to do,
# like use rvm, which doesn't work with shell, it needs bash.
'''

Resources