Search code examples
githubjekyllgithub-pages

Using common Jekyll code for multiple github pages websites


I'm wanting to have both a user website and multiple project websites hosted on Github Pages i.e.

  • username.github.io
  • username.github.io/project1
  • username.github.io/project2

I'd like them to share a common set of jekyll files so it's easier to change themes, info, etc.. Has anyone managed to do this? Using subtree / submodule isn't too helpful because you cannot overwrite the source config option to change the location of the jekyll source files. Github Pages always forces the source to be in the root directory. You can change where _includes and _layouts reside but that's not too useful. Any advice or examples?


Solution

  • Use username.github.ui/base as your repository with your base templates. This shouldn't be a gh-page.

    This repository should contain a branch with your base templates. For the moment, I say you are using the master branch for that. Branch out and create a project1 and project2 branch in this repo.

    Every time you change your base templates, change them in the master branch and then merge the master branch into project1 and project2.

    Clone the base repo locally, and add project1 and project2 both as remotes.

    Then simply do a git push project1 project1:gh-pages to push your local project1 branch to the first projects gh-pages branch.

    This will still multiply the files, but it will mean that you can easily keep them in sync. Changing your base templates would be like this:

    1. git pull project1's gh-pages branch into the base repos project1 branch. Repeat for project2.
    2. Change the base templates in the appropriate branch in the base repo.
    3. Merge the branch into the project1 and project2 branches
    4. git push project1's branch into project1sgh-pages` branch. Repeat for #2.

    If you set up some hooks this can be automated fairly easy.