Skip to content

Create 什么是 DigitalOcean?为什么选择它?.md #5

Create 什么是 DigitalOcean?为什么选择它?.md

Create 什么是 DigitalOcean?为什么选择它?.md #5

Workflow file for this run

name: Deploy Hugo site to Pages
on:
push:
branches: [ main ]
schedule:
- cron: '0 2 * * *' # UTC 每天2点
workflow_dispatch:
permissions:
contents: read
pages: write
id-token: write
concurrency:
group: "pages"
cancel-in-progress: false
jobs:
build-deploy:
runs-on: ubuntu-latest
environment:
name: github-pages
url: ${{ steps.deployment.outputs.page_url }}
env:
CUSTOM_DOMAIN: ${{ github.event.repository.pages.cname }}
steps:
# 1. 检出代码
- name: Checkout
uses: actions/checkout@v4
with:
submodules: 'recursive'
# 2. 设置 Hugo
- name: Setup Hugo
uses: peaceiris/actions-hugo@v2
with:
hugo-version: 'latest'
extended: true
# 3. 配置 GitHub Pages
- name: Setup Pages
uses: actions/configure-pages@v4
# 4. 下载 PaperMod 主题 & 处理文档
- name: Download PaperMod theme
run: |
mkdir -p themes
git clone https://github.com/adityatelange/hugo-PaperMod.git themes/PaperMod
# 这里的重点:把所有 .md 文件都写为 type = "post"
generate_front_matter() {
local file="$1"
local title=$(basename "$file" .md)
local date=$(date -u +"%Y-%m-%dT%H:%M:%SZ")
local category=$(dirname "$file" | sed 's|^./||')
# 如果已有 front matter,就直接复制
if sed -n '1{/^---$/h}; /^---$/{x;p;q}; H' "$file" | grep -q "^---$"; then
cp "$file" "content/$category/$title.md"
return
fi
# 若没有 front matter,自动生成
{
echo "---"
echo "title: \"$title\""
echo "date: $date"
# 想按目录来区分分类,可保留这一行
echo "categories: [\"$category\"]"
# ★★ 修复点:统一指定所有文章的 type = "post" ★★
echo "type: \"post\""
echo "---"
cat "$file"
} > "content/$category/$title.md"
}
# 遍历所有 md 文件(排除 README.md)
find . -type f -name "*.md" ! -name "README.md" | while read file; do
dir=$(dirname "$file" | sed 's|^./||')
mkdir -p "content/$dir"
generate_front_matter "$file"
done
# 5. 获取自定义域名
- name: Retrieve custom domain
id: fetch-domain
run: |
customDomain=$(curl -s -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
https://api.github.com/repos/${{ github.repository_owner }}/${{ github.event.repository.name }}/pages \
| jq -r '.cname // ""')
echo "CUSTOM_DOMAIN=$customDomain" >> $GITHUB_ENV
# 6. 创建并写入 Hugo 配置
- name: Configure Hugo
run: |
if [ -n "$CUSTOM_DOMAIN" ]; then
domain="$CUSTOM_DOMAIN"
else
owner="${GITHUB_REPOSITORY_OWNER}"
repo="$(echo $GITHUB_REPOSITORY | cut -d'/' -f2)"
domain="$owner.github.io/$repo"
fi
cat > config.toml << EOL
baseURL = "https://$domain"
languageCode = "zh-cn"
title = "${{ github.repository_owner }}'s Space"
theme = "PaperMod"
[params]
# 指定 mainSections 只有一个: "post"
# 因为我们在上面把所有文件都设为 type = "post"
mainSections = ["post"]
defaultTheme = "auto"
ShowReadingTime = true
ShowShareButtons = false
ShowPostNavLinks = true
ShowBreadCrumbs = true
ShowCodeCopyButtons = true
ShowToc = true
[params.homeInfoParams]
Title = "欢迎来到 ${{ github.repository_owner }} 的文档站"
Content = "在喧嚣时代中静心思考,用文字传递理性与希望。"
[[params.socialIcons]]
name = "github"
url = "https://github.com/${{ github.repository_owner }}"
[outputs]
home = ["HTML", "RSS", "JSON"]
# 想自定义分类和标签名称,可在这里改
[taxonomies]
category = "categories"
tag = "tags"
[menu]
main = [
{identifier = "archives", name = "Archives", url = "/archives/", weight = 10},
{identifier = "categories", name = "Categories", url = "/categories/", weight = 20},
{identifier = "search", name = "Search", url = "/search/", weight = 30},
{identifier = "tags", name = "Tags", url = "/tags/", weight = 40}
]
EOL
# 至少留个 content 目录
mkdir -p content
# 搜索页面
cat > content/search.md << EOL
---
title: "Search"
layout: "search"
summary: "search"
placeholder: "搜索文章..."
---
EOL
# 归档页面
cat > content/archives.md << EOL
---
title: "Archives"
layout: "archives"
url: "/archives/"
summary: "archives"
---
EOL
# 7. Hugo 构建
- name: Build site
run: hugo --minify
# 8. 上传构建产物
- name: Upload artifact
uses: actions/upload-pages-artifact@v3
with:
path: ./public
# 9. 部署到 GitHub Pages
- name: Deploy to GitHub Pages
id: deployment
uses: actions/deploy-pages@v4