Skip to content

Instantly share code, notes, and snippets.

@burgil
Last active May 6, 2024 17:41
Show Gist options
  • Save burgil/28154474441720e45e7e296375fc3383 to your computer and use it in GitHub Desktop.
Save burgil/28154474441720e45e7e296375fc3383 to your computer and use it in GitHub Desktop.
Auto Update Linux Server When GitHub Repository Changes
#!/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
#!/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
{
"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"
]
}
}
ps aux | grep "MyMainWebsite" | grep -v grep | awk '{print $2}' | xargs -I{} kill -9 {}
cd ..
git fetch
git pull
cd website
nohup npm start &
@burgil
Copy link
Author

burgil commented May 6, 2024

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment