1

In my .bashrc I have the following alias setup:

alias sitebuild='bundle exec jekyll build; git checkout gh-pages; git rm * && mkdir temp && mv * temp/ && mv temp/_site/* . && rm -rf temp && touch .nojekyll; git status; git add .; git commit -am "update"; git push --all origin'

A bit of explanation as to why I want to do this: I'm building a site using Jekyll and gh-pages, except that I've started using plugins not supported by GitHub and so I have to build the site locally and push it to the repo (instead of letting GH build the site for me remotely).

My system is the following: I work on my site on branch static_build, make my commits and build _site/ (containing all the final HTML). I then need to copy the contents of static_build:_site/ over to gh-pages:_site/ and push that and that only.

As far as I understand, the above command sitebuild should do that for me (and indeed it does when I run every argument separately in the shell); yet, it doesn't.

Any idea why?


EDIT: I have a feeling it might have to do with

mv * temp/

which, when I run every command individually, outputs

mv: rename temp to temp/temp: Invalid argument

It seems that in this case the terminal complains but still complies (ls shows that there is only temp left in the directory), but maybe this is no longer true when the alias is run?

EDIT 2: using instead

mv `ls -A | grep -v temp` ./temp

resolves the error of moving the folder inside itself, but not the main issue.

11
  • you probably want && instead of ; - in case one of your component commands fails, you probably don't want to continue.
    – meatspace
    Jan 27, 2017 at 18:46
  • 1
    Have you tried executing all commands as a single line (with the semi-colons and all) in an interactive shell, exactly like they appear in the whole alias? Maybe this would fail too, and help you pinpoint where the problem is.
    – Fred
    Jan 27, 2017 at 18:47
  • @Fred see the edit :/
    – Demosthene
    Jan 27, 2017 at 18:57
  • Are you in the right directory when executing the alias? There is no cd in your alias. Please note that the error you are listing for mv is possibly due to the fact that a directory cannot be moved inside itself.
    – Fred
    Jan 27, 2017 at 19:01
  • 2
    Probably unrelated, but you should really be using a function instead of an alias.
    – chepner
    Jan 27, 2017 at 19:04

2 Answers 2

1

As pointed out in the comments, a problem (if not the problem) is the attempt to move temp into itself. One way to avoid that (and replacing your alias with a shell function) is to make your temporary directory something that * won't match. Assuming you are not using bash's dotglob option:

sitebuild () {
    bundle exec jekyll build
    git checkout gh-pages
    git rm * -r --ignore-unmatch &&
      mkdir .temp && mv * .temp/ && mv .temp/_site/* . && rm -rf .temp &&
      touch .nojekyll
    git status
    git add .
    git commit -am "update"
    git push --all origin
}
2
  • Sorry, this is still not working. I think the directory moving and deleting is not happening properly. That might actually be caused by git rm *...
    – Demosthene
    Jan 27, 2017 at 19:50
  • Yup, 'twas git rm *. In fact, I needed to do git rm -r --ignore-unmatch * to both recursively remove the files and not crash (as was the case) when trying to git rm a file that hadn't been git add (such as the whole _site directory...). Edit your answer accordingly and I'll accept it :)
    – Demosthene
    Jan 27, 2017 at 20:07
0

[automatically] building a site using Jekyll and gh-pages (...) locally and push it to the repo

OP temp issue

first I think that the use of temp itself, is a susceptible source of problems

mv * .temp/               
&& mv .temp/_site/* .        # keep the generated _site folder
&& rm -rf .temp              # and remove ALL the jekyll sources!
&& touch .nojekyll`

running the above command from the source folder will remove all the sources, needed to rebuild the site.

General solution

  1. build from the source folder: ./
  2. go to destination folder: _site/, and push its content to your deploy_branch: gh-pages
  3. optionally, go back to source folder, source branch: static_build

with the following command:

# 1
bundle exec jekyll build
# 2
&& cd _site
&& git checkout -b gh-pages
&& touch .nojekyll
&& git add . && git commit -am "update"
&& git push origin gh-pages
# 3
&& cd ../ && git checkout -b static_build

(aside: the command above should be one-liner, remove the #, and remove line break or escape it with \ to make it work)

for a detailed explanation of How to configure GitHub to use non-supported Jekyll site plugins, check this SO answer

+ automate the deployment process with Rake.

rake is a simple ruby build program, that will give you more control over the building tasks. so you can exit early in case of error, or don't update if the git repo is clean.

  • add a Rakefile to the main folder
  • define your tasks inside it (eg. deploy task )
  • deploy by running rake deploy or just rake if you defined "deploy" as default task.

e.g a simple Rakefile that may help you doing the above.

# == Helpers ======================================

# return `false` in case of error
#        `nil`   if git output doesn't contain "clean"
#        "clean" (a truthy value) if it contains it
def clean?
  puts status = `git status`
  clean = ($? == 0) && status.match(/clean/)
end

# == Tasks ========================================

task :build do
  system "bundle exec jekyll build"
end

task :deploy => [:build] do

  Dir.chdir("_site") do

    system "git checkout -b gh-pages"

    exit if clean? == false
    unless clean?
      system "git add . && git commit -am 'update'"
    end

    system "git push --all origin"
  end
end

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.