Hugo Automation with Travis-ci and Github pages

时间秒杀一切 提交于 2021-02-08 04:54:21

问题


I am using github to host my blog and use the static site Generator HUGO to make it possible but its too tedious to make it offline and compile it then upload the public folder to gh-pages or make it available in docs folder.

So i wanted to automate the process so whenever i create a new .md file in content it should generate the static site and copy the public folder to gh-pages or the following combination -

  • Source files in "source" branch and content of "public" published to master [for user and organization pages]
  • Source files in master and publish "public" folder contents to "gh-pages"
  • Any other method you'd like to propose

Note: I mainly want to use Travis-ci but any other automation platform would also be cool


回答1:


One nice way to set up a Hugo blog for GitHub Pages is to use two separate repositories:

  • the first repository contains the blog source,
  • the second repository contains the generated content.

Name the second repository username.github.io (using your GitHub username). GitHub Pages will automatically deploy it to https://username.github.io/.

Then add the second repository as a git submodule to the first repository. The submodule needs to be located at ./public, which is where Hugo generates the static content. This allows you to easily push the generated content to GitHub.

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

This process is explained in more detail in the official Hugo tutorial Hosting on GitHub.


Continuous Integration

If you want to have full automation, you can set up Travis CI for the first repository. I wrote a detailed article about this setup here:

Hosting a Hugo blog on Github Pages with Travis CI

Travis CI invokes Hugo and pushes the generated content back to GitHub, where it will be deployed by GitHub Pages. For this, you need a .travis.yml file and a small deployment script:

.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

Finally, set up an environment variable GITHUB_AUTH_SECRET on Travis CI to provide access to the username.github.io repository. The blog article also explains how to use a separate bot account for this, restricting CI access to the username.github.io repository.




回答2:


This days (Oct. 2020), you would not need to use an external CICD service (like Travis-CI).

As described in "GitHub Action Hero · James Ives and “GitHub Pages Deploy” " from Michelle Mannering, you can use GitHub Actions.

Specifically, James Ives's GitHub Pages Deploy Action:

This GitHub Action will automatically deploy your project to GitHub Pages.
It can be configured to push your production-ready code into any branch you'd like, including gh-pages and docs.
It can also handle cross repository deployments too.

Example:

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


来源:https://stackoverflow.com/questions/42758355/hugo-automation-with-travis-ci-and-github-pages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!