Dr Jekyll and Mister --safe Github jekyll, github, bash, git

Setup a blog with Jekyll on Github. Back. Again.

So you’ve got a nice simple Github pages. Missing some tag ? No problem, check the Jekyll plugins directory, tape one and install it. jekyll serve --port 80 locally (i’m on Jekyll 1 prerelease) and all it’s fine ! Simple blog, with simple tag management.

One git push origin remote and … c’est le bordel, as we says here … First check my code … but nothing seems wrong. Monkey-patch the plugin … nothing wrong again …

Digging Google : Github run his github page with -- safe : no plugins are allowed to run on Github page !!

Solution : static site … generated dynamically

Finally have found the solution with http://edhedges.com/blog/2012/07/30/jekyll-with-plugins-hosted-on-github-pages/ : keep Git with 2 different structures,

Step by Step :

      git checkout -b dev
      
      jekyll build
      
      git commit-tree dev^{tree}:_site -m 'message'
      > COMMIT_ID
      

and check your log

      git lg COMMIT_ID 
      
      git update-ref refs/heads/master COMMIT_ID
      
      git checkout master
      
      git push -f origin master
      

And that’s it !

One for all

Ok, do all of this in just one step from the dev branch (after running jekyll) :

    git update-ref refs/heads/master $(echo 'Add commit message here!' | git commit-tree dev^{tree}:_site -p $(cat .git/refs/heads/master))
    

Aliasing

Easing the hacker life :)

So, in [alias] section of .gitconfig

    devtree = "!sh -c 'git update-ref refs/heads/master $(echo \"Transfert\" | git commit-tree dev^{tree}:_site -p $(cat .git/refs/heads/master))'"

    ### in quick and dirty way, a shell alias : 

    alias trf="jekyll build && git add . && git commit -m 'post : upd' && git devtree && git co master && git push origin master && git co dev"
    

Now, after commiting my work, I have just to launch a trf command for publishing on Github :)

In so quick & dirty way : a little bash script

Feel free to comment / improve it ! In short, use your dev for working and master only for pushing to Github. Gist code

    #!/bin/bash  


    echo "------- Transfer statics pages to Github (make sure jekyll build ok, and git branch dev commited) ---"

    current_branch=$(git branch | grep '*' | cut -c 3-)
    echo -e "-- \033[1;33mGit\033[0m    : Current branch :\033[0;31m $current_branch\033[0m"

    if [ $current_branch != "dev" ]; then
      echo -e "-- \033[0;31mERROR : Branch must be dev \033[0m"
      echo "------- Finished"
      exit 0;
    fi

    echo -e "-- \033[0;32mJekyll\033[0m : Build"
    jekyll build

    echo -e "-- \033[0;33mGit\033[0m    : add ."
    git add .

    echo -e "-- \033[0;34mUSER\033[0m   : enter the commit message for dev branch : "
    read dev_commit_message

    echo -e "-- \033[0;33mGit\033[0m    : commit dev "
    git commit -m "$dev_commit_message"


    master_commit_id=$(cat .git/refs/heads/master)
    echo -e "-- \033[0;33mGit\033[0m    : get the last COMMIT_ID of master branch :\033[0;31m ${master_commit_id:0:7}\033[0m"



    commit_tree_id=$(git commit-tree dev^{tree}:_site -p $master_commit_id -m '$dev_commit_message')
    echo -e "-- \033[0;33mGit\033[0m    : commit-tree _site, id :\033[0;31m ${commit_tree_id:0:7} \033[0m"

    new_master_commit_id=$(git update-ref refs/heads/master $commit_tree_id)
    echo -e "-- \033[0;33mGit\033[0m    : update-ref, new master id  : \033[0;31m ${new_master_commit_id:0:7} \033[0m"


    echo -e "-- \033[0;33mGit\033[0m    : checkout master"
    git checkout master

    echo -e "-- \033[0;33mGit\033[0m    : push origin master to github"
    git push origin master 

    echo -e "-- \033[0;33mGit\033[0m    : checkout dev"
    git checkout dev 

    echo "------- Finished "

    

Don’t hesitate to improve it !


comments powered by Disqus