Heroku部署Shiny应用遇Buildpack R超时推送失败求助
部署Shiny应用到Heroku时遇到构建超时错误:Timed out running buildpack R (shiny)
问题详情
- 执行部署命令:
git push heroku main,推送失败,提示Timed out running buildpack R (shiny) - Git远程配置:
git remote -v heroku https://git.heroku.com/countryprofile.git (fetch) heroku https://git.heroku.com/countryprofile.git (push) origin https://github.com/akshaybholee/CountryProfileHeroku.git (fetch) origin https://github.com/akshaybholee/CountryProfileHeroku.git (push)
- 部署日志关键片段:
remote: -----> Building on the Heroku-22 stack remote: -----> Using buildpack: https://github.com/virtualstaticvoid/heroku-buildpack-r.git remote: -----> R (shiny) app detected remote: -----> Installing R remote: Version 4.2.1 will be installed on heroku-22 stack. remote: -----> Downloading buildpack archives from AWS S3 remote: Downloading https://heroku-buildpack-r.s3.amazonaws.com/latest/heroku-buildpack-r-22-4.2.1-deploy.tar.gz remote: Downloading https://heroku-buildpack-r.s3.amazonaws.com/latest/heroku-buildpack-r-22-4.2.1-shiny.tar.gz remote: Setting up build environment remote: Downloading https://heroku-buildpack-r.s3.amazonaws.com/latest/heroku-buildpack-r-22-4.2.1-chroot.tar.gz remote: -----> Configuring build environment... remote: remote: # WARNING: Use of the Aptfile file is NOT recommended and will be deprecated in future. remote: remote: For greater control and flexibility consider using the Heroku remote: container stack together with the heroku-docker-r implementation. remote: See https://github.com/virtualstaticvoid/heroku-docker-r for more information. remote: remote: W: No sandbox user '_apt' on the system, can not drop privileges remote: Reading package lists... remote: Building dependency tree... remote: Reading state information... remote: The following additional packages will be installed: remote: default-libmysqlclient-dev gdal-data hdf5-helpers libaec-dev libaec0 remote: ...(大量系统依赖省略) remote: installing to /app/R/site-library/00LOCK-spam/00new/spam/libs remote: ** R remote: ** data remote: *** moving datasets to lazyload DB remote: ** demo remote: ** inst remote: ** byte-compile and prepare package for lazy loading remote: -----> Timed out running buildpack R (shiny) remote: remote: ! Push failed remote: Verifying deploy... remote: remote: ! Push rejected to countryprofile. remote: To https://git.heroku.com/countryprofile.git ! [remote rejected] main -> main (pre-receive hook declined) error: failed to push some refs to 'https://git.heroku.com/countryprofile.git'
解决方案
1. 精简依赖,减少安装时间
- 检查项目中的
DESCRIPTION或renv.lock文件,移除未使用的R包,尤其是像GDAL、Spatialite这类会触发大量系统依赖安装的包,如果应用不需要空间处理功能,直接删除相关包引用。 - 确保只保留应用运行必需的依赖,避免冗余包拖慢构建速度。
2. 延长构建超时时间
Heroku默认构建超时为15分钟,可通过CLI调整为更长时间(比如30分钟):
heroku config:set HEROKU_BUILDPACK_R_TIMEOUT=1800
设置完成后重新执行git push heroku main部署。
3. 改用Docker容器部署
按照buildpack的提示,使用Docker容器部署能获得更灵活的控制,避免buildpack的限制:
- 在项目根目录创建
Dockerfile,示例内容:
FROM rocker/shiny:4.2.1 # 安装系统依赖(按需调整) RUN apt-get update && apt-get install -y --no-install-recommends \ libssl-dev \ libcurl4-gnutls-dev \ && rm -rf /var/lib/apt/lists/* # 安装R包依赖 RUN R -e "install.packages(c('shiny', 'dplyr', 'ggplot2'), repos='https://cran.rstudio.com/')" # 复制应用文件 COPY . /srv/shiny-server/ # 设置工作目录 WORKDIR /srv/shiny-server/ # 暴露端口 EXPOSE 3838 # 启动Shiny服务 CMD ["R", "-e", "shiny::runApp(host='0.0.0.0', port=3838)"]
- 登录Heroku容器注册表:
heroku container:login
- 构建并推送镜像:
heroku container:push web
- 发布容器应用:
heroku container:release web
4. 清除缓存后重新构建
Heroku的构建缓存可能导致依赖安装异常,先清除缓存再重新部署:
heroku builds:cache:purge git push heroku main
内容的提问来源于stack exchange,提问作者akshay bholee
相关产品推荐
相关产品推荐

