Self Updating Bash Script from Github

Shell script self update using git

This is the script I came up with:

#!/bin/bash

SCRIPT=$(readlink -f "$0")
SCRIPTPATH=$(dirname "$SCRIPT")
SCRIPTNAME="$0"
ARGS="$@"
BRANCH="Your_git_branch"

self_update() {
cd $SCRIPTPATH
git fetch

[ -n $(git diff --name-only origin/$BRANCH | grep $SCRIPTNAME) ] && {
echo "Found a new version of me, updating myself..."
git pull --force
git checkout $BRANCH
git pull --force
echo "Running the new version..."
exec "$SCRIPTNAME" "$@"

# Now exit this old instance
exit 1
}
echo "Already the latest version."
}

main() {
echo "Running"
}

self_update
main

How to update a local repository automatically with the remote?

I eventually created a solution for myself that is not EXACTLY what I was looking for but does solve the problem for me:

I just introduced to ALL my scripts in the repo a call to a function that just checks if the repo is in sync with the remote. If it is not, it does not let the scripts continue and the script aborts, forcing the users to pull from the remote before running the scripts. So this way, I know no one will be using an out-of-date version of my scripts.

Here is the script I used (I put it in a file called GlobalFunctions.sh within the repo with all the other bash scripts):

#!/bin/bash
#------------------------------------------------------
# GlobalFunctions.sh
# Description:
# This file is supposed to serve as a file for global bash script functions that could be used by multiple files.
#-------------------------------------------------------------

#verify that the repository containing the scripts is up-to-date
function CheckScriptsValidity()
{
# DIRECTORY WHERE THE SCRIPT IS LOCATED
SCRIPT_DIRECTORY="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"

# CURRENT DIRECTORY
ORIGINAL_DIRECTORY=$(pwd)

cd $SCRIPT_DIRECTORY

git fetch #update the branch origin/main
if [[ -z $(git diff origin/main..HEAD) ]] # if the current location (HEAD) is at the same location as the remote main branch
then
echo "Scripts Repo is up to date."
else
echo "Scripts Repo is not up to date with remote. Please pull Scripts repo from remote. Aborting..."
read -p "Press enter to continue..."
exit 0
fi

cd $ORIGINAL_DIRECTORY
}

CheckScriptsValidity #Verify that the repository containing the scripts is up-to-date

And within each bash script file I simply added this line on top:

source $(dirname "$0")/GlobalFunctions.sh   #Load script file so that we can call its functions (path defined based on this: https://stackoverflow.com/a/42101141/4441211)

Is there a way to auto update my github repo?

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".

Make a shell script to update 3 git repos

First, I recommend against using git pull. Instead, create a safer git up alias:

git config --global alias.up '!git remote update -p; git merge --ff-only @{u}'

See this answer for an explanation of git up.

Then you can safely script it:

#!/bin/sh
for repo in repo1 repo2 repo3 repo4; do
(cd "${repo}" && git checkout master && git up)
done


Related Topics



Leave a reply



Submit