Last active
          February 16, 2025 02:38 
        
      - 
      
- 
        Save SpotlightForBugs/b1666bc9fc2e1c03d17bdf10871ec902 to your computer and use it in GitHub Desktop. 
    Shell script to update a git repository from GitHub.
  
        
  
    
      This file contains hidden or 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
    
  
  
    
  | #!/bash | |
| #shell script to update a git repository from GitHub. | |
| #(C)2022-SpotlightForBugs - https://github.com/SpotlightForBugs/SpotlightForBugs | |
| #What is this script for? | |
| #welcome to the script that updates a git repository from GitHub, automatically. | |
| #This script is designed to be installed via crontab, so that it runs as a service. | |
| versionNumber="1" | |
| scriptPath="$(dirname "$(readlink -f "$0")")" | |
| ###parameters | |
| githubURL="github.com/author/Repo" #the URL of the GitHub repository | |
| localPath="/path/to/repo/" #the path to the local repository | |
| branchName="master" #the name of the branch to update | |
| accessToken="needs the scope repo and admin:repo_hook to work properly" #the access token to use for authentication (needs the scope repo and admin:repo_hook to work properly) | |
| installAsService="true" #set to true to install the script so that it runs as a service. Set to false to disable this option. | |
| ###end of parameters | |
| #if the file autoupdateRepoParameters.sh exists, then load it | |
| if [ -f "$scriptPath/autoupdateRepoParameters.sh" ]; then | |
| . "$scriptPath/autoupdateRepoParameters.sh" | |
| fi | |
| #before continuing, verify that the user has set the parameters correctly. | |
| #1 -check accessToken | |
| if [ "$accessToken" = "needs the scope repo and admin:repo_hook to work properly" ]; then | |
| echo "Please set the accessToken parameter in the script to a valid access token." | |
| exit 1 | |
| fi | |
| #1.1 -check accessToken's permissions | |
| if [ "$(curl -s -H "Authorization: token $accessToken" https://api.github.com/user | grep -o '"login": ".*"' | cut -d '"' -f 4)" = "null" ]; then | |
| echo "The accessToken parameter is not valid. Please set it to a valid access token." | |
| exit 1 | |
| fi | |
| #2 check path: is the path allowed to be used? | |
| #3 check url: can the repository be accessed with the given url and branch and the given access token? | |
| if [ "$(curl -s -H "Authorization: token $accessToken" https://api.github.com/repos/$githubURL/branches/$branchName | grep -o '"name": ".*"' | cut -d '"' -f 4)" = "null" ]; then | |
| echo "The githubURL or branchName parameter is not valid. Please set it to a valid url and branch." | |
| exit 1 | |
| fi | |
| #1. check if the repository exists | |
| if [ ! -d "$localPath" ]; then | |
| echo "Repository does not exist. Cloning..." | |
| git clone https://$accessToken@$githubURL.git $localPath | |
| echo "Repository cloned." | |
| exit 0 | |
| fi | |
| #2. check if the branch exists for the repository | |
| if [ "$(git -C $localPath branch -a | grep -o "remotes/origin/$branchName")" = "remotes/origin/$branchName" ]; then | |
| echo "Branch exists." | |
| else | |
| echo "Branch does not exist. Defaulting to the default branch of the Repo..." | |
| branchName="$(curl -s -H "Authorization: token $accessToken" https://api.github.com/repos/$githubURL | grep -o '"default_branch": ".*"' | cut -d '"' -f 4)" | |
| fi | |
| #3. update the repository | |
| git -C $localPath fetch origin "$branchName" | |
| git -C $localPath reset --hard FETCH_HEAD | |
| git -C $localPath clean -df | |
| echo "Repository updated." | |
| #4. update the submodules | |
| git -C $localPath submodule update --init --recursive | |
| echo "Submodules updated." | |
| #5. update the permissions | |
| chmod -R 755 $localPath | |
| echo "Permissions updated." | |
| #6 perform other tasks after the update | |
| #default: do nothing | |
| ###Server Restarts | |
| #flask: run app.py in the background | |
| #nohup python3 $localPath/app.py & | |
| #django: run manage.py in the background | |
| #nohup python3 $localPath/manage.py runserver & | |
| #mySQL: restart the server | |
| #systemctl restart mysql | |
| #nginx: restart the server | |
| #systemctl restart nginx | |
| #apache: restart the server | |
| #systemctl restart apache2 | |
| #php: restart the server | |
| #systemctl restart phpx.x-fpm (x.x = php version) | |
| #redis: restart the server | |
| #systemctl restart redis | |
| ##Database Updates/Restarts | |
| #mongodb: restart the server | |
| #systemctl restart mongodb | |
| #elasticsearch: restart the server | |
| #systemctl restart elasticsearch | |
| #postgresql: restart the server | |
| #systemctl restart postgresql | |
| #custom: run a custom command | |
| #Enter your custom command here, for example: echo "Hello World!" > $localPath/hello.txt OR make -C $localPath | |
| ###Logfile generation | |
| #create a log file | |
| #echo "Repository updated at $(date)" >> $localPath/update.log | |
| #7. install the script as a service if the user has set the installAsService parameter to true. | |
| #This will make the script run automatically when the server starts. | |
| #The Script will run in the background and will not be visible to the user. | |
| if [ "$installAsService" = "true" ]; then | |
| #run on startup using crontab. Automatically determnine the script's path which can be different depending on the user's setup. | |
| #check if the script is already installed as a service | |
| if [ "$(crontab -l | grep -o "autoupdateRepo.sh")" = "autoupdateRepo.sh" ]; then | |
| echo "The script is already installed as a service." | |
| else | |
| #install the script as a service | |
| echo "Installing the script as a service..." | |
| #determine the script's path | |
| scriptPath="$(dirname "$(readlink -f "$0")")" | |
| #add the script to crontab | |
| (crontab -l 2>/dev/null; echo "@reboot $scriptPath/autoupdateRepo.sh") | crontab - | |
| echo "The script has been installed as a service." | |
| fi | |
| fi | |
| #9 update this gist if a new version is available | |
| #backup this file to a temporary file in case the update fails. It will be named autoupdateRepo.sh-timeStamp.bak | |
| cp $0 $0-$(date +%s).bak | |
| #save the parameters to a file to be used by the update part of the script to load the parameters in the new file. | |
| #remove the contents of the old parameters file if it exists | |
| rm -f $scriptPath/autoupdateRepoParameters.sh | |
| touch $scriptPath/autoupdateRepoParameters.sh | |
| #save all the parameters to a file called autoupdateRepoParameters.sh | |
| echo "githubURL=\"$githubURL\"" > "$scriptPath/autoupdateRepoParameters.sh" | |
| echo "branchName=\"$branchName\"" >> "$scriptPath/autoupdateRepoParameters.sh" | |
| echo "installAsService=\"$installAsService\"" >> "$scriptPath/autoupdateRepoParameters.sh" | |
| echo "localPath=\"$localPath\"" >> "$scriptPath/autoupdateRepoParameters.sh" | |
| echo "optionWatch=\"$optionWatch\"" >> "$scriptPath/autoupdateRepoParameters.sh" | |
| echo "accessToken=\"$accessToken\"" >> "$scriptPath/autoupdateRepoParameters.sh" | |
| #print out the configuration in the terminal, showing a star for each character in the accessToken | |
| echo "Configuration:" | |
| echo "githubURL=\"$githubURL\"" | |
| echo "branchName=\"$branchName\"" | |
| echo "installAsService=\"$installAsService\"" | |
| echo "localPath=\"$localPath\"" | |
| echo "optionWatch=\"$optionWatch\"" | |
| echo "accessToken=\"$(printf "%0.s*" $(seq 1 ${#accessToken}))\"" | |
| echo "The script has been configured." | |
| #the id of the gist is "b1666bc9fc2e1c03d17bdf10871ec902" and the author is "SpotlightforBugs" | |
| curl -s "https://gist.githubusercontent.com/SpotlightforBugs/b1666bc9fc2e1c03d17bdf10871ec902/raw/" > $scriptPath/"autoupdateRepo.sh" | |
| ###This script is licensed under the GNU General Public License v3.0.### | |
| ###You can find a copy of the license at https://www.gnu.org/licenses/gpl-3.0.en.html### | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment