Github Action协同Hexo连续集成博客
<h1 style="color: black; text-align: left; margin-bottom: 10px;">Github Action<span style="color: black;">协同</span>Hexo<span style="color: black;">连续</span>集成博客</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">经过</span>Hexo写博客,<span style="color: black;">按照</span>Hexo文档安装hexo并且找一个自己<span style="color: black;">爱好</span>的主题,<span style="color: black;">安排</span>在GitPage上,节省<span style="color: black;">咱们</span>自己的服务器资源</span></span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">新建Github仓库</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">新建github仓库,如</span><span style="color: black;">hexo-blog</span> ,仓库<span style="color: black;">倘若</span>要<span style="color: black;">安排</span>gitpage<span style="color: black;">不可</span><span style="color: black;">选取</span>private。</span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">新建Hexo目录</h1>hexo <span style="color: black;">init</span> hexo-blog<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">正常配置hexo和hexo主题并<span style="color: black;">起始</span>写作,写作完成后:</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">倘若</span>你的主题文件夹theme中有.git文件夹,请将其删除后再push代码</span></span></span></span></p>git init
git add .
git <span style="color: black;">commit</span> -m <span style="color: black;">"first commit"</span>
git branch -M <span style="color: black;">main</span>
git remote <span style="color: black;">add</span> origin git@github.com:xxxx/hexo-blog (xxx是你的github用户名)
git push -u origin <span style="color: black;">main</span>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">新建Git Page仓库</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">在Github中新建一个仓库,仓库名为:</span></span></span></p><你的Git用户名>.github.<span style="color: black;">io</span>//用户名
如: sawyersven.github.<span style="color: black;">io</span>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">至此准备工作完成,</span><span style="color: black;">hexo-blog</span>仓库存放博客的源码,xxx.github.io仓库存放打包生成的静态资源,下边<span style="color: black;">起始</span>配置CI</span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">配置hexo deploy</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">添加hexo的git deploy依赖:</span></span></span></p>npm <span style="color: black;">install</span>hexo-deployer-git / yarn<span style="color: black;">add</span> hexo-deployer-git<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">在hexo-blog根目录的</span><span style="color: black;">_config.yml</span>文件中找到deploy字段并添加:</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">type为git(必须是git)</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">repo是要发布的repo的<span style="color: black;">位置</span></span></span></span></p>...
deploy:
<span style="color: black;">type</span>: <span style="color: black;">git</span>
repo: <span style="color: black;">git@github.com:<gitusername>/<gitusername>.github.io.git</span>
...<h1 style="color: black; text-align: left; margin-bottom: 10px;">配置Github Action</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">GithubAction的</span><span style="color: black;">文档</span></span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">配置workflows</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">在</span><span style="color: black;">hexo-blog</span>项目根目录下创建一个<span style="color: black;">.github\workflows</span>结构的目录,<span style="color: black;">而后</span>新建一个yml文件:</span></span></p>mkdir -p .github/workflows<span style="color: black;">cd</span> ./github/workflows
touch main.yml<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">而后</span><span style="color: black;">咱们</span>编辑main.yml:</span></span></span></p>name: Deploy <span style="color: black;">#随便取</span>
on:
push:
branches: <span style="color: black;"># 当main<span style="color: black;">或</span>master分支被push到远端仓库的时候,触发ci</span>
- main
- master
jobs:
build:
runs-on: ubuntu-latest <span style="color: black;"># 在ubuntu下执行后续的操作</span>steps:
- name: Checkout Respository master branch<span style="color: black;">#actions/checkout checkout到$GITHUB_WORKSPACE下的repo 默认为当前repo</span>
uses: actions/checkout@master
- name: Setup Node.js 14.x <span style="color: black;"># 安装node14</span>
uses: actions/setup-node@master
<span style="color: black;">with</span>:
node-<span style="color: black;">version</span>: <span style="color: black;">14.17.3</span>
- <span style="color: black;">name</span>: <span style="color: black;">Cache</span> node modules <span style="color: black;"># 缓存依赖<span style="color: black;">增多</span>工作流的执行速度</span>
uses: actions/<span style="color: black;">cache</span>@v1
<span style="color: black;">id</span>: <span style="color: black;">cache</span>
<span style="color: black;">with</span>:
<span style="color: black;">path</span>: node_modules
<span style="color: black;">key</span>: ${{ runner.os }}-node-${{ hashFiles(<span style="color: black;">**/.json</span>) }} <span style="color: black;"># <span style="color: black;">这儿</span><span style="color: black;">按照</span>用的包管理工具切换lock文件,hashFile会为匹配的文件计算SHA-256哈希值</span>
<span style="color: black;">restore</span>-<span style="color: black;">keys</span>: |
${{runner.os}}-node-
- <span style="color: black;">name</span>: Setup Hexo Dependencies <span style="color: black;"># 安装hexo-cli 并且安装依赖</span>
run: |
npm <span style="color: black;">install</span>hexo-cli -g
npm<span style="color: black;">install</span>
- <span style="color: black;">name</span>: Setup Deploy <span style="color: black;">Private</span> <span style="color: black;">Key</span> <span style="color: black;"># 给ubuntu环境添加ssh的私钥,先按<span style="color: black;">这儿</span>填,后边单独讲</span>env:
HEXO_DEPLOY_PRIVATE_KEY: ${{secrets.HEXO_DEPLOY_PRI}}
run: |
mkdir -p ~/.ssh/
echo<span style="color: black;">"$HEXO_DEPLOY_PRIVATE_KEY"</span> > ~/.ssh/id_rsa
chmod <span style="color: black;">600</span>~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/know_hosts
-<span style="color: black;">name</span>: Setup Git Infomation <span style="color: black;"># hexo deploy必须有git user.name和git user.email<span style="color: black;">才可</span>运行</span>run: |
git config<span style="color: black;">--global user.name "sawyersven"</span>
git config <span style="color: black;">--global user.email "kormondor@gmail.com"</span>
- <span style="color: black;">name</span>: Deploy Hexo
run: |
hexo clean
hexo generate
hexo deploy<h1 style="color: black; text-align: left; margin-bottom: 10px;">配置SSH key</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">由于</span>是,在</span><span style="color: black;">hexo-blog</span>的仓库中提交代码<span style="color: black;">而后</span>构建到了<span style="color: black;">xxx.github.io</span>的仓库中,<span style="color: black;">因此</span>需要把SSH key配置在执行action的ubuntu环境中</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">首要</span>在命令行中执行:</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">设置filename的时候<span style="color: black;">意见</span>设置为github_id_rsa<span style="color: black;">或</span>其他名<span style="color: black;">叫作</span>,以便区分</span></span></span></span></p>ssh-keygen # 生成的key默认在~<span style="color: black;">/.ssh/gi</span>thub_id_rsa,windows在C:<span style="color: black;">/User/</span><username><span style="color: black;">/.ssh/gi</span>thub_id_rsa<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">而后</span>将公钥上传到github的ssh key中:</span><span style="color: black;">点我直达配置页</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">选取</span></span><span style="color: black;">New SSH key</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">title : 任意填</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">key: 用记事本或编辑器打开</span><span style="color: black;">github_id_rsa.pub</span>,将里面的文本粘贴进去.</span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">配置Action Secrets</h1>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">因为</span>workflow的文件是在代码仓库中,脚本中的内容是对仓库可见的,<span style="color: black;">倘若</span><span style="color: black;">咱们</span>将<span style="color: black;">有些</span>隐私内容直接明文写在文件中,会被<span style="color: black;">泄密</span>。<span style="color: black;">因此</span>github<span style="color: black;">供给</span>了Action secrect来帮<span style="color: black;">咱们</span>存放<span style="color: black;">敏锐</span>信息。在Actions中<span style="color: black;">经过</span>${{secrets.}}<span style="color: black;">就可</span>获取</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">进入</span><span style="color: black;">blog-hexo</span>的repo中,点击<span style="color: black;">settings</span>,<span style="color: black;">而后</span>在侧边栏<span style="color: black;">选取</span><span style="color: black;">Secrets</span>-<span style="color: black;">Actions</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">而后</span><span style="color: black;">选取</span></span><span style="color: black;">New repository secret</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">name中填写HEXO_DEPLOY_PRI</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">value将</span><span style="color: black;">github_id_rsa</span>的内容复制后粘贴进去<span style="color: black;">就可</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">至此github Action的配置就完<span style="color: black;">成为了</span>,<span style="color: black;">能够</span>尝试更新</span><span style="color: black;">hexo-blog</span>的代码并push到github,<span style="color: black;">而后</span>观察Action中job的执行<span style="color: black;">状况</span>.</span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">而后</span><span style="color: black;">拜访</span>xxx.github.io<span style="color: black;">就可</span>查看效果。</span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">这儿</span>不<span style="color: black;">意见</span>将本机正在<span style="color: black;">运用</span>的ssh密钥对上传到github中,尤其是私钥.</span></span></span></span></p>
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">到<span style="color: black;">这儿</span>配置就完<span style="color: black;">成为了</span>,后文是<span style="color: black;">有些</span>知识点的<span style="color: black;">弥补</span>,可<span style="color: black;">选取</span>阅读</span></span></span></span></p>
<h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">弥补</span>阅读</h1>
<h1 style="color: black; text-align: left; margin-bottom: 10px;">main.yml中的Setup Deploy Private Key</h1>- name: Setup Deploy Private Key<span style="color: black;"># 给ubuntu环境添加ssh的私钥,先按<span style="color: black;">这儿</span>填,后边单独讲</span>
env:
HEXO_DEPLOY_PRIVATE_KEY: <span style="color: black;">${{secrets.HEXO_DEPLOY_PRI}</span>} <span style="color: black;"># 设置环境变量</span>run: |
mkdir -p ~/.ssh/<span style="color: black;">echo</span> <span style="color: black;">"<span style="color: black;">$HEXO_DEPLOY_PRIVATE_KEY</span>"</span> > ~/.ssh/id_rsa
chmod 600 ~/.ssh/id_rsa
ssh-keyscan github.com >> ~/.ssh/know_hosts<h1 style="color: black; text-align: left; margin-bottom: 10px;">拆分理解</h1>mkdir -p ~<span style="color: black;">/.ssh/</span><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">创建.ssh文件夹</span></span></span></p> <span style="color: black;">echo</span> <span style="color: black;">"<span style="color: black;">$HEXO_DEPLOY_PRIVATE_KEY</span>"</span> > ~/.ssh/id_rsa<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">读取环境变量$HEXO_DEPLOY_PRIVATE_KEY的值,并将其写入id_rsa中 , 其实这步操作约等于<span style="color: black;">咱们</span>在本地生成ssh key并将公钥上传到github的ssh key中,只是<span style="color: black;">次序</span>对调了<span style="color: black;">罢了</span>。</span></span></span></p>chmod <span style="color: black;">600</span> ~<span style="color: black;">/.ssh/i</span>d_rsa<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">设置文件权限为<span style="color: black;">持有</span>者可读写,其他人不可读写执行</span></span></span></p>ssh-keyscan github.com >> ~<span style="color: black;">/.ssh/</span>know_hosts<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">收集github.com的公钥并将其写入know_hosts,避免被openSSH警告</span></span></span></p><h1 style="color: black; text-align: left; margin-bottom: 10px;">将Gitpage解析到自己的域名</h1><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">在购买域名的服务商中添加新的CNAME解析,解析值填为:xxx.github.io (你的gitpage的<span style="color: black;">拜访</span><span style="color: black;">位置</span>)</span></span></span></p><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">进入gitpage的仓库,并点击settings</span></span></span></p><div style="color: black; text-align: left; margin-bottom: 10px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/211533c6f2a3494fa1827019fc6ca97d~noop.image?_iz=58558&from=article.pc_detail&lk3s=953192f4&x-expires=1728086120&x-signature=T9kv8GepF2wm1vIO3U8X5BHfS%2Fs%3D" style="width: 50%; margin-bottom: 20px;"></div><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">image-20220715224453906</p><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在custom domain中填写刚才解析的<span style="color: black;">位置</span>,github会校验域名的解析<span style="color: black;">是不是</span>是gitpage,<span style="color: black;">倘若</span>是就会successful.</p><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">这个时候<span style="color: black;">拜访</span>你解析的<span style="color: black;">位置</span>,就<span style="color: black;">能够</span>看到你的Github Page被解析过去了。</span></span></span></p><h1 style="color: black; text-align: left; margin-bottom: 10px;">CNAME文件</h1><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">发掘</span>每次提交以后设置的Customer domain会被清除掉</span></span></span></p><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">因此</span><span style="color: black;">运用</span>CNAME文件<span style="color: black;">守护</span></span></span></span></p><p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;">在hexo-blog的source文件夹下创建一个CNAM的文件,内容如下:</span></span></span></p>
<span style="color: black;">blog</span><span style="color: black;">.sawyersven</span><span style="color: black;">.xyz</span> # <span style="color: black;">仅有</span>一行代码,<span style="color: black;">便是</span>你要解析的自定义域名
<p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;"><span style="color: black;"><span style="color: black;"><span style="color: black;">而后</span>每次deploy后就<span style="color: black;">不消</span>手动<span style="color: black;">调节</span>custom domain了</span></span></span></p> 回顾过去一年,是艰难的一年;展望未来,是辉煌的一年。 大势所趋,用于讽刺一些制作目的就是为了跟风玩梗,博取眼球的作品。 “沙发”(SF,第一个回帖的人) 回顾历史,我们感慨万千;放眼未来,我们信心百倍。 楼主继续加油啊!外链论坛加油!
页:
[1]