11# Sample workflow for building and deploying a Hugo site to GitHub Pages
2- name : Deploy Hugo site to Pages
2+ name : Deploy Hugo site to gh-pages branch
33
44on :
55 # Runs on pushes targeting the default branch
66 push :
77 branches :
8- - dev
8+ - dev # 或者您希望触发部署的分支,例如 main 或 master
99
1010 # Allows you to run this workflow manually from the Actions tab
1111 workflow_dispatch :
1212
13- # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
13+ # Sets permissions of the GITHUB_TOKEN to allow deployment
1414permissions :
15- contents : read
16- pages : write
17- id-token : write
15+ contents : write # 需要写入权限来推送分支
16+ pages : write # 如果您仍然使用GitHub Pages服务(尽管是推送到gh-pages),保留此权限
17+ id-token : write # 如果您使用OIDC,保留此权限
1818
1919# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
2020# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
@@ -25,7 +25,6 @@ concurrency:
2525# Default to bash
2626defaults :
2727 run :
28- # GitHub-hosted runners automatically enable `set -eo pipefail` for Bash shells.
2928 shell : bash
3029
3130jobs :
5352 with :
5453 submodules : recursive
5554 fetch-depth : 0
56- - name : Setup Pages
57- id : pages
58- uses : actions/configure-pages@v5
55+ # 如果您不使用actions/configure-pages和actions/deploy-pages,则不需要此步骤
56+ # - name: Setup Pages
57+ # id: pages
58+ # uses: actions/configure-pages@v5
5959 - name : Install Node.js dependencies
6060 run : " [[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true"
6161 - name : Cache Restore
@@ -70,10 +70,15 @@ jobs:
7070 run : git config core.quotepath false
7171 - name : Build with Hugo
7272 run : |
73+ # 注意:如果您不使用actions/configure-pages,则baseURL可能需要硬编码或从其他地方获取
74+ # 如果您的GitHub Pages URL是 username.github.io/repo-name/
75+ # 则 baseURL 应该是 /repo-name/
76+ # 如果是 username.github.io/
77+ # 则 baseURL 应该是 /
7378 hugo \
7479 --gc \
7580 --minify \
76- --baseURL "${{ steps.pages.outputs.base_url }} /" \
81+ --baseURL "/" \ # 根据您的实际情况修改此项
7782 --cacheDir "${{ runner.temp }}/hugo_cache"
7883 - name : Cache Save
7984 id : cache-save
@@ -82,19 +87,41 @@ jobs:
8287 path : |
8388 ${{ runner.temp }}/hugo_cache
8489 key : ${{ steps.cache-restore.outputs.cache-primary-key }}
85- - name : Upload artifact
86- uses : actions/upload-pages-artifact@v3
87- with :
88- path : ./public
90+ # 移除 Upload artifact 步骤,因为我们将直接推送到分支
91+ # - name: Upload artifact
92+ # uses: actions/upload-pages-artifact@v3
93+ # with:
94+ # path: ./public
8995
9096 # Deployment job
9197 deploy :
9298 environment :
93- name : github-pages
94- url : ${{ steps.deployment.outputs.page_url }}
99+ name : github-pages # 仍然可以使用此环境,但实际部署方式不同
100+ url : ${{ steps.deployment.outputs.page_url }} # 此URL将由peaceiris/actions-gh-pages提供
95101 runs-on : ubuntu-latest
96- needs : build
102+ needs : build # 确保在构建完成后运行
97103 steps :
98- - name : Deploy to GitHub Pages
99- id : deployment
100- uses : actions/deploy-pages@v4
104+ - name : Checkout
105+ uses : actions/checkout@v4
106+ with :
107+ submodules : recursive
108+ fetch-depth : 0 # 确保获取完整的历史记录以便推送
109+
110+ - name : Configure Git
111+ run : |
112+ git config user.name "github-actions[bot]"
113+ git config user.email "github-actions[bot]@users.noreply.github.com"
114+
115+ - name : Deploy to gh-pages branch
116+ uses : peaceiris/actions-gh-pages@v3 # 使用此Action来推送
117+ id : deployment # 为输出URL设置ID
118+ with :
119+ github_token : ${{ secrets.GITHUB_TOKEN }}
120+ publish_dir : ./public # 指定要发布到gh-pages分支的目录
121+ publish_branch : gh-pages # 指定目标分支
122+ user_name : " github-actions[bot]"
123+ user_email : " github-actions[bot]@users.noreply.github.com"
124+ # 如果您的站点是项目页面(例如 username.github.io/repo-name/),
125+ # 并且您希望在gh-pages分支的根目录中包含子目录,请取消注释以下行
126+ # cname: example.com # 如果您有自定义域名,可以在这里设置
127+ # keep_files: true # 如果您想保留gh-pages分支上的现有文件
0 commit comments