Despite of GitHub pages build automatically you could deploy only the static files to the branch gh-pages, the main purpose of do something like this is to run third party plugins; todo so, I created a script to help in the proccess of do it.
If you prefere download the script here
What this script does is:
Default names of the folders:
BUILD_FOLDER='_site'
PUSH_FOLDER='_site_ghpages'
- It creates a folder with the seted name in PUSH_FOLDER. / Clean it if the folder alredy exists;
- Change the directory to the PUSH_FOLDER (cd $PUSH_FOLDER);
- Clone only the remote branch gh-pages of GitHub to PUSH_FOLDER;
- Copy all the content of the BUILD_FOLDER to the PUSH_FOLDER;
- Add all the files of PUSH_FOLDER in the local branch gh-pages, commit and push to the remote gh-pages;
- Go back to the previouly directory.
How to use it:
First make sure of:
- Ignore the PUSH_FOLDER in the .gitignore file;
- 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 scirpt push with a 'Automatic commmit' 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