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

Do you really go into your Linux server every time you make a commit and manually restart it?
Or did you set up the most insane infrastructure in the world to do it?

Both methods are not the way.

Let me show you the way

This is the way:

  1. Simply add a custom keyword to your start script in the package.json for example:
  "scripts": {
    "start": "node http.js /MyMainWebsite",
  },
  1. Create a file named update.sh in your GitHub repository with the content of the complete-example.sh you can find above.
  2. Make sure to modify the OWNER and REPO_NAME to track the correct repository.
  3. Uncomment the lines and match the logic to your folder structure
  4. Fetch & Pull the repository (One last time) on your Linux server and give execution permission to the updater file you created, eg. chmod +x updater.sh
  5. Run the updater in the background (or optionally for the first time in the foreground):

Background:

nohup ./updater.sh &

Foreground:

./updater.sh

Now you don't have to use the following commands ever again: PS | KILL | GIT

@burgil
Copy link
Author

burgil commented May 6, 2024

image

@burgil
Copy link
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:

  1. apt install gh
  2. gh auth login
  3. copy the code
  4. open in the browser https://github.com/login/device when the error shows
  5. enter the code
  6. 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
  1. git clone your private repo!

Mission Success! Git Colonized!

@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