GitHub Workflow运行Prettier后提交PR失败,寻求技术指导
GitHub Workflow提交PR格式化代码报错解决
问题描述
我想实现一个GitHub Workflow,自动对Pull Request(PR)中新增/修改的文件执行Prettier代码格式化,并将格式化后的修改提交回PR。已编写Workflow配置,但提交步骤报错,附上代码及错误截图求助。
Workflow代码
name: Pull Request Workflow on: pull_request: types: [opened, reopened, edited, synchronize] jobs: lint-analyse-code: name: Lint & Analyse Code runs-on: ubuntu-latest steps: - name: Step 1 - Checkout Repository uses: actions/checkout@v3 - name: Step 2 - Setup GIT run: | echo "BRANCHES" git branch -v echo "REMOTES" git remote -v echo "GITHUB_REF is $GITHUB_REF" echo "GITHUB_HEAD_REF is $GITHUB_HEAD_REF" echo "GITHUB_BASE_REF is $GITHUB_BASE_REF" git config advice.ignoredHook false - name: Step 3 - Setup Node.js uses: actions/setup-node@v3 - name: Step 4 - Install NPM Dependencies run: npm install - name: Step 5 - Get Changed Files id: changed-files uses: tj-actions/changed-files@v29.0.7 with: since_last_remote_commit: true - name: Step 6 - Prettier run: npm run prettier-npx ${{ steps.changed-files.outputs.all_changed_files }} - name: Step 7 - Commit Changes run: | echo HEAD:$GITHUB_HEAD_REF git config user.email "devops@blahblah.co.za" git config user.name "DevOps" git add . git commit -m "Automatically Applied Prettier [skip actions]" || echo "Nothing to push" git push origin HEAD:$GITHUB_HEAD_REF
错误截图

解决方法
1. 核心问题分析
报错原因主要是Git权限不足和分支checkout方式不正确:
- 默认的
actions/checkout@v3未指定PR分支,且默认凭证无推送权限 - 文件名含空格时,Prettier命令可能无法正确处理
2. 分步修复
(1)修正Checkout步骤
直接checkout PR的目标分支,禁用默认凭证并拉取完整历史:
- name: Checkout Repository uses: actions/checkout@v3 with: ref: ${{ github.head_ref }} # 拉取PR的源分支 persist-credentials: false # 避免默认凭证干扰 fetch-depth: 0 # 拉取完整仓库历史
(2)配置Git推送凭证
添加凭证配置,使用仓库的GITHUB_TOKEN(同仓库PR适用):
- name: Configure Git Credentials run: | git config --global credential.helper store echo "https://${{ secrets.GITHUB_TOKEN }}:@github.com" > ~/.git-credentials
如果是处理fork过来的PR,需在仓库Secrets中添加带repo权限的个人访问令牌(PAT),并将上述代码中的${{ secrets.GITHUB_TOKEN }}替换为${{ secrets.PAT }}。
(3)修复Prettier命令
循环处理每个变更文件,避免文件名含空格导致的错误:
- name: Run Prettier on Changed Files run: | for file in ${{ steps.changed-files.outputs.all_changed_files }}; do if [ -f "$file" ]; then npx prettier --write "$file" fi done
3. 完整修复后的Workflow
name: Pull Request Workflow on: pull_request: types: [opened, reopened, edited, synchronize] jobs: lint-analyse-code: name: Lint & Analyse Code runs-on: ubuntu-latest steps: - name: Checkout Repository uses: actions/checkout@v3 with: ref: ${{ github.head_ref }} persist-credentials: false fetch-depth: 0 - name: Setup Git run: | git config user.email "devops@blahblah.co.za" git config user.name "DevOps" git config advice.ignoredHook false - name: Setup Node.js uses: actions/setup-node@v3 with: node-version: 20 # 指定Node版本,避免环境差异 - name: Install NPM Dependencies run: npm install - name: Get Changed Files id: changed-files uses: tj-actions/changed-files@v29.0.7 with: since_last_remote_commit: true - name: Run Prettier on Changed Files run: | for file in ${{ steps.changed-files.outputs.all_changed_files }}; do if [ -f "$file" ]; then npx prettier --write "$file" fi done - name: Configure Git Credentials run: | git config --global credential.helper store echo "https://${{ secrets.GITHUB_TOKEN }}:@github.com" > ~/.git-credentials - name: Commit and Push Changes run: | git add . git commit -m "Automatically Applied Prettier [skip actions]" || echo "No changes to commit" git push origin HEAD:${{ github.head_ref }}
额外注意事项
- 提交消息中的
[skip actions]可避免触发Workflow循环执行 - 处理fork的PR时,必须使用个人访问令牌,默认GITHUB_TOKEN无推送权限
- 指定Node.js版本可减少依赖安装的环境差异问题
内容的提问来源于stack exchange,提问作者Peter Guest
相关产品推荐
相关产品推荐

