Closed AnneMTreasure closed 3 years ago
The official way to to this is documented here, which is also exactly what's done in googlesheets4, googledrive, and bigrquery, all of which run tests with a service account token on GHA. So in addition to the article you can also look at those repos for how things are put together.
https://gargle.r-lib.org/articles/articles/managing-tokens-securely.html
Hi Jenny, thanks for your reply. Does the https://gargle.r-lib.org/articles/articles/managing-tokens-securely.html article apply to non-package environments as well, which is what I have? I have seen this page, and tried to work through it, but have not been sure of the package versus non-package environment to set this up?
It's true that the helpers like gargle:::secret_path()
are anticipating a package directory structure. You would need to execute the "business end" of the key functions yourself, without the package-y path bits.
But the general principles all hold up, regardless of whether the project is a package.
That encryption approach has now been baked into the httr2 package and written up in a more general way:
https://httr2.r-lib.org/articles/wrapping-apis.html#secret-management-1
Maybe those instructions for encrypting / decrypting will be easier to transfer to a non-package project.
If this ends up playing out on GitHub and I can see your code and GHA config, I will try to help.
Hi Jenny, many thanks for your advice. I have managed to get my GitHub Action to work (just a test repo for now).
I used the tokencodr
package for the encryption, and got some very good advice and pointers for this from the package developer, and this demo:
https://github.com/jdtrat/tokencodr-google-demo
The authorisations using googlesheets4
are all working now.
Many thanks for your help!
Hi there, I’m having a problem with GitHub Actions, and using
googlesheets4
to read an encrypted .json file using a password saved as a GitHub Actions secret.I have written an R script that reads in data from a google sheet, does some data manipulation steps, then writes this new data set to a separate google sheet. For this, I have used the package
googlesheets4
. To run the script and get authorisation to be able to read from & write to google sheets in a non-interactive way, I have set up a service account on Google Cloud Platform, created the credentials, and a service account token key in .json format, and shared the google sheets with the email given in the .json file. The .json key file is stored in my R project directory, and now I can run my script without interaction, using the following for authorisation:gs4_auth(email = "my email address", path = "~/path_to_file/sheets_service_account_key.json")
This works fine locally on my computer. However, I now want to run my script as a GitHub Action using a cron scheduler. My problem is the .json key file, which should not be made public, and I am struggling to figure out how to either keep it as a secret on GitHub, which can somehow be called into the GitHub Action using
googlesheets4
in my R script, or to encrypt the file.I was pointed to the
tokencodr
package and, using this, I have encrypted the .json file and put this in a .secret folder in my GitHub repo (this encrypted file is called MY_GOOGLE). Committing this encrypted file to GitHub is apparently ok, as long as the password (created when the file is encrypted) is kept secret. I have copied this password to an Actions secret in my GitHub repo.Then, in my R script, I try to use the
gs4_auth
function to tell it to look for the encrypted .json in the .secret folder in my GitHub repo using:gs4_auth(email = "*@email.co.za", path = "~/repo_name/.secret/MY_GOOGLE")
Then, in my GitHub Action .yaml, I have the following:
etc
The GitHub Action does not work. This is the error message part of the GitHub Action build log:
Can
googlesheets4
be used in a GitHub Action in this way / Is it possible to use thegooglesheets4
package in my R script to look for the encrypted .json file in the .secret folder in my GitHub repo, and the necessary password in the GitHub Action secret? I am also assuming that the password is enough to decrypt the file, which is possibly not the case.Any advice would be much appreciated. Many thanks.