Using blogdown with github actions - alternative to serve_site

Viewed 170

Is there a way to replicate the behaviour of blogdown::serve_site() where it caches files (i.e. it only rebuilds newly updated or "touched" files) but without actually causing a local preview?

The reason I ask is that I would like to automate this process with Github Actions and this seems to fail when using serve_site.

For reference, I use Netlify to host and build the site. The general gist of my process is that I run a script to update a data file and then "touch" a file before using serve_site to update just that file.

# 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()

I can then commit this and Netlify will update the site. This works perfectly fine on my local machine and is what I've been doing for a while. But I'm trying to automate it with Github Actions so that it runs every day.

To do that I can setup the following. I copied this from 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()'

This runs fine until it gets to the 'build site' part, where it just hangs and I get the following error. I'm assuming this is because the process never actually finishes and so just times out.

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

I've tried using blogdown::build_site() and blogdown::build_hugo() but build_site re-renders every page which I don't want and build_hugo doesn't re-render the touched file!

Basically what I need is to replicate the caching mechanism of serve_site so that it just renders files where the RMD is newer than the HTML file without trying to preview it locally.

For reference - the failing Github Action run is here

1 Answers

I managed to work this one out so sharing the answer. Two things I did seemed to help here.

First - using blogdown::build_site(local = TRUE). The local part was what I had been missing earlier.

I also needed to add the touch_file part of the action to the run command. I'm not sure if that was actually important but it was the step that finally got this whole process working for me so I'm going to say yes.

The extract is below. In the 'Install Packages' section you also need to add packages that are used in the files you touch.

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} 

After this, you'll need some code to commit

Related