解决GitHub Actions报错remote write access to repository not granted

最近在折腾 RSS 订阅更新,用到了 GitHub Actions。写了一个定时任务部署上去,一直报错,卡了好几个小时,记录一下解决方案。

问题

定时任务到时间才会触发,先用 push 触发来进行测试。Actions 的脚本:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
name: fetch rss scriptions

on:
push:
branches:
- main

env:
TZ: Asia/Shanghai

jobs:
build:
name: fetch rss post
runs-on: ubuntu-latest

steps:
- name: checkout actions
uses: actions/checkout@v4

- name: Set up Python 3.9
uses: actions/setup-python@v2
with:
python-version: 3.9

- name: fetch post list
run: |
python feed_parse.py

- name: commit
env:
GITHUB_REPO: github.com/lozhu20/my-blog-source
run: |
git config --global user.name lozhu20
git config --global user.email zhulongkun20@icloud.com
git add .
git commit -m "feed parse schedule task"
git pull --rebase
git push "https://${{ secrets.HEXO_BLOG_DEPLOY_KEY }}@$GITHUB_REPO" main:main

内容很简单,就是在每次 push 的时候执行一个 python 脚本,然后变更文件提交到 main 分支上。

报错信息:

1
2
3
4
5
6
7
▶️ Run git config --global user.name lozhu20
[main 7686805] feed parse schedule task
2 files changed, 1823 insertions(+), 47 deletions(-)
create mode 100644 tmp.xml
Current branch main is up to date.
remote: Write access to repository not granted.
fatal: unable to access 'https://github.com/lozhu20/my-blog-source/': The requested URL returned error: 403

报错

提示没有写入权限。

解决方案

一直以为是脚本里的仓库名称、分支或者 URL 不对,或者是生成的 key 的权限问题,网上搜了半天发现还需要给授权仓库写入权限:

repo > settings > Actions > General > Workflow permissions

修改配置

这个地方默认选择是:Read repository contents and packages permissions

需要勾选:Read and Write permissions

授权之后,再重新提交触发 Actions,就是舒心的绿色 ✅ 了!

绿色

参考: Github Actions - Write access to repository is not granted