Last active
May 6, 2024 17:41
-
-
Save burgil/28154474441720e45e7e296375fc3383 to your computer and use it in GitHub Desktop.
Auto Update Linux Server When GitHub Repository Changes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
OWNER="burgil" | |
REPO_NAME="Example" | |
get_latest_commit() { | |
curl -s "https://api.github.com/repos/$OWNER/$REPO_NAME/commits/main" | grep '"sha"' | head -n 1 | cut -d '"' -f 4 | |
} | |
LAST_COMMIT=$(get_latest_commit) | |
while true; do | |
LATEST_COMMIT=$(get_latest_commit) | |
echo "LATEST COMMIT: $LATEST_COMMIT" | |
if [ "$LATEST_COMMIT" != "$LAST_COMMIT" ]; then | |
echo "Changes detected in the repository!" | |
LAST_COMMIT=$LATEST_COMMIT | |
fi | |
sleep 30 | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
OWNER="burgil" | |
REPO_NAME="Zeteor" | |
get_latest_commit() { | |
curl -s "https://api.github.com/repos/$OWNER/$REPO_NAME/commits/main" | grep '"sha"' | head -n 1 | cut -d '"' -f 4 | |
} | |
LAST_COMMIT=$(get_latest_commit) | |
while true; do | |
LATEST_COMMIT=$(get_latest_commit) | |
echo "LATEST COMMIT: $LATEST_COMMIT" | |
if [ "$LATEST_COMMIT" != "$LAST_COMMIT" ]; then | |
echo "Changes detected in the repository!" | |
LAST_COMMIT=$LATEST_COMMIT | |
echo "Restarting Website..." | |
ps aux | grep "MyMainWebsite" | grep -v grep | awk '{print $2}' | xargs -I{} kill -9 {} | |
# cd .. | |
echo "Fetching Updates..." | |
git fetch | |
echo "Pulling Updates..." | |
git pull | |
# cd website | |
echo "Starting Website..." | |
nohup npm start & | |
echo "Website Restarted!" | |
fi | |
sleep 30 | |
done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "example-updater", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"start": "node index.js /MyMainWebsite", | |
"dev": "nodemon index.js" | |
}, | |
"keywords": [], | |
"author": "Burgil", | |
"license": "", | |
"dependencies": { | |
"nodemon": "^3.1.0", | |
}, | |
"nodemonConfig": { | |
"ignore": [ | |
"example_db.json" | |
] | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ps aux | grep "MyMainWebsite" | grep -v grep | awk '{print $2}' | xargs -I{} kill -9 {} | |
cd .. | |
git fetch | |
git pull | |
cd website | |
nohup npm start & |
Author
burgil
commented
May 6, 2024
How to do it with a Private Github Repository?!
Today I am going to share with you the secret, my friend!
SSH? No need!
You can simply git clone
private repositories if you login with gh
, here is how to do it:
apt install gh
gh auth login
- copy the code
- open in the browser
https://github.com/login/device
when the error shows - enter the code
- enter enter enter:
? What account do you want to log into? GitHub.com
? What is your preferred protocol for Git operations? HTTPS
? Authenticate Git with your GitHub credentials? Yes
? How would you like to authenticate GitHub CLI? Login with a web browser
- git clone your private repo!
Mission Success! Git Colonized!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment