从零用VitePress搭建博客教程(7) -– 如何用Github Actions自动化部署到Github Pages?
<p>接上一节:<span style="color: rgba(51, 102, 255, 1)"><span style="color: rgba(51, 102, 255, 1)">从零用VitePress搭建博客教程(6) -– 第三方组件库的使用和VitePress搭建组件库文档 </span></span></p><p>我们搭建完成vitePress后,那么接下来就是如何部署到线上服务器,这里使用Github Pages,免得自己购买服务器,当然你也可以自己购买服务器来部署(比如阿里云服务器)。</p>
<p>在部署之前,我们先简单了解下<span style="color: rgba(255, 0, 0, 1)"><strong>Github Actions</strong></span>和<span style="color: rgba(255, 0, 0, 1)"><strong>Github Pages</strong></span></p>
<h1><strong>一、基本概念认识</strong></h1>
<h2><strong>1、理解</strong><strong>Github Actions</strong></h2>
<p>中文文档地址:<span style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 255, 1)">https://docs.github.com/zh/actions</span></span></p>
<p>简单说,<span style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 255, 1)">Github Actions</span></span>就是GitHub官方提供的自动化(CI/CD)服务, 通过它可以完成自动化测试、集成、部署等操作。</p>
<div class="lake-content">
<p id="u54bf2b6d" class="ne-p">它的优势有:</p>
<ol class="ne-ol">
<li id="uab0f2044">和GitHub集成更容易</li>
<li id="uf9870ac1">支持复用其他人的基本片段</li>
</ol></div>
<p><strong>GitHub Actions的基本概念主要有以下几个:</strong></p>
<p id="ub22549f3" class="ne-p"><span style="color: rgba(255, 0, 0, 1)"><strong>workflow </strong></span><span class="ne-text"><span style="color: rgba(255, 0, 0, 1)"><strong>(工作流程)</strong></span>:持续集成一次运行的过程就是一个 workflow,一个项目可以有多个workflow。</span></p>
<p id="u5889aa00" class="ne-p"><span style="color: rgba(255, 0, 0, 1)"><strong>job</strong></span><span class="ne-text"><span style="color: rgba(255, 0, 0, 1)"><strong> (任务)</strong></span>:一个 workflow 由一个或多个 jobs 构成,含义是一次持续集成的运行,可以完成多个任务。</span></p>
<p id="ua325f89b" class="ne-p"><span style="color: rgba(255, 0, 0, 1)"><strong>step</strong></span><span class="ne-text"><span style="color: rgba(255, 0, 0, 1)"><strong>(步骤)</strong></span>:每个 job 由多个 step 构成,一步步完成,step 下有 name、uses、run、with 等,表示一个 action</span></p>
<p id="u50369730" class="ne-p"><span style="color: rgba(255, 0, 0, 1)"><strong>action</strong></span><span class="ne-text"><span style="color: rgba(255, 0, 0, 1)"><strong> (动作</strong>)</span>:每个 step 可以依次执行一个或多个命令(action)。</span></p>
<p id="u50369730" class="ne-p"><span class="ne-text"><strong>注意点:</strong><br>yml配置文件通常存放在项目中的.github/workflows 目录,每个workflow都是.github/workflows目录下的一个文件<br>workflow 文件采用 YAML 格式,文件名可以任意取,但是后缀统一为 .yml。<br></span></p>
<h3><strong>如何执行部署</strong><strong>的?</strong></h3>
<p>简单说就是,我们通过配置yml文件来执行,当提交代码到Github仓库后,就可以自动部署到Github Pages上去。</p>
<div class="lake-content">因为Github 识别到配置文件后,会自动执行配置中的工作流(主要看配置自动的情况)</div>
<h3><strong>yml 配置文件一些概念说明</strong></h3>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">name:CI
on:
# 手动运行工作流程(workflow_dispatch 事件触发器配置后可以在actions下手动运行工作流)
workflow_dispatch:
push:
branches:
</span>-<span style="color: rgba(0, 0, 0, 1)"> master
jobs:
# jobs的id
build</span>-and-<span style="color: rgba(0, 0, 0, 1)">deploy:
# 指定服务器的运行环境:最新版本ubuntu
runs</span>-on: ubuntu-<span style="color: rgba(0, 0, 0, 1)">latest
steps:
# 使用actions</span>/<span style="color: rgba(0, 0, 0, 1)">checkout@v4 库拉取代码到 ubuntu 上
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: Checkout
uses: actions</span>/<span style="color: rgba(0, 0, 0, 1)">checkout@v4
# 安装 pnpm
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: Install pnpm
uses: pnpm</span>/action-<span style="color: rgba(0, 0, 0, 1)">setup@v2
with:
version: </span><span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)">
# 打包成静态文件
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: Build
run: pnpm </span><span style="color: rgba(0, 0, 255, 1)">install</span> && pnpm build</pre>
</div>
<p>我想大家看到上面的配置代码,基本就知道意思了,这里也简单说下</p>
<h3><strong>常用的字段及含义如下:</strong></h3>
<p><strong>name : workflow 的名称 , 如果省略该字段,默认为当前workflow的文件名</strong></p>
<p><strong>on: </strong> 指定触发workflow的条件,通常是一些事件触发器,比如:push, workflow_dispatch、pull_request等</p>
<p><strong>steps: </strong>简单说就是一个步骤的集合(里面有多个小步骤),从上到下执行,它有几个相关的选项</p>
<div class="lake-content"><ol class="ne-ol">
<li id="u88a90594"><strong>name</strong>:每个小步骤的名称(可自由定义)。</li>
<li id="ude12db93"><strong>uses:</strong>每个小步骤使用的 actions 库名称或路径,Github Actions 允许我们使用别人写好的 Actions 库。</li>
<li id="u2decdf6d"><strong>run: </strong>每个小步骤要执行的 shell 命令。</li>
<li id="u4816de33"><strong>with:</strong> 配置actions的额外参数。</li>
</ol></div>
<h2><strong>2、理解</strong><span style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 255, 1)"><strong>Github Pages</strong></span></span></h2>
<p><span style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 255, 1)"><strong>Github Pages</strong></span></span>简单说就是可以将我们托管在Github上的项目,免费发布为对外的公共网页,免去咱们花钱买服务器。</p>
<p>我们可以使用 GitHub Pages 来展示一些开源项目、博客等等。</p>
<p>下面开始说明如何去部署我们的博客站点</p>
<h1><strong>二、用Github Actions自动化部署到Github Pages</strong></h1>
<p>主要通过以下4个步骤完成部自动化署到</p>
<p><strong>1、在Gtihub上</strong><strong>创建仓库和相关分支</strong></p>
<p><strong>2、配置github pages</strong></p>
<p><strong>3、</strong><strong>编写自动化部署yml文件</strong></p>
<p><strong>4、找到链接地址查看博客效果</strong></p>
<h2><strong>1、在Gtihub上创建仓库和相关分支</strong></h2>
<p id="u9a94ad88" class="ne-p"><span class="ne-text">我们在Github上新建一个仓库, 这里我的项目叫<strong>vitePress-project</strong>。<strong>master</strong>主分支上提交我们的源代码。</span></p>
<p id="uc0b3d6ca" class="ne-p">然后我们再新建一个分支叫<strong>deploy-pages</strong>,清空里面的内容,这个分支用于存放pnpm build打包后的代码。</p>
<p id="uc0b3d6ca" class="ne-p"> </p>
<h2><strong>2、配置Github Pages</strong></h2>
<p id="ue0455e9c" class="ne-p"><span class="ne-text">到vitePress-project仓库 -> Settings -> Pages 去设置Pages关联的分支deploy-pages, 如图</span></p>
<p class="ne-p"><span class="ne-text"><img src="https://img2023.cnblogs.com/blog/573390/202310/573390-20231024093332601-609216384.png"></span></p>
<h2>3、<strong>编写自动化部署配置文件</strong></h2>
<p id="ua69f995f" class="ne-p"><span class="ne-text">有两种方法可以创建自动化部署文件</span></p>
<p id="u208da902" class="ne-p"><span class="ne-text"><strong>1、直接</strong><strong>去github仓库/Actions下新建一个自动部署文件<strong>ci.yml</strong>(ci.yml名字可以自定义),然后修改内容即可</strong></span></p>
<p id="u44a73835" class="ne-p"><strong>2、手动创建,如下所示</strong></p>
<p class="ne-p"><strong><img src="https://img2023.cnblogs.com/blog/573390/202310/573390-20231024093355915-1686686189.png"></strong></p>
<p class="ne-p"><strong>ci.yml配置内容如下</strong></p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">name: GitHub Actions Build and Deploy
on:
# 手动运行工作流程(workflow_dispatch 事件触发器配置后可以在actions下手动运行工作流)
workflow_dispatch:
jobs:
build</span>-and-<span style="color: rgba(0, 0, 0, 1)">deploy:
runs</span>-on: ubuntu-<span style="color: rgba(0, 0, 0, 1)">latest#指定服务器的运行环境:最新版本ubuntu
steps:
# 使用actions</span>/checkout@v4 库拉取代码到 ubuntu 上
-<span style="color: rgba(0, 0, 0, 1)"> name: Checkout
uses: actions</span>/checkout@v4
<span style="color: rgba(0, 0, 255, 1)">with</span><span style="color: rgba(0, 0, 0, 1)">:
# 根据网上资料查询此处可以设置为 </span><span style="color: rgba(0, 0, 255, 1)">false</span>。https:<span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">github.com/actions/checkout</span>
persist-credentials: <span style="color: rgba(0, 0, 255, 1)">false</span><span style="color: rgba(0, 0, 0, 1)">
# 安装 pnpm
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: Install pnpm
uses: pnpm</span>/action-setup@v2
<span style="color: rgba(0, 0, 255, 1)">with</span><span style="color: rgba(0, 0, 0, 1)">:
version: </span>8<span style="color: rgba(0, 0, 0, 1)">
# 设置node的版本
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: Use Node.js
# 使用 actions</span>/setup-node@v3 库安装 nodejs,with 提供了一个参数 node-version 表示要安装的 nodejs 版本
uses: actions/setup-node@v3
<span style="color: rgba(0, 0, 255, 1)">with</span><span style="color: rgba(0, 0, 0, 1)">:
node</span>-version: '18.x'<span style="color: rgba(0, 0, 0, 1)">
cache: </span>'pnpm'<span style="color: rgba(0, 0, 0, 1)">
# 打包成静态文件
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: Build
run: pnpm install </span>&&<span style="color: rgba(0, 0, 0, 1)"> pnpm build
# 部署到GitHub Pages </span>-<span style="color: rgba(0, 0, 0, 1)"> 也就是将打包内容发布到GitHub Pages
</span>-<span style="color: rgba(0, 0, 0, 1)"> name: Deploy
# 使用别人写好的 actions去部署(将打包文件部署到指定分支上)
uses: JamesIves</span>/github-pages-deploy-action@v4.3.3
<span style="color: rgba(0, 0, 0, 1)"> # 自定义环境变量
</span><span style="color: rgba(0, 0, 255, 1)">with</span><span style="color: rgba(0, 0, 0, 1)">:
# 指定仓库:你要发布的仓库路径名
repository</span>-name: msyuan/vitePress-project
# 部署到 deploy-<span style="color: rgba(0, 0, 0, 1)">pages 分支,也就是部署后提交到那个分支
branch: deploy</span>-<span style="color: rgba(0, 0, 0, 1)">pages
# 填写打包好的目录名称路径,本项目配置在根目录
folder: dist</span></pre>
</div>
<p id="u6e446d7b" class="ne-p"><strong>用到的相关插件地址:</strong></p>
<div class="lake-content">
<p id="uc4320bb6" class="ne-p"><span style="color: rgba(0, 0, 255, 1)"><span class="ne-text" style="color: rgba(0, 0, 255, 1)">https://github.com/actions/checkout</span></span></p>
<p id="ufae52f6f" class="ne-p"><span style="color: rgba(0, 0, 255, 1)"><span class="ne-text" style="color: rgba(0, 0, 255, 1)">https://github.com/pnpm/action-setup/</span></span></p>
<p id="ubf982865" class="ne-p"><span style="color: rgba(0, 0, 255, 1)"><span class="ne-text" style="color: rgba(0, 0, 255, 1)">https://github.com/actions/setup-node</span></span></p>
<p id="u1be3d2cc" class="ne-p"><span style="color: rgba(0, 0, 255, 1)"><span class="ne-text" style="color: rgba(0, 0, 255, 1)">https://github.com/JamesIves/github-pages-deploy-action</span></span></p>
<div> 因为我们上面配置的是手动去运行工作流,所以需手动去执行部署,如图所示</div>
</div>
<p><img src="https://img2023.cnblogs.com/blog/573390/202310/573390-20231024093535807-375607374.png"></p>
<p> </p>
<p>可以看到正在部署中….</p>
<p><img src="https://img2023.cnblogs.com/blog/573390/202310/573390-20231024093555584-458635636.png"></p>
<h2><strong>4、</strong><strong>找到链接地址查看博客效果</strong></h2>
<div class="lake-content">
<p id="uf7584d30" class="ne-p"><span class="ne-text">进入Settings -> Pages会看到博客链接地址:</span></p>
<p id="u58051f7f" class="ne-p">预览效果:<span style="color: rgba(0, 0, 255, 1)"><span class="ne-text" style="color: rgba(0, 0, 255, 1)">https://msyuan.github.io/vitePress-project/</span></span></p>
<p id="ua23f3f02" class="ne-p"><span class="ne-text">同时打包后的代码也正常部署到deploy-pages分支上去了,到此已经完成部署工作</span></p>
</div>
<p><img src="https://img2023.cnblogs.com/blog/573390/202310/573390-20231024093632529-380171651.png"></p>
<p>github项目地址:<span style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 255, 1)">https://github.com/msyuan/vitePress-project</span></span></p>
<p><span class="md-plain md-expand">在线预览效果:<span class="md-link md-pair-s md-expand" style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 255, 1)">https://msyuan.github.io/vitePress-project</span></span></span></p>
<p><span class="md-plain md-expand"><span class="md-link md-pair-s md-expand" style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 0, 1)"><strong>原文地址</strong></span>:</span></span><span class="md-link md-pair-s md-expand" style="color: rgba(0, 0, 255, 1)"><span style="color: rgba(0, 0, 255, 1)">http://www.qianduan8.com/2072.html</span></span></span></p>
<p> </p><br><br>
来源:https://www.cnblogs.com/myboogle/p/17784015.html
頁:
[1]