You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Github Actions自动合并PR报错:GITHUB_TOKEN未设置

问题排查:Github Actions自动合并PR时提示GITHUB_TOKEN未设置

我在编写Pull Request创建时自动合并的Github Actions工作流,遇到报错:

2023-02-19T02:09:08.581Z ERROR environment variable GITHUB_TOKEN not set!

已在仓库设置页面配置了Token,但问题依旧。原工作流代码如下:

name: CI/CD 

on: 
  pull_request: 
    branches: [ master ]  
    
jobs: 
  super-linter:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Super-Linter
        uses: github/super-linter@v4.10.1
        with:    
          files: ${{ join(github.event.pull_request.changed_files, ',') }}
          
  Merge:       
    runs-on: ubuntu-latest       
    needs: super-linter
    steps:   
      - name: Checkout Code 
        uses: actions/checkout@v2 
      - name: Merge pull requests 
        uses: pascalgn/automerge-action@v0.14.1   
        with: 
          GITHUB_TOKEN: ${{ secrets.TOKEN }}
      
        
  deploy: 
    runs-on: self-hosted   
    needs: Merge
    steps: 
      #- uses: actions/checkout@v2  #this is used for if you want to push all source code into runner       
      - name: update code base 
        working-directory: /test_pipe/www/html 
        run: sudo git pull origin master        
      - name: restart   
        working-directory: /test_pipe/www/html
        run: sudo systemctl restart nginx  

排查与修复方案

  • 修正Token传递方式:pascalgn/automerge-action优先读取环境变量而非with字段参数,需将Token通过env传入:

    - name: Merge pull requests 
      uses: pascalgn/automerge-action@v0.14.1   
      env:
        GITHUB_TOKEN: ${{ secrets.TOKEN }}
    
  • 验证Secret名称匹配:确认仓库Settings > Secrets and variables > Actions中创建的Secret名称确实是TOKEN,且该Token拥有repo权限(可读写PR及代码)。

  • 检查Token权限:自定义Token需授予repo:status、public_repo(公仓)或repo(私有仓)权限,且对应账号有合并PR到master分支的权限。

  • 升级Action版本:v0.14.1版本较旧,建议升级到最新稳定版,避免参数读取兼容性问题。

修改后的工作流示例

name: CI/CD 

on: 
  pull_request: 
    branches: [ master ]  
    
jobs: 
  super-linter:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Super-Linter
        uses: github/super-linter@v4.10.1
        with:    
          files: ${{ join(github.event.pull_request.changed_files, ',') }}
          
  Merge:       
    runs-on: ubuntu-latest       
    needs: super-linter
    steps:   
      - name: Checkout Code 
        uses: actions/checkout@v2 
      - name: Merge pull requests 
        uses: pascalgn/automerge-action@v0.14.1   
        env:
          GITHUB_TOKEN: ${{ secrets.TOKEN }}
      
        
  deploy: 
    runs-on: self-hosted   
    needs: Merge
    steps: 
      - name: update code base 
        working-directory: /test_pipe/www/html 
        run: sudo git pull origin master        
      - name: restart   
        working-directory: /test_pipe/www/html
        run: sudo systemctl restart nginx  

内容的提问来源于stack exchange,提问作者Andrew Bao-Viet Nguyen

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.31 09:31:05