9.1 Actions - Openscapes/2021-noaa-nmfs GitHub Wiki

You may have heard of Travis Continuous Integration. GitHub Actions are like that. Basically, this provides a way to run some code on your repo (or parts of it) when you commit code up to the repo. So this allows you to automate tasks. Like

  • Run a check on your R package code to make sure it builds
  • Run code to rebuild your (GitHub Pages) website when files are updated
  • Re-build your Readme file
  • Run tests
  • Really pretty much any task with your code

This RWorkflow section talks about GitHub Actions and how to use the usethis to set up Actions.

I personally find it easiest to copy simple GitHub Actions from other people. Look for the .github/workflow folders in repos.

  • Check an R package

  • Rebuild documentation from Roxygen

  • Rebuild package down

  • Rendering a README.Rmd file is an easy way to start with GitHub Actions. Let's say you want your Readme to have figures generated by R code. This Action will watch if README.Rmd changes and then re-knit to create the README.md file (github markdown) that GitHub needs.

    Create a .github folder in your repo. In that create a workflows folder. Then create a text file and call it, say, Readme.yaml. Note it doesn't matter what you name it but .yaml is important. Then copy this code into the file. Push to GitHub. That's it! Note the first part says when to do the action. The code says to do the action when you push a change to README.Rmd, so re-name if your Rmd file has a different name (like Readme.Rmd).

on:
  push:
    paths:
      - README.Rmd

name: Render README

jobs:
  render:
    name: Render README
    runs-on: macOS-latest
    steps:
      - uses: actions/checkout@v2
      - uses: r-lib/actions/setup-r@v1
      - uses: r-lib/actions/setup-pandoc@v1
      - name: Install packages
        run: Rscript -e 'install.packages(c("rmarkdown", "knitr"))'
      - name: Render README
        run: Rscript -e 'rmarkdown::render("README.Rmd", output_format = "md_document")'
      - name: Commit results
        run: |
          git commit README.md -m 'Re-build README.Rmd' || echo "No changes to commit"
          git push origin || echo "No changes to commit"

Example of interesting things you can do with Readme files