Forked from inspiretk/git auto deploy from local machine to remote server
          
        
    
          Created
          June 25, 2023 03:39 
        
      - 
      
- 
        Save VoVanToan26/b8ad6b7b1dcc4fd91c1cebcc09c700f8 to your computer and use it in GitHub Desktop. 
    git auto deploy from local machine to remote server
  
        
  
    
      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
    
  
  
    
  | log to your remote server root user | |
| sudo adduser user sudo | |
| ssh [email protected] | |
| mkdir ~/deploy-folder | |
| git init --bare ~/project.git | |
| cd ~/project.git/hooks | |
| nano post-receive | |
| copy/paste the bash script #!/bin/bash | |
| Copy and paste below script | |
| #!/bin/bash | |
| while read oldrev newrev ref | |
| do | |
| # only checking out the master (or whatever branch you would like to deploy) | |
| if [[ $ref =~ .*/master$ ]]; | |
| then | |
| echo "Master ref received. Deploying master branch to production..." | |
| git --work-tree=/home/inspire/deploy-folder/ --git-dir=/home/inspire/repo.git/ checkout -f | |
| else | |
| echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server." | |
| fi | |
| done | |
| # end of file | |
| chmod +x post-receive | |
| type ls [you'll see post-receive file green meaning it's executable now] | |
| type pwd [make sure your file directory is same as your script] | |
| go to your local computer, and go to the folder you want to git. Use git bash program, and go to the folder if you're on windows | |
| cd ~/path/to/working-copy/ | |
| git init | |
| git remote add production [email protected]:/home/user/project.git | |
| now make a file, add it to your git, commit it and push it to production server | |
| nano test.txt [then add some text, ctrl x and save it] | |
| git add . | |
| git commit -m 'test1' | |
| git push production master | |
| now go to your server and go to your deploy folder, you'll see test.txt file in there | |
  
    
      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
    
  
  
    
  | #!/bin/bash | |
| while read oldrev newrev ref | |
| do | |
| # only checking out the master (or whatever branch you would like to deploy) | |
| if [[ $ref =~ .*/master$ ]]; | |
| then | |
| echo "Master ref received. Deploying master branch to production..." | |
| git --work-tree=/home/inspire/deploy-folder/ --git-dir=/home/inspire/repo.git/ checkout -f | |
| else | |
| echo "Ref $ref successfully received. Doing nothing: only the master branch may be deployed on this server." | |
| fi | |
| done | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment