未知数据源 2024年11月26日
Pelican and GitHub Pages workflow
index_new5.html
../../../zaker_core/zaker_tpl_static/wap/tpl_guoji1.html

 

本文介绍了作者如何使用Pelican静态网站生成器和GitHub Pages服务部署博客文章的流程。由于GitHub Pages要求master分支包含要公开的网站根目录,而Pelican默认输出目录为output,作者巧妙地利用Git分支和钩子解决了两者之间的冲突。他创建了source分支存放博客源文件,master分支存放Pelican生成的output目录,并通过pre-push钩子在每次推送到source分支时自动更新master分支,确保GitHub Pages始终显示最新版本。此外,文章还详细解释了钩子脚本的逻辑,包括生成生产版本、创建CNAME文件、使用GitHub Pages导入工具以及生成开发版本等步骤。

🤔 **Pelican和GitHub Pages的结合:**Pelican是一个静态网站生成器,可以将Markdown等格式的内容转换为HTML文件,GitHub Pages则提供免费的网站托管服务。作者使用这两个工具构建了自己的博客。

🔄 **分支管理:**为了解决GitHub Pages需要output目录作为根目录的问题,作者创建了两个分支:source分支存放博客源文件,master分支存放Pelican生成的output目录。

🪝 **Git钩子:**作者利用Git钩子机制,在每次推送到source分支时自动更新master分支。pre-push钩子会执行一个脚本,该脚本会生成生产版本、创建CNAME文件、将output目录导入到gh-pages分支,并最终推送到远程master分支。

🚫 **.gitignore文件:**作者将output目录加入到.gitignore文件中,避免将生成的网站文件纳入版本控制。

📝 **双版本生成:**脚本在更新master分支后,会再次执行Pelican命令生成开发版本,方便作者继续撰写下一篇文章。

This blog is powered by Pelican and hosted through GitHub using GitHub Pages.In this post I'll describe the workflow I use when deploying new posts.

For those of you not familiar with these technologies, Pelican is a static site generator - meaning you can write your content in a format suchas Markdown, and Pelican will automatically generate the HTML files for you; and GitHub Pages isa service provided by GitHub for hosting a website under the <your-username>.github.io URL.

Using Pelican and GitHub Pages is quite easy. There's one annoying little thing though... GitHub Pages assumesthe master branch contains the root folder to be served to the world. If you're using Pelican's default settings, the outputfolder is the folder you want to serve. output contains the generated website's files.A natural choice of how to organize the files inside the repositorywould be to define pelican's root folder - the parent of output - as the root of the repository. But GitHub Pages needsoutput to be the root. Bummer...

There are thosewho solve it using two separate repositories: one for the website "source" files, andone for the output which will be served using GitHub Pages.

I personally don't like breaking my blog into two repositories. I want to keep everything in one place, so I chose to solve the problemusing branches and git hooks.

The first step is to create two branches:

These branches will obviously live in my GitHub Pages repo(https://github.com/yoel-zeldes/yoel-zeldes.github.io),and since the master branch contains output's contents, a user navigating to yoel-zeldes.github.iowill be presented with the goodness of my blog.

Unfortunately, manually maintaining these two branches is cumbersome. Git hooks to the rescue!

Git has a mechanism to execute custom scripts when certain important actions occur. In my case, whenever I push a commit to the source branch,I'd like the master branch to get updated with the new contents of output. This can be done using the pre-push hook, which is executed -you guessed it - just before a push occurs.

All you have to do is create a file named .git/hooks/pre-push with the following content:

#!/bin/shwhile read local_ref local_sha remote_ref remote_shado        if [ "$remote_ref" = "refs/heads/source" ]        then                echo 'pushing output folder (production version) to master...'                pelican content -o output -s publishconf.py                echo anotherdatum.com > output/CNAME                ghp-import output                git push --no-verify git@github.com:yoel-zeldes/yoel-zeldes.github.io.git gh-pages:master                pelican content -o output        fidoneexit 0
    The first thing the script does is iterating over the commits that are about to be pushed. Specifically, only commits that are pushed to thesource branch are of interest to us.If commits are pushed to source, it executes pelican command using publishconf.py. This will generate the production version of the blog into output.It then creates a CNAME file, which is needed since I use a custom domain (http://anotherdatum.com).The GitHub Pages Import tool is used for copying the contents of output to a branch named gh-pages.gh-pages is pushed to the remote master branch. --no-verify skips the pre-push hook so this script won't run again.pelican is executed again to generate the development version of my blog, so I'll be able to write the next post.

Now, whenever I push to source, and only to source, master gets updated with the new contents. Cool!

One last small detail: I added output to the .gitignore file. This way, the source branch won't include this folder.We don't really want to put it under version control - it would be like putting other types of generated files such as.pyc or .o under version control.

Fish AI Reader

Fish AI Reader

AI辅助创作,多种专业模板,深度分析,高质量内容生成。从观点提取到深度思考,FishAI为您提供全方位的创作支持。新版本引入自定义参数,让您的创作更加个性化和精准。

FishAI

FishAI

鱼阅,AI 时代的下一个智能信息助手,助你摆脱信息焦虑

联系邮箱 441953276@qq.com

相关标签

Pelican GitHub Pages Git 博客部署 静态网站
相关文章