骏马飞鹰 發表於 2023-6-17 11:52:00

配置github pages教程

<h1 id="1基础配置">1.基础配置</h1>
<pre><code>mkdir myblog &amp;&amp; cd myblog
hugo new site .

git init
git submodule add git@github.com:MeiK2333/github-style.git themes/github-style
vim .gitignore
git add .
git push -u origin master
</code></pre>
<h2 id="git-submodule-相关命令">git submodule 相关命令</h2>
<p>Git - 子模块<br>
git submodule deinit 卸载子模块 - 知乎<br>
Git submodule add: "a git directory is found locally" issue - Stack Overflow</p>
<h3 id="下载子模块">下载子模块</h3>
<p>方法1:<br>
使用<code>git clone https://github.com/chaconinc/MainProject</code>克隆一个含有子模块的项目时,默认会包含该子模块目录,但其中还没有任何文件<br>
然后<code>git submodule init</code>初始化本地配置文件,<code>git submodule update</code>则从该项目中抓取所有数据并检出父项目中列出的合适的提交。</p>
<p>方法2:<br>
递归下载所有子模块: <code>git clone --recurse-submodules https://github.com/chaconinc/MainProject</code></p>
<h3 id="修改子模块">修改子模块</h3>
<p>在父项目中修改并提交子项目代码<br>
子项目中有三种状态,一是以版本编号命名的游离态,二是主分支master,三是自己创建的分支。一般是在自己的分支进行修改。</p>
<pre><code>cd &lt;directory_of_submodule&gt;
git checkout &lt;branch_name&gt;
git add .
git commit -m "commit_log"
git push origin &lt;branch_name&gt;
</code></pre>
<p>在父项目中更新子项目修改</p>
<pre><code>git submodule update --init// 初始化版本
git submodule update --remote// 更新到最新版本
</code></pre>
<p>在父项目中提交子项目版本</p>
<pre><code>git submodule update --init
git submodule update --remote
git add &lt;folder_of_submodule&gt;
git commit -m "commit message"
git push
</code></pre>
<h3 id="删除子模块">删除子模块</h3>
<pre><code>git submodule deinit &lt;project-sub-1&gt;
git rm &lt;project-sub-1&gt;
git commit -m "delete submodule project-sub-1"
</code></pre>
<p>使用 <code>git submodule deinit</code> 命令卸载一个子模块,会自动在 <code>.git/config</code> 中删除相关内容。这个命令如果添加上参数 <code>--force</code>,则子模块工作区内即使有本地的修改,也会被移除。<br>
执行 <code>git rm project-sub-1</code> 的效果,是移除了 project-sub-1 文件夹,并自动在 <code>.gitmodules</code> 中删除了相关内容。</p>
<h2 id="更新主题">更新主题</h2>
<p>github-style theme</p>
<pre><code>cd themes/github-style
git pull
</code></pre>
<h2 id="编辑readme">编辑readme</h2>
<pre><code>hugo new readme.md
echo '`Hello World!`' &gt; content/readme.md
</code></pre>
<h1 id="2新增帖子">2.新增帖子</h1>
<p>Hugo will create a post with <code>draft: true</code>, change it to false in order for it to show in the website.</p>
<pre><code class="language-shell">hugo new post/title_of_the_post.md
</code></pre>
<h1 id="3本地预览">3.本地预览</h1>
<p><code>hugo server -t github-style -D -w</code></p>
<blockquote>
<p>hugo server 的选项:<br>
-w 修改后本地服务器可以立即变化<br>
-D 也显示 <code>draft: true</code>的帖子</p>
</blockquote>
<h2 id="错误1">错误1</h2>
<p>如果出现了下面的报错,检查下:<br>
1.使用的配置文件是哪个<br>
2.<code>config.toml</code>文件中的<code>themeDir</code>的路径以及<code>theme</code>的名称是否和下载的文件夹名称一致</p>
<pre><code>WARN 2019/05/31 16:14:35 found no layout file for "HTML" for "section": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
WARN 2019/05/31 16:14:35 found no layout file for "HTML" for "section": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
WARN 2019/05/31 16:14:35 found no layout file for "HTML" for "taxonomyTerm": You should create a template file which matches Hugo Layouts Lookup Rules for this combination.
</code></pre>
<h2 id="错误2">错误2</h2>
<p>有些主题(比如puppet)依赖hugo名字中带extended的版本:<code>hugo_extended_0.121.2_linux-amd64.tar.gz</code>,否则会执行报错。</p>
<h1 id="4部署到-github-pages">4.部署到 Github Pages</h1>
<p>注意确认下 <code>${username}.github.io</code> 这个repo下面 <code>Settings/Pages/Branch</code> 是否和部署的分支一样。</p>
<h2 id="手动部署">手动部署</h2>
<p>生成 <code>hugo --theme=your_theme --baseUrl="your_server_or_domain" --buildDrafts</code><br>
提交 <code>public</code> 目录到 <code>${username}.github.io</code> 仓库<br>
在仓库的 <code>https://github.com/${username}/${username}.github.io/settings/pages</code> 页面可以设置部署哪个分支</p>
<h2 id="使用-github-actions-自动部署">使用 Github Actions 自动部署</h2>
<p>Hugo + GitHub Action,搭建你的博客自动发布系统 · Pseudoyu</p>
<ul>
<li>
<p>在 github 账户申请具备 repo 和 workflow 权限的密钥</p>
</li>
<li>
<p>在博客原始代码仓库添加环境变量 <code>PERSONAL_TOKEN</code><br>
<img src="https://cdn.jsdelivr.net/gh/devin0x01/myimages@master/githubpages/image_a98bd2b10c3990c971f643943b261a8d.png" alt="image" loading="lazy"></p>
</li>
<li>
<p>编辑 .github/workflow/deploy.yml 文件</p>
<blockquote>
<p><code>on</code> 表示 GitHub Action 触发条件,我设置了 <code>push</code>、<code>workflow_dispatch</code> 和 <code>schedule</code> 三个条件:<br>
<code>push</code>,当这个项目仓库发生推送动作后,执行 GitHub Action<br>
<code>workflow_dispatch</code>,可以在 GitHub 项目仓库的 Action 工具栏进行手动调用<br>
<code>schedule</code>,定时执行 GitHub Action,主要是使用一些自动化统计 CI 来自动更新我博客的关于页面,如本周编码时间</p>
</blockquote>
<blockquote>
<p><code>jobs</code> 表示 GitHub Action 中的任务,我们设置了一个 build 任务,<code>runs-on</code> 表示 GitHub Action 运行环境,我们选择了 ubuntu-latest。<br>
我们的 build 任务包含了 Checkout、Setup Hugo、Build Web 和 Deploy Web 四个主要步骤。<code>run</code> 是执行的命令,<code>uses</code> 是 GitHub Action 中的一个插件,我们使用了 <code>peaceiris/actions-hugo@v2</code> 和 <code>peaceiris/actions-gh-pages@v4</code> 这两个插件。Checkout 步骤中 <code>with</code> 配置 <code>submodules</code> 值为 <code>true</code> 可以同步博客源仓库的子模块,即我们的主题模块。<br>
另外,<code>EXTERNAL_REPOSITORY</code> 需要改为自己的 GitHub Pages 仓库。</p>
</blockquote>
</li>
</ul>
<h1 id="5图床设置">5.图床设置</h1>
<p>图床设置教程</p>
<ul>
<li>picgo-plugin-github-plus: picgo自带的github图床删除图片时不能同步到github,使用此插件可以在picgo相册中删除图片时自动把github图床内的图片也删除了。</li>
<li>picgo-plugin-rename-file: 此插件可以对上传的文件按指定格式重命名,比如按照md5值等。</li>
</ul>
<h2 id="图床访问加速">图床访问加速</h2>
<p>CDN jsdelivr加速github图床<br>
替换前缀即可, 其中<code>@{branch}</code>部分可以省略:replace <code>https://raw.githubusercontent.com/{user}/{repo}/{branch}/</code><br>
to <code>https://cdn.jsdelivr.net/gh/{user}/{repo}@{branch}/</code></p><br><br>
来源:https://www.cnblogs.com/devin1024/p/17487303.html
頁: [1]
查看完整版本: 配置github pages教程