update workflow #10
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Deploy public | |
on: | |
push: | |
branches: ["main"] | |
workflow_dispatch: | |
inputs: | |
hugoVersion: | |
description: 'Hugo version to use' | |
required: false | |
default: '0.128.0' | |
type: string | |
baseUrl: | |
description: 'Site base URL' | |
required: false | |
type: string | |
environment: | |
description: 'Hugo environment' | |
required: false | |
default: 'production' | |
type: string | |
permissions: | |
contents: write | |
actions: read | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
env: | |
HUGO_VERSION: ${{ inputs.hugoVersion || '0.128.0' }} | |
steps: | |
- name: Install Hugo CLI | |
run: | | |
wget -O ${{ runner.temp }}/hugo.deb https://github.com/gohugoio/hugo/releases/download/v${HUGO_VERSION}/hugo_extended_${HUGO_VERSION}_linux-amd64.deb \ | |
&& sudo dpkg -i ${{ runner.temp }}/hugo.deb | |
- name: Install Dart Sass | |
run: sudo snap install dart-sass | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Install Node.js dependencies | |
run: "[[ -f package-lock.json || -f npm-shrinkwrap.json ]] && npm ci || true" | |
- name: Build with Hugo | |
env: | |
HUGO_CACHEDIR: ${{ runner.temp }}/hugo_cache | |
HUGO_ENVIRONMENT: ${{ inputs.environment || 'production' }} | |
run: | | |
hugo --minify --gc --cleanDestinationDir | |
- name: Upload artifact | |
uses: actions/upload-artifact@v3 | |
with: | |
name: hugo-public | |
path: ./public | |
retention-days: 1 | |
push_public: | |
runs-on: ubuntu-latest | |
needs: build | |
steps: | |
- name: Checkout repo | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Download artifact | |
uses: actions/download-artifact@v3 | |
with: | |
name: hugo-public | |
path: ./public | |
- name: Setup Git and sync public branch | |
run: | | |
git config user.name "github-actions" | |
git config user.email "actions@github.com" | |
# 获取当前提交信息 | |
COMMIT_MSG=$(git log -1 --pretty=%B) | |
COMMIT_HASH=$(git rev-parse --short HEAD) | |
# 如果 public 分支不存在则创建 | |
if ! git show-ref --verify --quiet refs/remotes/origin/public; then | |
git checkout --orphan public | |
git rm -rf . | |
cp -r public/* . | |
git add . | |
git commit -m "Initial public branch from main ${COMMIT_HASH}" | |
git push origin public | |
else | |
# 确保临时目录存在 | |
mkdir -p temp_public | |
# 保存构建产物 | |
cp -r public/* temp_public/ | |
# 切换分支 | |
git fetch origin public | |
git checkout public || git checkout -b public --track origin/public | |
# 清理当前目录(保留 .git) | |
find . -mindepth 1 -maxdepth 1 ! -name '.git' -exec rm -rf {} + | |
# 恢复构建产物 | |
cp -r temp_public/* . | |
rm -rf temp_public | |
git add . | |
if git diff --staged --quiet; then | |
echo "No changes detected" | |
exit 0 | |
fi | |
git commit -m "Update from main ${COMMIT_HASH}" -m "Source commit message:" -m "${COMMIT_MSG}" | |
git push origin public | |
fi |