Use Github Secrets for pkgdown yaml

Some of my package functions require a API setting a API key and I'd like to have those functions run on pkgdown, while not publicly exposing the API in my code. How do I enter a Github Secrets to my yaml?

I need to set the variable in R:

Sys.setenv(CENSUS_API_KEY = "ggrwewtw121312") # fake key

and suppose I have set a secret in on my Github, titled CENSUS_API_KEY.

Then in my pkgdown yaml I currently tried

      - name: Add CENSUS API KEY
        run: |
          Sys.setenv(CENSUS_API_KEY = {{ $secrets.CENSUS_API_KEY }})
        shell: Rscript {0}

but that won't work because the {{$ }} notation I guess cannot be used in R code. What is the right syntax?

I've not used the secrets in GitHub but I have used the pins package for the API_KEY (to an R Studio Connect server) https://pins.rstudio.com/ I was following what others in my team set up but seemed really secure in that the API key is only located on my computer and not in any script.

pins looks very useful, thank you.

For this specific application, I tried to edit the yaml more directly. I figured it out as follows. First, write the variable into the server's .Renviron file with Shell. Then it will be available in any R script of the repo, through Sys.getenv()

In the yaml:

  - name: "[Custom block] Add CENSUS API KEY"
    run: echo "CENSUS_API_KEY = '${{ secrets.CENSUS_API_KEY }}'" >> .Renviron

where CENSUS_API_KEY is a Github Secrets I added separately.
Then call in the R script, e.g.

 tidycensus::census_api_key(Sys.getenv("CENSUS_API_KEY"))

(repo: https://github.com/kuriwaki/ccesMRPprep)

2 Likes

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

If you have a query related to it or one of the replies, start a new topic and refer back with a link.