1

I have created a shell script file for pushing a git repository automatically every hour using crontab like as follows,

backup.sh

cd /home/user/share/my_project && git commit -a -m "hourli crontab backup 'date'"
cd /home/user/share/my_project && git push origin branch1
send mypassword\r
wait

The issue with this code is, the git is using ssh and every time we run this code using bash ./backup.sh is asking password in the terminal. Not accepting the password specified in the shell script.

Shijin TR
  • 7,516
  • 10
  • 55
  • 122
  • May you need to use a authentication token or something like that? – evolutionxbox May 09 '19 at 10:23
  • the repository is in an isolated local network, but 'send mypassword\r' is not working – Shijin TR May 09 '19 at 10:25
  • Just curious, Why are pushing every hour with no change? . Why are you going for this approach and not not for Jenkins or CI way ? – Akshay barahate May 09 '19 at 10:26
  • @Akshaybarahate We have an application hosted in one local server and few developers working on the same directly same time. Unfortunately, we don't have to create copies for the projects in our local for some reasons. So that we try to push the code every hour using the crontab. So that developers can't worry about the code backup and git . – Shijin TR May 09 '19 at 10:30
  • @Shijin that sounds like an easy way to get yourself into trouble... – evolutionxbox May 09 '19 at 10:34
  • I doubt you guys follow git workflow practice and merger request concept. [Workflow and Merger](https://www.atlassian.com/git/tutorials/comparing-workflows/feature-branch-workflow). What you are doing is an overhead of cron Job I feel. Instead adopting to best practice like the workflow and feature based branch and merging to master will create good team flow. Let me know you thoughts on this – Akshay barahate May 09 '19 at 10:36
  • Possible duplicate of [Is there a way to cache GitHub credentials for pushing commits?](https://stackoverflow.com/questions/5343068/is-there-a-way-to-cache-github-credentials-for-pushing-commits) – phd May 09 '19 at 13:16
  • https://stackoverflow.com/search?q=%5Bgit%5D+skip+password – phd May 09 '19 at 13:17
  • Did any of the answers lead to solving your problem? Consider accepting one that does! – Adam May 16 '19 at 18:15

1 Answers1

0

I agree with the comments that this isn't a best practice.

Ignoring that and hacking on anyway you should look into git-credential-store. Example from the docs;

$ git config credential.helper store
$ git push http://example.com/repo.git
Username: <type your username>
Password: <type your password>

[several days later]
$ git push http://example.com/repo.git
[your credentials are used automatically]

The credentials will be stored locally, unencrypted in ~/.git-credentials. If this is an acceptable trade-off then it'll work for you. Just ensure you understand the user context that the cron job is running under.

Adam
  • 4,180
  • 2
  • 27
  • 31