name: Upstream Sync permissions: contents: write issues: write on: schedule: - cron: "0 */6 * * *" workflow_dispatch: jobs: sync_latest_from_upstream: name: Sync latest commits from upstream repo runs-on: ubuntu-latest if: ${{ github.event.repository.fork }} steps: - uses: actions/checkout@v4 with: ref: master fetch-depth: 0 # Must use a PAT with repo scope — GITHUB_TOKEN cannot push in fork scheduled workflows token: ${{ secrets.UPSTREAM_TOKEN }} - name: Check UPSTREAM_TOKEN run: | if [ -z "$UPSTREAM_TOKEN" ]; then echo "::error::UPSTREAM_TOKEN secret is not set. Please add a Personal Access Token with repo scope to your repository secrets." exit 1 fi env: UPSTREAM_TOKEN: ${{ secrets.UPSTREAM_TOKEN }} - name: Sync upstream changes id: sync run: | git remote add upstream https://github.com/alexta69/metube.git 2>/dev/null || true git fetch upstream master LOCAL=$(git rev-parse HEAD) UPSTREAM=$(git rev-parse upstream/master) if [ "$LOCAL" = "$UPSTREAM" ]; then echo "Already up-to-date" echo "has_changes=false" >> "$GITHUB_OUTPUT" exit 0 fi echo "Local: $LOCAL" echo "Upstream: $UPSTREAM" echo "has_changes=true" >> "$GITHUB_OUTPUT" # Configure git for the merge commit git config user.name "github-actions[bot]" git config user.email "github-actions[bot]@users.noreply.github.com" # Try merge first (preserves both histories) if git merge upstream/master --no-edit; then git push origin master echo "Merge succeeded" echo "sync_result=success" >> "$GITHUB_OUTPUT" else echo "Merge conflict detected, aborting" git merge --abort echo "sync_result=conflict" >> "$GITHUB_OUTPUT" exit 1 fi - name: Sync check — create issue on failure if: failure() uses: actions/github-script@v7 with: script: | await github.rest.issues.create({ owner: context.repo.owner, repo: context.repo.repo, title: "🚨 同步失败 | Sync Fail", labels: ["🚨 Sync Fail"], body: `Due to conflicts or workflow changes in the [alexta69/metube](https://github.com/alexta69/metube) upstream repository, the automatic sync has failed. Please manually sync your fork and resolve any conflicts. 由于上游仓库 [alexta69/metube](https://github.com/alexta69/metube) 存在冲突或 workflow 变更,自动同步失败。请手动 Sync Fork 并解决冲突。` });