6hz7vif 发表于 2024-10-3 05:24:42

运用Hugo创立静态博客,并托管在Github Pages上


    <h1 style="color: black; text-align: left; margin-bottom: 10px;">从netfily迁移到github pages</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">netfily被封</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其实,早在3年前,我刚<span style="color: black;">创立</span>新博客的时候,<span style="color: black;">便是</span>托管在netfily上的,本博客的<span style="color: black;">第1</span>篇内容 《<span style="color: black;">运用</span>Hugo和Netlify<span style="color: black;">创立</span>静态博客,并托管在Github上》 </p>https://www.zhangshengdong.com/post/hugo_and_netlify/ ,<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>,netfily的登陆<span style="color: black;">因为</span>用到了google的脚本,被大陆封了。只是<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 style="color: black;">能够</span>自动从github仓库里同步发布,<span style="color: black;">因此</span><span style="color: black;">亦</span>就没<span style="color: black;">留意</span>。</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">可是两天前,我新写了一篇博客,本地hugo编译没问题,可是netfily却<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 style="color: black;">思虑</span>寻找新的托管网站。</p>
    </h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;"><span style="color: black;">运用</span>github pages</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在知乎上对比了一圈托管网站,<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 style="color: black;">亦</span><span style="color: black;">便是</span>github pages了。但<span style="color: black;">这般</span>一来,就<span style="color: black;">无</span>了免费cdn,网页加载速度慢了许多。</p>
    </h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">github流水线</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">自动化流程</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">github pages最好是设置一个名为username.github.io的仓库。但很不巧的是,我的博客源码之前<span style="color: black;">便是</span><span style="color: black;">保留</span>在我的blog仓库中。而github pages生成的二级域名,<span style="color: black;">针对</span>网站<span style="color: black;">好似</span>不太友好。<span style="color: black;">因此</span>只能设计一个自动化流程:</p>在源码blog仓库中,设置github action,自动编译静态网页;将编译出的public文件夹上传至新的BZ-coding组织的BZ-coding.github.io仓库;将BZ-coding.github.io仓库<span style="color: black;">选取</span>master分支自动发布github pages网页。
    </h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">hugo github action</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在blog仓新增自动编译hugo的github action:</p>
    </h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># Sample workflow for building and deploying a Hugo site to GitHub Pages </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">name: Deploy Hugo site to Pages </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">on: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># Runs on pushes targeting the default branch</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> push: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> branches: ["master"] </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> # Allows you to run this workflow manually from the Actions tab</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> workflow_dispatch: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">permissions: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> contents: read </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> pages: write </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> id-token: write </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># Allow one concurrent deployment </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">concurrency: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">group: "pages"</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> cancel-in-progress: true </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"># Default to bash </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">defaults: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> run: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> shell: bash </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">jobs: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> # Build job </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> build: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> runs-on: ubuntu-latest </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> env: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> HUGO_VERSION: 0.102.3 </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> steps: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- name: Install Hugo CLI</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">run: | wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_Linux-64bit.deb \ &amp;&amp; sudo dp<span style="color: black;">公斤</span> -i ${{ runner.temp }}/hugo.deb</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- name: Checkout </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> uses: actions/checkout@v3 </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> with: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> submodules: recursive </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- name: Build with Hugo</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> run: | </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> echo "pwd is " </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> pwd </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ls </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> hugo --minify --verbose </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- name: Display </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> run: | </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> echo "github.workspace is ${{ github.workspace }}" </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">ls ${{ github.workspace }}</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> echo "github.workspace public is ${{ github.workspace }}/public" </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> ls ./public </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- name: Deploy </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> uses: peaceiris/actions-gh-pages@v3 </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> with: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} # <span style="color: black;">这儿</span>的 ACTIONS_DEPLOY_KEY 则是上面设置 Private Key的变量名</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> external_repository: BZ-coding/BZ-coding.github.io # Pages 远程仓库 </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">publish_dir: "./public"</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> keep_files: false # remove existing files </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"> publish_branch: master # deploying </p>branch commit_message: ${{ github.event.head_commit.message }}
    </h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">自动上传至网页发布仓</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">在上面脚本的最后,加入了把编译结果文件上传至另一个仓库的代码:</p>
    </h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">- name: Deploy </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">uses: peaceiris/actions-gh-pages@v3 </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">with: </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">deploy_key: ${{ secrets.ACTIONS_DEPLOY_KEY }} # <span style="color: black;">这儿</span>的 ACTIONS_DEPLOY_KEY 则是上面设置 Private Key的变量名</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">external_repository: BZ-coding/BZ-coding.github.io # Pages 远程仓库 </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">publish_dir: "./public" </p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">keep_files: false # remove existing files</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">publish_branch: master # deploying </p>branch commit_message: ${{ github.event.head_commit.message }}
    </h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">其中,需要把<span style="color: black;">目的</span>账号里的ssh公钥,对应的私钥,配置在blog仓库的secrets里。</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">并且,这边的上传,<span style="color: black;">此刻</span><span style="color: black;">好似</span><span style="color: black;">不可</span>分成两个job,否则<span style="color: black;">好似</span>会public里为空。</p>
    </h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">配置自动发布网站和自定义域名</h1>
    <h1 style="color: black; text-align: left; margin-bottom: 10px;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">把网页发布仓</p>BZ-coding/BZ-coding.github.io,里的github pages配置成master的根目录。

      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">而后</span>在BZ-coding账号里验证zhangshengdong.com域名之后,就<span style="color: black;">能够</span>在github pages配置的自定义域名里配上www.zhangshengdong.com了。还<span style="color: black;">能够</span>强制打开https。</p>
    </h1>
    <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;">
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><span style="color: black;">然则</span>随后<span style="color: black;">发掘</span>,每次网页代码更新后,BZ-coding.github.io的自定义域名就会消失。</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;">经搜索,得知,要在生成的网站根目录里添加CNAME文件,文件内容就写你想自定义的域名就行,<span style="color: black;">例如</span>www.zhangshengdong.com 。</p>
      <p style="font-size: 16px; color: black; line-height: 40px; text-align: left; margin-bottom: 15px;"><img src="https://p3-sign.toutiaoimg.com/tos-cn-i-qvj2lq49k0/75862a46107d43ba8530e313f0f28ee7~noop.image?_iz=58558&amp;from=article.pc_detail&amp;lk3s=953192f4&amp;x-expires=1728086133&amp;x-signature=RxOppCKdq%2B2xb3F4tQXJjTsuAFQ%3D" style="width: 50%; margin-bottom: 20px;"></p>
    </h1>




b1gc8v 发表于 2024-10-19 20:46:56

感谢楼主分享,祝愿外链论坛越办越好!
页: [1]
查看完整版本: 运用Hugo创立静态博客,并托管在Github Pages上