GitHub Action执行R爬虫脚本报错:nothing to commit, working tree clean
解决GitHub Action定时爬虫的git提交报错问题
问题背景
通过GitHub Action每小时运行R爬虫脚本,根据ASIN向量抓取亚马逊商品名称,生成带时间戳的CSV文件并提交到仓库。首次运行正常,但后续执行时出现nothing to commit, working tree clean错误,导致Action执行失败。
报错原因
- git的
commit命令在没有文件变更时会返回非零退出码,而GitHub Action默认将非零码判定为执行失败 - 可能伴随的问题:R脚本生成的文件名包含特殊字符(如冒号、空格),或脚本运行异常未生成CSV文件
修复方案
1. 调整git提交逻辑,允许无变更时跳过提交
修改Action中的Commit files步骤,添加条件判断,仅在有文件变更时执行提交和推送,避免无变更时报错:
git config --local user.name github-actions git config --local user.email "actions@github.com" git add data/* # 检查是否有暂存的变更 if git diff --cached --quiet; then echo "No new files to commit, skipping push" else git commit -m "GH ACTION Autorun $(date)" git push origin main fi
或者简化为允许commit命令失败时不终止Action:
git commit -am "GH ACTION Autorun $(date)" || true
2. 优化R脚本的文件名生成与目录检查
- 生成无特殊字符的时间戳,避免文件系统或git识别问题:
# 生成格式为YYYYMMDD_HHMMSS的时间戳 date <- format(Sys.time(), "%Y%m%d_%H%M%S") # 确保data目录存在 if (!dir.exists("data")) dir.create("data") # 导出CSV,禁用行名减少冗余 write.csv(product_name_df_delay_ua, file = paste0("data/Amazon_name_", date, ".csv"), row.names = FALSE)
- 移除脚本中的全局赋值
final_df <<- as.data.frame(final),改用局部变量避免潜在冲突:
name_checker_2 <- function(asin) { url_compositor <- paste0("https://www.amazon.it/dp/", asin) Sys.sleep(15) # 用R原生的Sys.sleep替代system('sleep 15') page <- read_html(url_compositor, user_agent = ua) name <- page %>% html_nodes(xpath = "//*[@id='productTitle']/text()") %>% html_text() %>% trimws() # 直接返回数据框,无需全局变量 return(data.frame(asin = asin, asin_2 = unlist(asin), name = name)) }
3. 优化Action的R包安装步骤
将多个单独的安装命令合并为批量安装,提升效率:
R -e 'install.packages(c("tidyverse", "tibble", "openxlsx", "gdata", "lubridate", "rvest", "stringr", "dplyr", "purrr", "plyr"), dependencies = TRUE)'
同时使用最新版本的Action组件:
- uses: actions/checkout@v4 - uses: r-lib/actions/setup-r@v2
修改后的完整YAML配置
# Hourly scraping name: amazonR on: schedule: - cron: '0 * * * *' jobs: autoscrape: runs-on: macos-latest steps: - uses: actions/checkout@v4 - uses: r-lib/actions/setup-r@v2 - name: Install packages run: | R -e 'install.packages(c("tidyverse", "tibble", "openxlsx", "gdata", "lubridate", "rvest", "stringr", "dplyr", "purrr", "plyr"), dependencies = TRUE)' - name: Scrape run: Rscript amazon_name_checker_simplest.R - name: Commit files run: | git config --local user.name github-actions git config --local user.email "actions@github.com" git add data/* if git diff --cached --quiet; then echo "No changes to commit" else git commit -m "GH ACTION Autorun $(date)" git push origin main fi env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
内容的提问来源于stack exchange,提问作者Andrea
相关产品推荐
相关产品推荐

