├── Dockerfile ├── entrypoint.sh └── README.md /Dockerfile: -------------------------------------------------------------------------------- 1 | FROM alpine:latest 2 | 3 | LABEL "com.github.actions.name"="Hugo for GitHub Pages" 4 | LABEL "com.github.actions.description"="Builds and deploys the project to GitHub Pages" 5 | LABEL "com.github.actions.icon"="home" 6 | LABEL "com.github.actions.color"="red" 7 | 8 | LABEL "repository"="https://github.com/khanhicetea/gh-actions-hugo-deploy-gh-pages" 9 | LABEL "homepage"="https://github.com/khanhicetea/gh-actions-hugo-deploy-gh-pages" 10 | LABEL "maintainer"="KhanhIceTea " 11 | 12 | LABEL "Name"="Hugo for GitHub Pages" 13 | LABEL "Version"="0.1.0" 14 | 15 | ENV LC_ALL C.UTF-8 16 | ENV LANG en_US.UTF-8 17 | ENV LANGUAGE en_US.UTF-8 18 | 19 | RUN apk add --no-cache \ 20 | git \ 21 | openssh-client \ 22 | libc6-compat \ 23 | libstdc++ 24 | 25 | COPY entrypoint.sh /entrypoint.sh 26 | 27 | ENTRYPOINT ["/entrypoint.sh"] 28 | -------------------------------------------------------------------------------- /entrypoint.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | echo '=================== Install Hugo ===================' 3 | DOWNLOAD_HUGO_VERSION=${HUGO_VERSION:-0.54.0} 4 | GITHUB_DEPLOY_REPOSITORY=${GITHUB_REMOTE_REPOSITORY:-$GITHUB_REPOSITORY} 5 | GITHUB_DEPLOY_BRANCH=${GITHUB_BRANCH:-"gh-pages"} 6 | echo "Installing Hugo $DOWNLOAD_HUGO_VERSION" 7 | wget -O /tmp/hugo.tar.gz https://github.com/gohugoio/hugo/releases/download/v${DOWNLOAD_HUGO_VERSION}/hugo_extended_${DOWNLOAD_HUGO_VERSION}_Linux-64bit.tar.gz &&\ 8 | tar -zxf /tmp/hugo.tar.gz -C /tmp &&\ 9 | mv /tmp/hugo /usr/local/bin/hugo &&\ 10 | rm /tmp/* 11 | echo '=================== Create deploy key to push ===================' 12 | mkdir /root/.ssh 13 | ssh-keyscan -t rsa github.com > /root/.ssh/known_hosts && \ 14 | echo "${GIT_DEPLOY_KEY}" > /root/.ssh/id_rsa && \ 15 | chmod 400 /root/.ssh/id_rsa 16 | echo '=================== Update all submodules ===================' 17 | git submodule init 18 | git submodule update --recursive --remote 19 | echo '=================== Build site ===================' 20 | HUGO_ENV=production hugo -v --minify -d dist 21 | echo '=================== Publish to GitHub Pages ===================' 22 | cd dist 23 | remote_repo="git@github.com:${GITHUB_DEPLOY_REPOSITORY}.git" && \ 24 | remote_branch=${GITHUB_DEPLOY_BRANCH} && \ 25 | echo "Pushing Builds to $remote_repo:$remote_branch" && \ 26 | git init && \ 27 | git remote add deploy $remote_repo && \ 28 | git checkout $remote_branch || git checkout --orphan $remote_branch && \ 29 | git config user.name "${GITHUB_ACTOR}" && \ 30 | git config user.email "${GITHUB_ACTOR}@users.noreply.github.com" && \ 31 | git add . && \ 32 | echo -n 'Files to Commit:' && ls -l | wc -l && \ 33 | timestamp=$(date +%s%3N) && \ 34 | git commit -m "Automated deployment to GitHub Pages on $timestamp" > /dev/null 2>&1 && \ 35 | git push deploy $remote_branch --force && \ 36 | rm -fr .git && \ 37 | cd ../ 38 | echo '=================== Done ===================' 39 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Hugo Github Action 2 | 3 | GitHub Action for building and publishing Hugo-built site. 4 | 5 | Inspired by [BryanSchuetz/jekyll-deploy-gh-pages](https://github.com/BryanSchuetz/jekyll-deploy-gh-pages) 6 | 7 | ## Secrets 8 | 9 | - `GIT_DEPLOY_KEY` - *Required* your deploy key which has **Write access** 10 | 11 | ## Create Deploy Key 12 | 13 | 1. Generate deploy key `ssh-keygen -t rsa -f hugo -q -N ""` 14 | 1. Then go to "Settings > Deploy Keys" of repository 15 | 1. Add your public key within "Allow write access" option. 16 | 1. Copy your private deploy key to `GIT_DEPLOY_KEY` secret in "Settings > Secrets" 17 | 18 | ## Environment Variables 19 | 20 | - `HUGO_VERSION` : **optional**, default is **0.54.0** - [check all versions here](https://github.com/gohugoio/hugo/releases) 21 | 22 | - `GITHUB_BRANCH` : **optional**, default is **gh-pages** 23 | 24 | - `GITHUB_REMOTE_REPOSITORY` : **optional**, default is **GITHUB_REPOSITORY** 25 | 26 | 27 | ## Example 28 | 29 | **push.yml** (New syntax) 30 | 31 | **Deploy to gh-pages branch**: (under same repo) 32 | 33 | - Note: put the `CNAME` file within your domain name inside `static` folder of compiling branch (master) 34 | 35 | ```yaml 36 | name: Deploy to GitHub Pages 37 | 38 | on: push 39 | 40 | jobs: 41 | hugo-deploy-gh-pages: 42 | runs-on: ubuntu-latest 43 | steps: 44 | - uses: actions/checkout@master 45 | - name: hugo-deploy-gh-pages 46 | uses: khanhicetea/gh-actions-hugo-deploy-gh-pages@master 47 | env: 48 | GIT_DEPLOY_KEY: ${{ secrets.GIT_DEPLOY_KEY }} 49 | HUGO_VERSION: "0.53" 50 | ``` 51 | 52 | 53 | **Deploy to Remote Branch**: 54 | 55 | ```yaml 56 | name: Deploy to Remote Branch 57 | 58 | on: 59 | push: 60 | branches: 61 | - master 62 | 63 | jobs: 64 | hugo-deploy-gh-pages: 65 | runs-on: ubuntu-latest 66 | steps: 67 | - uses: actions/checkout@master 68 | - name: hugo-deploy-gh-pages 69 | uses: khanhicetea/gh-actions-hugo-deploy-gh-pages@master 70 | env: 71 | GITHUB_REMOTE_REPOSITORY: / 72 | GITHUB_BRANCH: 73 | GIT_DEPLOY_KEY: ${{ secrets.GIT_DEPLOY_KEY }} 74 | HUGO_VERSION: "0.58.3" 75 | ``` 76 | 77 | **Note**: make sure to add `GIT_DEPLOY_KEY` in secrets of mentioned `GITHUB_REMOTE_REPOSITORY` 78 | 79 | **main.workflow** (Old syntax - deprecated) 80 | 81 | ```hcl 82 | workflow "Deploy to GitHub Pages" { 83 | on = "push" 84 | resolves = ["hugo-deploy-gh-pages"] 85 | } 86 | 87 | action "hugo-deploy-gh-pages" { 88 | uses = "khanhicetea/gh-actions-hugo-deploy-gh-pages@master" 89 | secrets = [ 90 | "GIT_DEPLOY_KEY" 91 | ] 92 | env = { 93 | HUGO_VERSION = "0.53" 94 | GITHUB_BRANCH = "master" 95 | } 96 | } 97 | ``` 98 | 99 | ## Other actions 100 | 101 | - RPC Ping (Ping Search Engine) : https://github.com/khanhicetea/gh-actions-rpc-ping 102 | 103 | ## LICENSE 104 | 105 | Copyright (c) 2019 106 | 107 | Licensed under the MIT License. 108 | --------------------------------------------------------------------------------