Files
auto-update/.gitea/workflows/nextcloud_sync&build.yml

159 lines
6.4 KiB
YAML
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
name: Sync Nextcloud and Build Docker
# 触发条件
on:
schedule:
# 每天 18:00(UTC)执行一次(相当于北京时间 02:00),周日强制全量构建
- cron: '0 18 * * *'
push:
branches:
- main
paths:
- 'Dockerfile'
workflow_dispatch: # 支持手动触发
# 环境变量(可根据需要自行修改)
env:
NEXTCLOUD_ZIP: latest.zip
NEXTCLOUD_DIR: nextcloud
jobs:
sync-and-build:
runs-on: ubuntu-latest
permissions:
contents: read
packages: write # 推送镜像需要
steps:
# ==================== 1. 基础准备 ====================
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0 # 需要完整历史来比较变更
- name: Free disk space (optional)
run: sudo df -h && sudo rm -rf /usr/share/dotnet /usr/local/lib/android /opt/ghc
# ==================== 2. 同步 Nextcloud ====================
- name: Create nextcloud directory
run: mkdir -p ${{ env.NEXTCLOUD_DIR }}
- name: Download latest.zip
id: download
run: |
curl -fsSL -o ${{ env.NEXTCLOUD_ZIP }} \
https://download.nextcloud.com/server/releases/latest.zip || exit 1
- name: Unzip Nextcloud
run: |
unzip -qo ${{ env.NEXTCLOUD_ZIP }} -d ${{ env.NEXTCLOUD_DIR }}
rm -f ${{ env.NEXTCLOUD_ZIP }}
# ==================== 3. 检测是否有文件变更 ====================
- name: Check for changes
id: check_changes
run: |
# 如果是 push 事件且路径匹配,已在触发器里过滤,这里再做一次细粒度检查
if [ "${{ github.event_name }}" = "push" ]; then
echo "Push 事件,文件已匹配,直接标记为需要构建"
echo "has_changes=true" >> $GITHUB_OUTPUT
exit 0
fi
# 比较本地 nextcloud 目录与仓库中已有的差异
git config --global --add safe.directory "$GITHUB_WORKSPACE"
git fetch --depth=1 origin ${{ github.ref_name }} || true
# 统计差异文件数
CHANGES=$(git diff --name-only HEAD ${{ env.NEXTCLOUD_DIR }} | wc -l)
if [ "$CHANGES" -gt 0 ]; then
echo "检测到 ${{ env.NEXTCLOUD_DIR }} 目录有 $CHANGES 处变更"
echo "has_changes=true" >> $GITHUB_OUTPUT
else
echo "无文件变更"
echo "has_changes=false" >> $GITHUB_OUTPUT
fi
# ==================== 4. 判断是否需要构建 Docker ====================
- name: Decide whether to build
id: should_build
run: |
# 1. 周日定时任务强制构建
if [ "${{ github.event_name }}" = "schedule" ] && [ "$(date +%w)" = "0" ]; then
echo "周日定时任务 → 强制构建"
echo "build=true" >> $GITHUB_OUTPUT
# 2. push 触发且路径匹配(已在 trigger 中过滤,这里直接 true
elif [ "${{ github.event_name }}" = "push" ]; then
echo "push 触发(Dockerfile)→ 构建"
echo "build=true" >> $GITHUB_OUTPUT
# 3. 手动触发
elif [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "手动触发 → 构建"
echo "build=true" >> $GITHUB_OUTPUT
# 4. 有新的 Nextcloud 文件变更
elif [ "${{ steps.check_changes.outputs.has_changes }}" = "true" ]; then
echo "Nextcloud 同步有变更 → 构建"
echo "build=true" >> $GITHUB_OUTPUT
# 5. 其它情况(包括非周日 schedule 且无变更)
else
echo "无变更且非强制构建时间 → 跳过构建"
echo "build=false" >> $GITHUB_OUTPUT
fi
# ==================== 5. 仅在需要时执行 Docker 构建 ====================
- name: Set up Docker Buildx
if: steps.should_build.outputs.build == 'true'
uses: docker/setup-buildx-action@v3
- name: Login to Custom Registry
if: steps.should_build.outputs.build == 'true'
uses: docker/login-action@v3
with:
registry: ${{ secrets.REGISTRY_SERVER }}/${{ gitea.repository_owner }}/nextcloud-docker
username: ${{ gitea.actor }}
password: ${{ secrets.REGISTRY_TOKEN }}
- name: Generate Beijing time version tag
if: steps.should_build.outputs.build == 'true'
id: version
run: |
echo "tag=$(TZ='Asia/Shanghai' date '+%Y%m%d-%H%M%S')" >> $GITHUB_OUTPUT
- name: Build & Push Docker image
if: steps.should_build.outputs.build == 'true'
uses: docker/build-push-action@v6
with:
context: .
file: ./Dockerfile
platforms: linux/amd64,linux/arm64 # 可按需调整
push: true
tags: |
${{ secrets.REGISTRY_SERVER }}/${{ gitea.repository_owner }}/nextcloud-docker:latest
${{ secrets.REGISTRY_SERVER }}/${{ gitea.repository_owner }}/nextcloud-docker:${{ steps.version.outputs.tag }}
cache-from: type=registry,ref=${{ secrets.REGISTRY_SERVER }}/${{ gitea.repository_owner }}/nextcloud-docker:latest
cache-to: type=inline,mode=max
# ==================== 6. 提交同步的 Nextcloud 文件(可选)================
# 如果你希望把下载的 nextcloud 目录也提交回仓库(保持同步记录),打开下面这段
# - name: Commit synced Nextcloud files
# if: steps.check_changes.outputs.has_changes == 'true'
# run: |
# git config user.name "github-actions[bot]"
# git config user.email "github-actions@users.noreply.github.com"
# git add ${{ env.NEXTCLOUD_DIR }}
# git commit -m "chore: sync nextcloud $(date '+%Y-%m-%d %H:%M:%S')" || echo "Nothing to commit"
# git push origin HEAD:${{ github.ref_name }}
# ==================== 7. 结束提示 ====================
- name: Build summary
if: always()
run: |
echo "=== 工作流执行摘要 ==="
echo "触发方式: ${{ github.event_name }}"
echo "是否构建: ${{ steps.should_build.outputs.build }}"
echo "Nextcloud 变更: ${{ steps.check_changes.outputs.has_changes }}"
[ "${{ steps.should_build.outputs.build }}" = "true" ] && echo "镜像标签: ${{ steps.version.outputs.tag }}" || true