【问题标题】:Hugo Automation with Travis-ci and Github pages带有 Travis-ci 和 Github 页面的 Hugo 自动化
【发布时间】:2017-08-03 03:46:58
【问题描述】:

我正在使用 github 来托管我的博客并使用静态站点 Generator HUGO 来实现它,但是让它离线并编译它然后将公共文件夹上传到 gh-pages 或使其在 docs 文件夹中可用,这太乏味了。

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

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

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

【问题讨论】:

  • 这并不能回答您的问题,但由于与您处于相似的位置,我最终选择了 GitLab 来托管我的博客。除非您必须使用 GitHub,否则可能值得考虑切换。
  • 它在云端编译 Hugo?
  • GitLab 可以,是的。我在本地添加内容,然后在推送到远程时它会构建站点并且它可以作为静态站点使用。
  • 谢谢你试试看是否可行:)

标签: github travis-ci github-pages static-site hugo


【解决方案1】:

现在(2020 年 10 月),您无需使用外部 CICD 服务(如 Travis-CI)。

Michelle Mannering 中的“GitHub Action Hero · James Ives and “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

【讨论】:

    【解决方案2】:

    为 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。我在这里写了一篇关于这个设置的详细文章:

    Hosting a Hugo blog on Github Pages with Travis CI

    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
    

    deploy.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 存储库。

    【讨论】:

      猜你喜欢
      • 2014-06-10
      • 1970-01-01
      • 2017-12-22
      • 1970-01-01
      • 2019-01-06
      • 2014-04-17
      • 2013-12-02
      • 2015-03-28
      • 2018-03-14
      相关资源
      最近更新 更多