Saturday, November 19, 2022

How to sync your website via GitHub

This 'tutorial' is for my own convenience. Whenever I am launching a new web site I tend to repeat the same procedure. It's much easier to follow it if it's written down at one place. So here it is ... (no more looking at .bash_history and browser history to figure out what I did the last time!)

I am assuming that you have just created your dummy/hello world website, perhaps even by some tool or script (e.g. cPanel's 'new python app' or something similar). The steps you need to take now are the following:

  1. Create (private) project on GitHub.
  2. Import the created project in your preferred IDE.
  3. Clean your (remote) website directory.
    Delete all empty/unused directories, if any.
  4. Transfer all remaining files to your project's directory.
    Pick your preferred method. For me the easiest is compress/download/uncompress.
  5. Add 'usual' files to .gitignore:
    e.g. project files and directories, compiled binaries, server generated temp files, etc.
  6. Add files to git index.
  7. Commit and push. 

We are done with the first part. Since we won't be doing code changes on our web server, we'll configure it in a way that it can only pull from our repository. So, on our server:

  1. Generate public/private RSA key pair:
    1. Start ssh-keygen.
    2. Enter the file name, for example your project's name.
    3. We won't need the passphrase.
  2. Add deploy key to your GitHub repository:
    Under Settings, click on Deploy keys and  copy-paste your public key there.
  3. Authenticate from your server to GitHub via SSH:
    ssh -i ~/.ssh/private_key_file -T git@github.com
  4. Since neither the repository nor our destination directory are empty, we will execute the following command sequence (in destination directory!):
    git init
    git remote add origin git@github.com:username/project.git
    git pull
    git checkout master -f
    git branch --set-upstream-to origin/master

We are done. Now we can add the following bash script for easier pulling from our web server's directory:
#! /bin/bash

eval `ssh-agent -s`
ssh-add ~/.ssh/private_key_file
git pull

That would be all!
P.S.: Don't forget to change file permissions once you sync them!

No comments:

Post a Comment