How do I push jekyll _site directory to gh-pages branch, and leave the source in master?
Asked Answered
P

3

14

I have a basic jekyll site consisting of pages (not posts) but, because I wanted to sort the pages when I listed them, I had to use the Jekyll-Sort plugin (kinda weird sorting pages is not built in to jekyll).

Because I'm using a plugin, I can't leverage GitHub's auto jekylling. So I'd like to push the source code of the project to the master branch and just the _site directory to the gh-pages branch.

I can't figure out how to do this - I tried adding a git repo inside the _site directory to push that to gh-pages but every time I run jekyll it erases that entire directory and I lose the .git folder.

Any suggestions? Or a way to natively sort?

Pine answered 24/7, 2013 at 13:48 Comment(0)
I
13

A less painful solution:

  1. Checkout your branch, where your build-source is located (maybe src, or master)
  2. (Optional: Add _site to your .gitconfig, so it get's ignored, if not already done)
  3. Remove all content of the _site directory:

    $ rm -r _site/*

  4. Clone your repo's gh-pages branch into the _site directory:

    $ git clone -b gh-pages `git config remote.origin.url` _site

Final steps: Just let jekyll build, do commit & push:

$ jekyll build,

cd into _site:

$ cd _site,

target all files for commit:

$ git add -A,

commit them:

git commit -am 'Yeah. Built from subdir'

and push your site to GitHub-Pages:

git push.

Incommunicable answered 4/3, 2016 at 14:6 Comment(0)
K
1

I did this for a while myself with a shell script.

Solution 1.

Create a .gitignore that excludes the _site/ folder. Then have your shell script check whether you are on master, if so, add all changed files and commit them. Then move the _site/ folder to a temporary folder. Switch to the gh-pages branch and copy the temporary folder over. Add all and commit. Push both master & gh-pages branch.

Solution 2. Copy the contents of the _site/ folder to another repo that is an exact clone of the repo you are working with but checked out on the gh-pages branch. Then simply push the master branch from the source repo and the gh-pages branch from the other repo

Krystlekrystyna answered 27/7, 2013 at 8:26 Comment(2)
Both options feel so hacky and unclean I will not even try to employ them. There must be a better wayParagrapher
I'm not sure how much gh-pages have changed over the last few years, but I can't imagine they let you run plugins on the github servers. this means that you will need to push both the source as well as the generated page. And no matter how you do that, you'll want that in two branches. So yeah...Krystlekrystyna
S
0

I was looking for it too, so if you wanted to use a third party plugin that isn't supported in the current list of GitHub Pages, you'll have to build and push manually the static files to the remote branch gh-pages, letting it only with the static files. as they pointed in their page GitHub Docs

So to send the static files and let only the build content in the remote branch gh-pages do this:

  1. First make sure you already have the branch gh-pages in your GitHub repository;
  2. In your project create a folder with the name '_site_ghpages';
  3. Add it to your .gitigore file;
  4. In the Git bash/shell change the directory to the folder '_site_ghpages':
cd _site_ghpages
  1. Clone only the remote branch gh-pages of GitHub to the folder '_site_ghpages':
git clone --branch gh-pages `git config remote.origin.url` _site_ghpages
  1. Delete all the content if any;
  2. Copy all the content of the build folder '_site' and paste in '_site_ghpages' folder;
  3. in the folder _site_ghpages add and commit all the files:
  git add .
  git commit -m "My commit message"
  git push

  1. Go back to the previouly directory. cd ..

In this way each time you update your content, you should go to the folder '_site_ghpages'; clean it up; copy the content from the folder '_site'; paste in _site_ghpages, open the git bash in the folder '_site_ghpages'; add all the files, commit it, and finally push to the remote branch gh-pages.


Honestly all this process is a bit boring. So to workaround it I maked a script in bash that does all the above but automatically.

I put it in a repository so if you prefere you could check and download it here.


How to use the bash script :

First make sure of:

  1. Ignore the PUSH_FOLDER ('_site_ghpages') in the .gitignore file;
  2. Already have a branch called "gh-pages" created in your repository (After run the script this branch will have only the static files of the build folder).

To execute the script first you should allow it to run as a executable in your machine, to do so, with Bash/Git Bash run:

chmod +x push_ghpages.sh

After that just run the file calling:

. push_ghpages.sh

When called without argument the script push with a 'Automatic commit' message. So if you want to set your commit message just pass it as a argument, like this:

. push_ghpages.sh "Your commit message goes here."

If you have any problem while execute, just delete the PUSH_FOLDER, empty the remote gh-pages and let the script rebuild e push the content.

Also enter the PUSH_FOLDER cd my_push_folder_directory and run a git branch to check if it have ONLY the branch gh-pages, if it have a master branch or any other, delete it.

#! /bin/bash
# Author: [email protected]
# This is a script used to automatically deploy a Jekyll website/blog with third party plugins to GitHub Pages.

BUILD_FOLDER="_site";
PUSH_FOLDER="_site_ghpages"; 
COMMIT_MESSAGE=$1

#Remove all the content from the "PUSH_FOLDER".
function removeAllContentFromPushFolder(){
    rm -r $PUSH_FOLDER/*;
}

#Create the folder "PUSH_FOLDER".
function createFolderToPush(){
    mkdir $PUSH_FOLDER
}

#Copy all the content from the folder _site to PUSH_FOLDER.
function copySiteToFolder(){
    cp -r $BUILD_FOLDER/. $PUSH_FOLDER
}

#Clone only the branch "gh-pages" to the folder "PUSH_FOLDER". 
function cloneGhpages(){
    git clone --branch gh-pages `git config remote.origin.url` $PUSH_FOLDER
}

function prepareThePushFolder(){
    if [[ -d ./$PUSH_FOLDER ]]
    then
        #Remove all the content from the folder "PUSH_FOLDER".
        removeAllContentFromPushFolder
    else
        #Create the folder "PUSH_FOLDER" if it doesn't exist.
        createFolderToPush
        #Call the function that clone the branch "gh-pages" to the folder "PUSH_FOLDER". 
        cloneGhpages
        #Remove any prevous content from the folder "PUSH_FOLDER".
        removeAllContentFromPushFolder

    fi
    #Call the function that copy all the content from the folder _site to "PUSH_FOLDER".
    copySiteToFolder
}

function changeDirectoryToGhpages(){
    cd $PUSH_FOLDER
}

function setMessageCommit(){
    if ! [ "$COMMIT_MESSAGE" ]
    then
      COMMIT_MESSAGE='Automatic Commit'
    fi
}

function pushBranchGhpages(){
    git add .
    git commit -m "$COMMIT_MESSAGE"
    git push
}

function changeDirectoryBack(){
   cd ..
}

prepareThePushFolder
changeDirectoryToGhpages
setMessageCommit
pushBranchGhpages
changeDirectoryBack

Snuggle answered 14/5, 2022 at 1:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.