我想用 Sys.getenv()
检索环境变量。
在本地,我可以保存 .Renviron
我的工作目录中包含变量的文件。这些负载非常好并且测试通过。
存储库在这里供引用:https://github.com/Stoltzman-Consulting/githubActionsR
但是,由于存储了 secret ,我无法将此文件上传到我的存储库中。我在 GitHub secret 部分创建了 secret :
我的测试如下:
test_that("multiplication works", {
expect_equal(2 * 2, 4)
})
test_that("Environment variable exists", {
expect_equal(Sys.getenv('MY_SECRET'), 'i_want_this')
})
test_that("Environment variable 2 exists", {
expect_equal(Sys.getenv('MY_SECRET2'), 'i_want_this_2')
})
我的 GitHub Actions 文件如下:# For help debugging build failures open an issue on the RStudio community with the 'github-actions' tag.
# https://community.rstudio.com/new-topic?category=Package%20development&tags=github-actions
on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master
name: R-CMD-check
jobs:
R-CMD-check:
runs-on: ${{ matrix.config.os }}
name: ${{ matrix.config.os }} (${{ matrix.config.r }})
strategy:
fail-fast: false
matrix:
config:
- {os: windows-latest, r: 'release'}
- {os: macOS-latest, r: 'release'}
- {os: ubuntu-20.04, r: 'release', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
- {os: ubuntu-20.04, r: 'devel', rspm: "https://packagemanager.rstudio.com/cran/__linux__/focal/latest"}
env:
R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
RSPM: ${{ matrix.config.rspm }}
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
MY_SECRET: ${{ secrets.MY_SECRET }}
MY_SECRET2: ${{ secrets.MY_SECRET2 }}
steps:
- uses: actions/checkout@v2
- uses: r-lib/actions/setup-r@v1
with:
r-version: ${{ matrix.config.r }}
- uses: r-lib/actions/setup-pandoc@v1
- name: Query dependencies
run: |
install.packages('remotes')
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
shell: Rscript {0}
- name: Cache R packages
if: runner.os != 'Windows'
uses: actions/cache@v2
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-
- name: Install system dependencies
if: runner.os == 'Linux'
run: |
while read -r cmd
do
eval sudo $cmd
done < <(Rscript -e 'writeLines(remotes::system_requirements("ubuntu", "20.04"))')
- name: Install dependencies
run: |
remotes::install_deps(dependencies = TRUE)
remotes::install_cran("rcmdcheck")
shell: Rscript {0}
- name: Run Tests
env:
MY_SECRET2: ${{ secrets.MY_SECRET2 }}
run: |
Sys.setenv(MY_SECRET2 = "$MY_SECRET2")
testthat::test_file('my_test.R')
shell: Rscript {0}
- name: Check
env:
_R_CHECK_CRAN_INCOMING_REMOTE_: false
run: rcmdcheck::rcmdcheck(args = c("--no-manual", "--as-cran"), error_on = "warning", check_dir = "check")
shell: Rscript {0}
- name: Upload check results
if: failure()
uses: actions/upload-artifact@main
with:
name: ${{ runner.os }}-r${{ matrix.config.r }}-results
path: check
你会注意到我什至尝试过使用 Sys.setenv()
但这也不起作用(并且不是一个非常可扩展的解决方案)。我怎样才能让这个通过?
最佳答案
一种有效的解决方案,创建 ~/.Renviron
块内
- name: Create and populate .Renviron file
run: |
echo MY_SECRET="$MY_SECRET" >> ~/.Renviron
echo MY_SECRET2="$MY_SECRET2" >> ~/.Renviron
shell: bash
只要这在您的测试之前进行,这就会起作用。它已添加到列出的 github 存储库中。
关于r - 如何使用 secret 通过 Sys.getenv() 和 GitHub Actions 获得 R 环境?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65926899/