开发者

publish jekyll generated to gh-pages and not overwrite the .git in _site

开发者 https://www.devze.com 2023-04-08 08:48 出处:网络
I am using jekyll to publish a static site directly on gh-pages branch in Github. The issue I have is that every time I run

I am using jekyll to publish a static site directly on gh-pages branch in Github. The issue I have is that every time I run

$ jekyll --no-auto /Users/khinester/Sites/tzm/

this overwrites the .git directory and I have to recreate this:

$ git init-db
$ git add remote ..
$ git add .
$ git commit -a -m 'message'
$ git branch gh-pages && git checkout gh-pages
etc..
$ git push -f github gh-pages

basically i have the master branch containing the files needed to generate the blog and gh-pages branch which displays the actual blog.

and also note that i have to force push this.

it will be n开发者_如何学JAVAice to have also be able to version control the updates!

i have read the https://github.com/mojombo/jekyll/wiki/Deployment but this seems has more steps then what i do now.

is there a better way to do this or have i missing something.


Jekyll has a config array called keep_files. Everything in that array will be kept when Jekyll rebuilds the site.

Here's where it was added: https://github.com/mojombo/jekyll/pull/630. Searching the issues for keep_files will reveal more of its black magic.

.git and .svn files are added to keep_files by default so this shouldn't be a problem anymore.


I used Steven Penny's script to write this one, which out of the box is for Project Pages, not User Pages.

#!/bin/bash

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
cd $DIR

SELF=`basename $0`
SOURCE_BRANCH="master"
DEST_BRANCH="gh-pages"
TMP_DIR="tmp"

git checkout $SOURCE_BRANCH
jekyll build -d $TMP_DIR
git checkout $DEST_BRANCH
# This will remove previous files, which we may not want (e.g. CNAME)
# git rm -qr .
cp -r $TMP_DIR/. .
# Delete this script from the output
rm ./$SELF
rm -r $TMP_DIR
git add -A
git commit -m "Published updates"
# May not want to push straight away
# git push origin master
git checkout $SOURCE_BRANCH


I use the following shell script for committing a Hakyll generated site (in directory _site) to the gh-pages branch. The script:

  • Does not require you to switch branches... just run the script from master or whatever branch you're on.
  • Uses the main repository; it does not matter that jekyll clobbers the .git directory because... it isn't there!
  • Does nothing if nothing has changed!
  • Recognises new, previously untracked files (even when there are no changes to existing files)
  • Adds a new commit to the gh-pages branch, so you don't need to force push.
  • Includes a timestamp in the commit message

Code follows; update paths as necessary

export GIT_INDEX_FILE=$PWD/.git/index-deploy
export GIT_WORK_TREE=$PWD/_site
REF=refs/heads/gh-pages
git read-tree "$REF"
git add --all --intent-to-add
git diff --quiet && exit
git add --all
TREE=$(git write-tree)
COMMIT=$(git commit-tree "$TREE" -p "$REF" -m "snapshot $(date '+%y-%m-%d %H:%M')")
git update-ref "$REF" "$COMMIT"
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号