Middleman and Github pages
-
24-06-2021 - |
Question
I'm trying to create a static site using Middleman. The git repo master
has the source files. The static files are generated in the build
folder which is in .gitignore
. I have a branch gh-pages
for Github pages. How do I setup things such that the gh-pages
has contents of the build
folder of master
.
Thanks.
Solution
Looks like this gem provides an elegant solution:
OTHER TIPS
I've started using the same technique as Octopress uses, it works great for Middleman.
Basically I use two git repositories, one inside the root folder and one inside the build folder. The root repository pushes to the develop
branch on the GitHub remote and excludes the build
directory. The repository inside the build
directory pushes to the master
(or gh-pages
) branch of the same GitHub remote.
To automate the pushing of the new static pages, I use the following Rakefile:
desc "deploy build directory to github pages"
task :deploy do
puts "## Deploying branch to Github Pages "
cp_r ".nojekyll", "build/.nojekyll"
cd "build" do
system "git add ."
system "git add -u"
puts "\n## Commiting: Site updated at #{Time.now.utc}"
message = "Site updated at #{Time.now.utc}"
system "git commit -m \"#{message}\""
puts "\n## Pushing generated website"
system "git push origin master"
puts "\n## Github Pages deploy complete"
end
end
Another good gem is middleman-deploy. After you have installed it and configured everything, you can simply run
$ middleman deploy
and your build directory will be pushed to GitHub pages. You can specify which branch you push to in the config. I also have a blog post here regarding switching from Jekyll to GitHub pages and it talks a little about deployment.
I couldn't find a clean way of doing this. This is a script I've been using:
bundle exec middleman build
mv build /tmp/
git checkout gh-pages
git rm -rf .
cp -r /tmp/build/* .
git add .
git commit -m "Update site"
rm -rf /tmp/build
git push
git checkout master