2

我正在使用 github 来托管我的博客并使用静态站点 Generator HUGO 来实现它,但它太繁琐,无法使其脱机并编译它,然后将公共文件夹上传到 gh-pages 或使其在 docs 文件夹中可用。

所以我想自动化这个过程,所以每当我在内容中创建一个新的 .md 文件时,它应该生成静态站点并将公共文件夹复制到 gh-pages 或以下组合 -

  • "source" 分支中的源文件和 "public" 的内容发布到 master [用于用户和组织页面]
  • master 中的源文件并将“public”文件夹内容发布到“gh-pages”
  • 您想提出的任何其他方法

注意:我主要想使用 Travis-ci,但任何其他自动化平台也很酷

4

2 回答 2

0

为 GitHub Pages 建立 Hugo 博客的一种好方法是使用两个独立的存储库:

  • 第一个存储库包含博客源,
  • 第二个存储库包含生成的内容。

命名第二个存储库 username.github.io(使用您的 GitHub 用户名)。GitHub Pages 会自动将其部署到https://username.github.io/

然后将第二个存储库作为 git 子模块添加到第一个存储库。子模块需要位于./public,这是 Hugo 生成静态内容的地方。这使您可以轻松地将生成的内容推送到 GitHub。

git submodule add \
    https://github.com/username/username.github.io.git \
    public

这个过程在官方 Hugo 教程Hosting on GitHub中有更详细的解释。


持续集成

如果您想要完全自动化,您可以为第一个存储库设置 Travis CI。我在这里写了一篇关于这个设置的详细文章:

使用 Travis CI 在 Github 页面上托管 Hugo 博客

Travis CI 调用 Hugo 并将生成的内容推送回 GitHub,由 GitHub Pages 部署。为此,您需要一个.travis.yml文件和一个小型部署脚本:

.travis.yml

---
install:
  - curl -LO https://github.com/gohugoio/hugo/releases/download/v0.55.4/hugo_0.55.4_Linux-64bit.deb
  - sudo dpkg -i hugo_0.55.4_Linux-64bit.deb

script:
  - hugo

deploy:
  - provider: script
    script: ./deploy.sh
    skip_cleanup: true
    on:
      branch: master

部署.sh

#!/bin/bash

echo -e "\033[0;32mDeploying updates to GitHub...\033[0m"

cd public

if [ -n "$GITHUB_AUTH_SECRET" ]
then
    touch ~/.git-credentials
    chmod 0600 ~/.git-credentials
    echo $GITHUB_AUTH_SECRET > ~/.git-credentials

    git config credential.helper store
    git config user.email "username@users.noreply.github.com"
    git config user.name "username"
fi

git add .
git commit -m "Rebuild site"
git push --force origin HEAD:master

最后,在 Travis CI 上设置一个环境变量GITHUB_AUTH_SECRET以提供对username.github.io存储库的访问。博客文章还解释了如何为此使用单独的机器人帐户,限制 CI 对username.github.io存储库的访问。

于 2019-04-28T13:23:40.873 回答
0

这几天(2020 年 10 月),您不需要使用外部 CICD 服务(如 Travis-CI)。

Michelle Mannering的“ GitHub Action Hero·James Ives 和“GitHub Pages Deploy”中所述,您可以使用 GitHub Actions。

具体来说,James IvesGitHub Pages Deploy Action

此 GitHub Action 将自动将您的项目部署到 GitHub Pages。
它可以配置为将您的生产就绪代码推送到您想要的任何分支,包括gh-pagesdocs.
它也可以处理跨存储库部署。

例子:

name: Build and Deploy
on: [push]
jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout ️
        uses: actions/checkout@v2.3.1 # If you're using actions/checkout@v2 you must set persist-credentials to false in most cases for the deployment to work correctly.
        with:
          persist-credentials: false

      - name: Install and Build  # This example project is built using npm and outputs the result to the 'build' folder. Replace with the commands required to build your project, or remove this step entirely if your site is pre-built.
        run: |
          npm install
          npm run build

      - name: Deploy 
        uses: JamesIves/github-pages-deploy-action@3.6.2
        with:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          BRANCH: gh-pages # The branch the action should deploy to.
          FOLDER: build # The folder the action should deploy.
          CLEAN: true # Automatically remove deleted files from the deploy branch
于 2020-10-03T06:07:42.370 回答