|
| 1 | +# refusing to allow a GitHub App to create or update workflow .github/workflows/XXXX.yml without workflows permission |
| 2 | +# 遇到这个错误,需要检查三点: |
| 3 | +# 1. 仓库设置 Workflow permissions 允许写权限 |
| 4 | +# 2. 使用的 TOKEN 具有 workflows 权限,如果是组织仓库还需要检查 TOKEN 的作用域 |
| 5 | +# 3. actions/checkout 也需要使用 workflows 权限的 TOKEN |
| 6 | +# ------------------------------------------------------------------- |
| 7 | +# https://github.com/Soltus |
| 8 | + |
| 9 | +name: Test |
| 10 | + |
| 11 | +on: |
| 12 | + schedule: |
| 13 | + # ┌───────────── minute (0 - 59) |
| 14 | + # │ ┌───────────── hour (0 - 23) |
| 15 | + # │ │ ┌───────────── day of the month (1 - 31) |
| 16 | + # │ │ │ ┌───────────── month (1 - 12 or JAN-DEC) |
| 17 | + # │ │ │ │ ┌───────────── day of the week (0 - 6 or SUN-SAT) |
| 18 | + # │ │ │ │ │ |
| 19 | + # │ │ │ │ │ |
| 20 | + # │ │ │ │ │ |
| 21 | + # * * * * * |
| 22 | + - cron: '0 7 * * *' |
| 23 | + # scheduled at 07:00 every day |
| 24 | + |
| 25 | + workflow_dispatch: # click the button on Github repo! |
| 26 | + inputs: |
| 27 | + sync_test_mode: # Adds a boolean option that appears during manual workflow run for easy test mode config |
| 28 | + description: 'Fork Sync Test Mode' |
| 29 | + type: boolean |
| 30 | + default: false |
| 31 | + |
| 32 | +env: |
| 33 | + target_sync_branch: "main" |
| 34 | + upstream_sync_repo: "prototype-validation/dist-repo-use-branch" # 根据实际情况修改 |
| 35 | + upstream_sync_branch: "main_-_dist_-_test" |
| 36 | + |
| 37 | +jobs: |
| 38 | + sync_latest_from_upstream: |
| 39 | + runs-on: ubuntu-latest |
| 40 | + name: Sync latest commits from upstream repo |
| 41 | + |
| 42 | + steps: |
| 43 | + # REQUIRED step |
| 44 | + # Step 1: run a standard checkout action, provided by github |
| 45 | + - name: Checkout target repo |
| 46 | + uses: actions/checkout@v4 |
| 47 | + with: |
| 48 | + fetch-depth: 0 |
| 49 | + token: ${{ secrets.TOEKN_FOREVER_FULL_WRITE }} # 使用具有完全写入权限的 TOKEN(必须包含 workflows ) |
| 50 | + # REQUIRED if your upstream repo is private (see wiki), 启用会造成 check-branch-exists 失败 |
| 51 | + # persist-credentials: false |
| 52 | + |
| 53 | + - name: Set up Git |
| 54 | + run: | |
| 55 | + git config --global user.name 'GitHub Actions' |
| 56 | + git config --global user.email '[email protected]' |
| 57 | +
|
| 58 | + - name: Check if target_sync_branch exists in the remote |
| 59 | + id: check-branch-exists |
| 60 | + run: | |
| 61 | + # grep -qE 使用了 -E 参数来启用扩展的正则表达式, 并且使用了 $ 符号来确保匹配的字符串以 main 结尾, 达到精准匹配的目的 |
| 62 | + if git ls-remote --heads origin | grep -qE 'refs/heads/${{ env.target_sync_branch }}$'; then |
| 63 | + echo "branch_exists=true" >> $GITHUB_OUTPUT |
| 64 | + echo "Remote branch ${{ env.target_sync_branch }} exists." |
| 65 | + git checkout ${{ env.target_sync_branch }} |
| 66 | + else |
| 67 | + echo "branch_exists=false" >> $GITHUB_OUTPUT |
| 68 | + echo "Remote branch ${{ env.target_sync_branch }} does not exist." |
| 69 | + git remote add upstream https://github.com/${{ env.upstream_sync_repo }}.git |
| 70 | + # 创建一个临时分支来fetch远程分支的内容 |
| 71 | + git fetch upstream ${{ env.upstream_sync_branch }}:temp_${{ env.upstream_sync_branch }} |
| 72 | + # 基于临时分支创建新的本地分支 |
| 73 | + git checkout -b ${{ env.target_sync_branch }} temp_${{ env.upstream_sync_branch }} |
| 74 | + # 删除临时分支 |
| 75 | + git branch -d temp_${{ env.upstream_sync_branch }} |
| 76 | + # 推送新分支到远程仓库 |
| 77 | + git push origin ${{ env.target_sync_branch }} |
| 78 | + fi |
| 79 | +
|
| 80 | + # REQUIRED step |
| 81 | + # Step 2: run the sync action |
| 82 | + - name: Sync upstream changes |
| 83 | + id: sync |
| 84 | + if: steps.check-branch-exists.outputs.branch_exists == 'true' |
| 85 | + |
| 86 | + with: |
| 87 | + target_sync_branch: ${{ env.target_sync_branch }} |
| 88 | + target_repo_token: ${{ secrets.TOEKN_FOREVER_FULL_WRITE }} # 可能涉及 workflows 的修改,需要对应权限 |
| 89 | + upstream_sync_branch: ${{ env.upstream_sync_branch }} |
| 90 | + upstream_sync_repo: ${{ env.upstream_sync_repo }} |
| 91 | + test_mode: ${{ inputs.sync_test_mode }} |
| 92 | + |
| 93 | + # Step 3: Display a sample message based on the sync output var 'has_new_commits' |
| 94 | + - name: New commits found |
| 95 | + if: steps.sync.outputs.has_new_commits == 'true' |
| 96 | + run: echo "New commits were found to sync." |
| 97 | + |
| 98 | + - name: No new commits |
| 99 | + if: steps.sync.outputs.has_new_commits == 'false' |
| 100 | + run: echo "There were no new commits." |
| 101 | + |
| 102 | + - name: Show value of 'has_new_commits' |
| 103 | + run: echo ${{ steps.sync.outputs.has_new_commits }} |
0 commit comments