GitHub Actions Object From Secrets Not Found

Hello! I have a pretty weird and niche question and I haven't been able to figure things out and would appreciate any insights anyone has to offer here.

I recently figured out how to use GitHub actions in R, and I created a quick-start guide to a database I own using bookdown, and I am looking to have it automatically refresh once a day in order to show a data preview of each table for the current day instead of showing older data in that preview.

I did a lot of playing around with GitHub actions and I was able to pass a secret string that is saved in the repository settings to a .Rmd file and things seemed to work fine, but now that I am doing the same thing to my bookdown project to pass the database login information, it is not able to find the variable that I assign the secret string to:

This is what the .yaml file step looks like:

I removed the db_ip and db_pswd secrets to make it easier to read

Any thoughts around why the object db_user cannot be found when the action runs?

This is what my .yaml file currently looks like:

on:
  schedule:
  - cron: "40 4 * * *"
    
name: update-website

jobs:
  update-website:
    runs-on: ${{ matrix.config.os }}

    strategy:
      fail-fast: false
      matrix:
        config:
          - {os: macOS-latest, r: '3.6'}
    env:
      R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
      CRAN: ${{ matrix.config.cran }}

    steps:
      - uses: actions/checkout@v1
      - uses: r-lib/actions/setup-r@master
      - name: Install pandoc and pandoc citeproc
        run: |
          brew install pandoc
          brew install pandoc-citeproc
      - name: Install dependencies
        run: |
          install.packages(c("remotes", "rcmdcheck", 'bookdown','DBI','RMySQL','data.table','knitr','rmarkdown','DT','webshot','PhantomJS'))
        shell: Rscript {0}
      - name: refresh book
        run: Rscript -e 'db_user <- "${{secrets.USER}}"' -e 'db_ip <- "${{secrets.IP}}"' -e 'db_pswd <- "${{secrets.PSWD}}"' -e "bookdown::render_book('index.Rmd', 'bookdown::gitbook')"
1 Like

Does anything change if you try running it as a custom shell script https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#example-running-a-python-script like in the Install dependencies action?

So something like:

run: |
  db_user <- ${{secrets.USER}}
  db_ip <- ${{secrets.IP}}
  ... # so on
shell: Rscript {0}
1 Like

That was a great idea! Definitely should have thought of that myself and I thought it was promising, but unfortunately I'm still running into the same issue:

Here is the adjusted .yaml:

on: [push]
    
name: update-website

jobs:
  update-website:
    runs-on: ${{ matrix.config.os }}

    strategy:
      fail-fast: false
      matrix:
        config:
          - {os: macOS-latest, r: '3.6'}
    env:
      R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
      CRAN: ${{ matrix.config.cran }}

    steps:
      - uses: actions/checkout@v1
      - uses: r-lib/actions/setup-r@master
      - name: Install pandoc and pandoc citeproc
        run: |
          brew install pandoc
          brew install pandoc-citeproc
      - name: Install dependencies
        run: |
          install.packages(c("remotes", "rcmdcheck", 'bookdown','DBI','RMySQL','data.table','knitr','rmarkdown','DT','webshot','PhantomJS'))
        shell: Rscript {0}
      - name: Refresh book
        run: |
          db_user <- "${{secrets.USER}}"
          
          db_ip <- "${{secrets.IP}}"
          
          db_pswd <- "${{secrets.PSWD}}"
          
          bookdown::render_book('index.Rmd', 'bookdown::gitbook')          
        shell: Rscript {0}

I just ran a test and got a very confusing result, so I am sharing that here.

Within the .yaml file I changed the line of code:

bookdown::render_book('index.Rmd', 'bookdown::gitbook') 

With the code:

rmarkdown::render("03-UsefulTables.Rmd")

This code renders the exact same .Rmd file that runs into the problem when I run render_book(), and yet this code doesn't run into any issues at all!

I promise all I changed is that one line and that it's running the same code chunk, this .yaml works as expected:

on: [push]
    
name: update-website

jobs:
  update-website:
    runs-on: ${{ matrix.config.os }}

    strategy:
      fail-fast: false
      matrix:
        config:
          - {os: macOS-latest, r: '3.6'}
    env:
      R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
      CRAN: ${{ matrix.config.cran }}

    steps:
      - uses: actions/checkout@v1
      - uses: r-lib/actions/setup-r@master
      - name: Install pandoc and pandoc citeproc
        run: |
          brew install pandoc
          brew install pandoc-citeproc
      - name: Install dependencies
        run: |
          install.packages(c("remotes", "rcmdcheck", 'bookdown','DBI','RMySQL','data.table','knitr','rmarkdown','DT','webshot','PhantomJS'))
        shell: Rscript {0}
      - name: Refresh book
        run: |
          db_user <- "${{secrets.USER}}"
          
          db_ip <- "${{secrets.IP}}"
          
          db_pswd <- "${{secrets.PSWD}}"
          
          rmarkdown::render("03-UsefulTables.Rmd")

Thoughts on how to get it to work with bookdown::render_book()? If I change the one line it can't find the variable db_user and using the ${{secrets.USER}} syntax doesn't work directly in the .Rmd file

If I pull directly from the same repository and run the same command locally with the values that the secrets are hiding this works as expected:

Thanks to Murray Cadzow who figured out the solution to this on Twitter! https://twitter.com/MurrayCadzow/status/1232897913199554560

In his own words:

By default in a non interactive session bookdown::render_book() will clean the environment before rendering. You could try running as bookdown::render_book(..., clean_envir = FALSE)

After adding , clean_envir = FALSE to my code everything finally works!

2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.