使用 Github Actions 部署 Hexo
使用 Github Actions 部署 Hexo
事件触发器
on: workflow_dispatch: push: branches: - main
|
检出仓库分支源码
- name: Checkout Repository main branch uses: actions/checkout@v3 with: submodules: "true"
|
安装 Node.js
- name: Setup Node.js 24.x uses: actions/setup-node@main with: node-version: 24 registry-url: https://registry.npmjs.org
|
安装部署密钥
- name: Setup Deploy Private Key env: DEPLOY_KEY: ${{ secrets.TOKEN }} NAME: ${{ secrets.NAME }} EMAIL: ${{ secrets.EMAIL }} run: | mkdir -p ~/.ssh/ echo "$DEPLOY_KEY" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-keyscan github.com >> ~/.ssh/known_hosts git config --global user.email "$EMAIL" git config --global user.name "$NAME"
|
缓存 node modules
- name: Cache node modules uses: actions/cache@v4 id: cache with: path: node_modules key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} restore-keys: | ${{ runner.os }}-node-
|
安装依赖
- name: Install Dependencies if: steps.cache.outputs.cache-hit != 'true' run: | npm install
|
生成站点静态资源
- name: Generate Public Files run: | # Restore last modified time git ls-files -z | while read -d '' path; do touch -d "$(git log -1 --format="@%ct" "$path")" "$path"; done hexo clean && hexo g
|
部署到 Github Pages
- name: Deploy Github uses: peaceiris/actions-gh-pages@v3 with: deploy_key: ${{ secrets.TOKEN }} publish_dir: ./public user_name: "github-actions[bot]" user_email: "github-actions[bot]@users.noreply.github.com" external_repository: XXX/XXX.github.io publish_branch: main
|
完整 Actions 脚本
.github/workflows/DeployToGithubPages.ymlname: Deploy To Github Pages
on: workflow_dispatch: push: branches: - main
jobs: deploy: if: ${{ contains(github.event.head_commit.message, 'draft')!= true }} name: Deploy Hexo Public To Pages runs-on: ubuntu-latest env: TZ: Asia/Shanghai steps: - name: Checkout Repository main branch uses: actions/checkout@v3 with: submodules: "true" - name: Setup Node.js 24.x uses: actions/setup-node@main with: node-version: 24 registry-url: https://registry.npmjs.org - name: Setup Deploy Private Key env: DEPLOY_KEY: ${{ secrets.TOKEN }} NAME: ${{ secrets.NAME }} EMAIL: ${{ secrets.EMAIL }} run: | mkdir -p ~/.ssh/ echo "$DEPLOY_KEY" > ~/.ssh/id_rsa chmod 600 ~/.ssh/id_rsa ssh-keyscan github.com >> ~/.ssh/known_hosts git config --global user.email "$EMAIL" git config --global user.name "$NAME"
- name: Install Globel Dependencies run: | npm install hexo-cli -g - name: Cache node modules uses: actions/cache@v4 id: cache with: path: node_modules key: ${{ runner.os }}-node-${{ hashFiles('**/package.json') }} restore-keys: | ${{ runner.os }}-node- - name: Install Dependencies if: steps.cache.outputs.cache-hit != 'true' run: | npm install - name: Generate Public Files run: | # Restore last modified time git ls-files -z | while read -d '' path; do touch -d "$(git log -1 --format="@%ct" "$path")" "$path"; done hexo clean && hexo g
- name: Deploy Github uses: peaceiris/actions-gh-pages@v3 with: deploy_key: ${{ secrets.TOKEN }} publish_dir: ./public user_name: "github-actions[bot]" user_email: "github-actions[bot]@users.noreply.github.com" external_repository: XXX/XXX.github.io publish_branch: main
|