git push using crontab every hour prompting password - linux

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.

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.

Related

Cannot automatically push commits from script

I used the following script (autoupdate.sh) to update my git repository automatically using SSH whenever I make a change to the local repository in raspberry pi 3B.
#!/usr/bin/env bash
DATADIR="/home/pi/data"
cd $DATADIR
if [[ -n $(git status -s) ]]; then
echo "Changed found. Pushing changes..."
git add -A && git commit -m "$1: Update files" && git push origin main
else
echo "No changes found. Skip pushing."
fi
Then I call a script measurement.sh that calls the above script whenever the internet is connected ( I used 4G dongle USB). Something like
...
cd ~/data; bash autoupdate.sh $DATE
...
However, when I run sudo bash measurement.sh it encountered the errors (It has made a commit but not push). Without sudo it works fine.
Permission denied(public key)
...
I checked from GitHub document https://docs.github.com/en/github/authenticating-to-github/troubleshooting-ssh/error-permission-denied-publickey by regenerating the ssh key as well as verified the public key but it did not solve at all. When I pushed commits in a separate terminal it works fines so I do not think the issue relates to the SSH key. I doubt that to run the script successfully it with sudo, the SSH keygen must also be generated with sudo at first.
What could be the reasons for it?
Without sudo it works fine.
So why use sudo in the first place.
As commented, using sudo alone means using commands as root.
At least, a sudo -u <auser> would means the ~ in cd ~/data would be resolved by the appropriate /home/auser, instead of /root.

Is there a way to auto update my github repo?

I have a website running on cloud server. Can I link the related files to my github repository. So whenever I make any changes to my website, it get auto updated in my github repository?
Assuming you have your cloud server running an OS that support bash script, add this file to your repository.
Let's say your files are located in /home/username/server and we name the file below /home/username/server/AUTOUPDATE.
#!/usr/bin/env bash
cd $(dirname ${BASH_SOURCE[0]})
if [[ -n $(git status -s) ]]; then
echo "Changes found. Pushing changes..."
git add -A && git commit -m 'update' && git push
else
echo "No changes found. Skip pushing."
fi
Then, add a scheduled task like crontab to run this script as frequent as you want your github to be updated. It will check if there is any changes first and only commit and push all changes if there is any changes.
This will run every the script every second.
*/60 * * * * /home/username/server/AUTOUPDATE
Don't forget to give this file execute permission with chmod +x /home/username/server/AUTOUPDATE
This will always push the changes with the commit message of "update".

Git Auto Commit Daily

Could I do take commit my changes in a directory by a daily routine? Say, In every 12 AM at early morning, It should commit all the changes in that directory automatically? Is it possible in git? I get some answers for auto commit for every changes. But I want it for daily once commit.
If you simply want to commit ALL changes every morning at 12 AM, you can do this using a cronjob.
Assuming that you are using a linux distribution with bash, you can write a bash script that does the commit
#!/bin/bash
cd <git directory> && git add -A && git commit * --allow-empty-message -m ''
Then you can place this cron job in /etc/cron.d/
0 0 * * * <username> /bin/bash <script location>
If you intend to run this as your own user only then you can instead add it to your personal crontab interactively by running
crontab -e

GitLab - Git pull command with username and password in CI

I have configured my CI pipeline to remote via SSH into a server and update the git repository there with the latest changes. However it fails. Because when the CI runs the git pull command, the response is : "could not read Username for 'https://gitlab.com' "
Is there a way to run git pull which includes username and password in one single command?
I know the best way is to add the SSH key into gitlab variables to avoid asking for username and password, but that didn't work for me either. so my only option would be to provide username and password.
My gitlab-ci.yml file is like below:
deploy_staging:
stage: deploy
before_script:
- mkdir -p ~/.ssh
- echo -e "$DEPLOY_KEY" > ~/.ssh/id_rsa
- chmod 600 ~/.ssh/id_rsa
- '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
script:
- ssh ubuntu#example.com "
cd my_Project &&
git pull
"
Your order is incorrect. You add the ssh deploy key to the environment executing your CI job when you should add it inside the example.com environment executing the pull command.
It's like preparing a room with a chair and table and then entering the next room and expect you can sit down on the chair from the other room.
You should follow the instructions for Deploy keys and add the public key from the ubuntu#example.com user to your project deploy keys.
This should be all to make the last bit work.
Note:
Your comment about: echo -e "$DEPLOY_KEY" > ~/.ssh/id_rsa; ssh ubuntu#example.com" cd myProject && git pull doesn't change the order, you still add the deploy_key to your current environment and then enter another one through ssh.
I tried very hard by following every single step that is mentioned by gitlab official guide here as well as #Stefan's suggestion, and still I wasn't able to run the git pull command on the remote server.
But I could connect to the remote server via SSH.
So, I end up storing the gitlab credential on the server to avoid interrupting the CI execution.
$ git config credential.helper store
$ git push http://example.com/repo.git
Username: <username>
Password: <password>
Simply use the below command to pull repo without credentials on GitLab CI
git pull ${CI_REPOSITORY_URL}
CI_REPOSITORY_URL -- is GitLab predefined environment url

git alias not receiving parameters

I've been trying to create a new alias that send files to staging area, and at the same time it commit with a message.
I've tried this:
git config --global alias.stagecomm '!git add -A && git commit -m $1'
When I try to run:
git stagecomm "Commit"
It says that it didn't match any files known to git.
Try with a sh: edit your git config --global --edit and type:
!sh -c 'git add -A && git commit -m $1'
Another option: define a script (even on Windows) without extension, called git-stagecomm
In it, put your commands:
git add -A
git commit -m $1
If that script is in your path, you will be able to call it with git stagecomm "mymessage"
Any script called git-xxx will be executed by the git bash, as git xxx.
No alias needed there.
That being said, you could also type (without alias)
git commit -am "My message"
See git commit man page.
-a
--all
Tell the command to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected.
That would not take new files though.

Resources