【问题标题】:Using blogdown with github actions - alternative to serve_site将 blogdown 与 github 操作一起使用 - 替代 serve_site
【发布时间】:2020-07-10 01:20:03
【问题描述】:

有没有办法复制 blogdown::serve_site() 缓存文件的行为(即它只重建新更新或“触及”的文件)但实际上不会导致本地预览?

我问的原因是我想用 Github Actions 自动化这个过程,而使用 serve_site 时这似乎失败了。

作为参考,我使用 Netlify 来托管和构建站点。我的过程的一般要点是我运行一个脚本来更新一个数据文件,然后在使用serve_site 更新该文件之前“触摸”一个文件。

# touch the blog post that references this file
blogdown:::touch_file("path_to_file.Rmd")

# serve the site which re-renders just the touched post (not all posts)
blogdown::serve_site()

然后我可以提交此操作,Netlify 将更新该站点。这在我的本地机器上工作得很好,这也是我一段时间以来一直在做的事情。但我正在尝试使用 Github Actions 将其自动化,以便它每天运行。

为此,我可以设置以下内容。我从this question复制了这个

name: Get new data and rebuild site

on:
  schedule:
    - cron: "0 13 * * 1"
  push:
    branches:
      - master

jobs:
  scrape-and-commit:
    runs-on: macOS-latest
    steps:
      - uses: actions/checkout@v2
      - uses: r-lib/actions/setup-r@master
      - uses: r-lib/actions/setup-pandoc@v1
      - name: Install packages
        run: Rscript -e 'install.packages(c("tidyverse", "here", "blogdown"))'
      - name: Install Hugo
        run: Rscript -e 'blogdown::install_hugo(extended = TRUE, version = "0.66.0")'
      - name: Get data
        run: Rscript -e 'source(here::here("scripts", "weekly_data_process.R"), echo = TRUE)'
      - name: Build site
        run: Rscript -e 'blogdown::serve_site()'

这运行良好,直到它到达“构建站点”部分,它只是挂起并且我收到以下错误。我假设这是因为该过程实际上从未完成,所以只是超时。

Serving the directory /Users/runner/work/plussixoneblog/plussixoneblog at http://127.0.0.1:4321
##[error]The operation was canceled.

我尝试过使用blogdown::build_site()blogdown::build_hugo(),但build_site 会重新呈现我不想要的每个页面,而build_hugo 不会重新呈现触摸过的文件!

基本上我需要的是复制serve_site 的缓存机制,以便它只呈现RMD 比HTML 文件更新的文件,而不尝试在本地预览它。

供参考 - 失败的 Github Action 运行是 here

【问题讨论】:

  • 抱歉,这不是特别可重现的,需要设置一些东西才能获得reprex。如果有帮助,我可以这样做,但我想我会检查以前是否有其他人遇到过。
  • 只是为了记录,这是在github.com/rstudio/blogdown/issues/468交叉发布的。
  • @YihuiXie 是的,对不起,我一直想更新这个 Stack Overflow 问题!我很快就会这样做。
  • 完全不用担心!非常感谢您回帖!

标签: r blogdown


【解决方案1】:

我设法解决了这个问题,因此分享了答案。我做的两件事似乎在这里有所帮助。

首先 - 使用blogdown::build_site(local = TRUE)。本地部分是我之前缺少的部分。

我还需要将动作的 touch_file 部分添加到运行命令中。我不确定这是否真的很重要,但这是最终让整个过程为我工作的步骤,所以我会说是的。

摘录如下。在“安装包”部分,您还需要添加在您接触的文件中使用的包。

jobs:
  touch-and-rebuild:
    runs-on: macOS-latest
    steps:
      - uses: actions/checkout@v2
      - uses: r-lib/actions/setup-r@master
      - uses: r-lib/actions/setup-pandoc@v1
      - name: Install packages
        run: Rscript -e 'install.packages(c("here", "blogdown"))'
      - name: Install Hugo
        run: Rscript -e 'blogdown::install_hugo(extended = TRUE, version = "0.66.0")'
      - name: Build site
        run: |
          blogdown:::touch_file(here::here("path", "to", "file.Rmd"))
          blogdown:::build_site(TRUE)
        shell: Rscript {0} 

在此之后,您将需要一些代码来提交

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-10-20
    • 2023-03-12
    • 2021-02-09
    • 2023-04-07
    • 2023-03-23
    • 1970-01-01
    相关资源
    最近更新 更多