I have my project running in ubuntu server and I did everything through the user myname. The project files, all related directories and static files used by my application belongs to the user myname.
Now I want to set up Jenkins Freestyle project for CI/CD in the ubuntu server, when I install Jenkins, a new jenkins user is created.
In the build step, when jenkins performs the job through Execute shell, it executes the shell commands through the jenkins user instead of the default myname user, which has No permissions to anything about the app. Therefore, it's impossible for Jenkins build to work:
- it cannot execute
git pull origin masterthroughjenkinsuser to pull the latest changes; - it cannot run the
venv/bin/python myapp.pyto start the application - even if i change the owner of
myapp.pyto thejenkinsuser, the running process cannot access any directories/storages/static files with thejenkinsuser. - So, I'll have to do
sudo chown -R jenkins:jenkins *on everything I've worked, everything that's related to my app.
This seems to be really silly. I must have missed something obvious...
What is a elegant way for Jenkins to work?
@Raman mentioned that I should run the commands with sudo -u myname; I tried so:
The Jenkins build Execute shell commands of the Freestyle project are defined as follows:
cd /home/john/jenkins_test/
whoami
sudo -u john whoami
sudo -u john git pull
The Jenkin build shows the following output:
[test] $ /bin/sh -xe /tmp/jenkins2232617598308237553.sh
+ cd /home/john/jenkins_test/
+ whoami
jenkins
+ sudo -u john whoami
sudo: no tty present and no askpass program specified
Build step 'Execute shell' marked build as failure
Finished: FAILURE