0

I've been trying to push commits created with a simple shell script:

cd $dir
git add . && git commit -m "Test commit" &&
git push --all origin

The script does the job perfectly. Yet Crontab is stuck at making commits locally and Jenkins is getting 'Permission denied' when accessing a local git folder even if I assign Jenkins a group that owns the folder.

I tried Jenkins jobs with Execute shell either with the code or the path to the script. Any help would much appreciated.

  • You have to add git creadentials to git, e.g. as shown here (https://www.geeksforgeeks.org/how-to-add-git-credentials-in-jenkins/) and use this to push commits. – m19v Oct 18 '22 at 13:56
  • It will break on first merge conflict – mugiseyebrows Oct 18 '22 at 14:32
  • Added git creds as advised. Still permission denied when cd to a directory and fatal from git (not a git repo) as well. Any plugin to push commits available? – Harrier Panels Oct 18 '22 at 15:14

2 Answers2

0

One thing to note is that Jenkins goes back to the original workspace to run each command. So when you run cd $dir, the script will switch to $dir, but once you start the git add... command, it will go back to your workspace directory. To prevent this, either chain the commands together:

cd $dir && git add . && git commit -m "Test commit" && git push --all origin

or use dir to wrap the git commands so that they always run in that specific directory.

M B
  • 2,700
  • 2
  • 15
  • 20
  • Chaining does not work either. Do I need a jenkinsfile to use dir? – Harrier Panels Oct 31 '22 at 20:33
  • @HarrierPanels Yes, `dir` works on pipeline as code. But first look at why chaining doesn't work. Add a `pwd` command after `cd $dir` and see if you are in the correct directory. If so, then the problem is something else. – M B Nov 01 '22 at 01:48
  • cd $dir gets Permission denied – Harrier Panels Nov 01 '22 at 10:55
  • Solved for cron. Created a hook to push automatically after a commit is done by cron, the solution is here: https://stackoverflow.com/questions/7925850/how-can-i-automatically-push-after-committing-in-git Now gonna check it with jenkins. – Harrier Panels Nov 02 '22 at 21:34
  • Added a local node to Jenkins. It now commits an pushes by shell commands, no hook or plugins used. Solved! – Harrier Panels Nov 04 '22 at 15:30
  • @HarrierPanels Consider answering your own question with detailed information on how you fixed it for anyone else who comes upon this problem – M B Nov 05 '22 at 04:57
0

As advised by @M B summarizing:

Crontab:

For the cron commits to be pushed automatically:

Add a hook file to your git directory: How can I automatically push after committing in Git?

Jenkins:

To commit and push automatically add a node. I also added to a sudoers file by sudo visudo:

jenkins ALL=(ALL) NOPASSWD: ALL
%sudo    ALL=(ALL:ALL) ALL

I have also assigned a safe directory:

git config --global --add safe.directory /path/to/your/git/dir

EDIT: Just tested with Amazon EC2 Plugin, after cloning the repo on an EC2 instance use:

git remote remove origin
git remote add origin https://ghp_TOKEN@github.com/USER/REPO.git
git add . && git commit -m "COMMIT" && git push --all origin

And these 3 also work for Crontab and Terraform (no other settings needed).

Hope this helps.