diff --git a/DESCRIPTION b/DESCRIPTION index ef6d0bc5..f42b5daa 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,7 +23,7 @@ Suggests: testthat VignetteBuilder: knitr -Config/fusen/version: 0.5.2 +Config/fusen/version: 0.5.1 Encoding: UTF-8 LazyData: true Roxygen: list(markdown = TRUE) diff --git a/R/nix_github_actions.R b/R/nix_github_actions.R new file mode 100644 index 00000000..d443f0d4 --- /dev/null +++ b/R/nix_github_actions.R @@ -0,0 +1,17 @@ +# WARNING - Generated by {fusen} from dev/cicd.Rmd: do not edit by hand + +#' nix_github_actions +#' @return Nothing, copies file to a diretory. +#' +#' @noRd +nix_github_actions <- function(){ + path <- file.path(".github", "workflows") + dir.create(path) + source <- system.file( + file.path("extdata", "run-pipeline.yaml"), + package = "rix", + mustWork = TRUE + ) + file.copy(source, path, overwrite = TRUE) + invisible() +} diff --git a/dev/cicd.Rmd b/dev/cicd.Rmd new file mode 100644 index 00000000..8979a182 --- /dev/null +++ b/dev/cicd.Rmd @@ -0,0 +1,48 @@ +--- +title: "Run targets pipelines on Github Actions" +output: html_document +editor_options: + chunk_output_type: console +--- + +```{r development, include=FALSE} +library(testthat) +``` + + + +```{r development-load} +# Load already included functions if relevant +pkgload::load_all(export_all = FALSE) +``` + +The function below copies a Github actions workflow file into the +`.github/workflows/run-pipeline.yaml`: + +```{r function-nix_github_actions} +#' tar_nix_ga +#' @return Nothing, copies file to a diretory. +#' +tar_nix_ga <- function(){ + # Add an empty .gitignore file if there isn’t any + + if(file.exists(".gitignore")){ + NULL + } else { + file.create(".gitignore") + } + + dir.create(".github/workflows", recursive = TRUE) + source <- system.file( + file.path("extdata", "run-pipeline.yaml"), + package = "rix", + mustWork = TRUE + ) + file.copy(source, path, overwrite = TRUE) + invisible() +} +``` + +Be sure to give Github Actions workflows read and write permissions. diff --git a/dev/config_fusen.yaml b/dev/config_fusen.yaml index 19f3c608..c6d431ad 100644 --- a/dev/config_fusen.yaml +++ b/dev/config_fusen.yaml @@ -24,6 +24,19 @@ building_envs_with_rix.Rmd: check: true document: true overwrite: 'yes' +cicd.Rmd: + path: dev/cicd.Rmd + state: active + R: R/nix_github_actions.R + tests: [] + vignettes: [] + inflate: + flat_file: dev/cicd.Rmd + vignette_name: .na + open_vignette: true + check: true + document: true + overwrite: 'yes' cran_archive.Rmd: path: dev/cran_archive.Rmd state: active diff --git a/inst/extdata/run-pipeline.yaml b/inst/extdata/run-pipeline.yaml new file mode 100644 index 00000000..f8fea4e0 --- /dev/null +++ b/inst/extdata/run-pipeline.yaml @@ -0,0 +1,97 @@ +# Inspired by the template in the {targets} package +# https://github.com/ropensci/targets/blob/3b16f37ba5cdca710adbb81c2301f6b1d7432cfb/inst/templates/github_actions.yaml + +name: run-pipeline + +on: + push: + branches: + - main + - master + +jobs: + targets: + runs-on: ubuntu-latest + env: + GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }} + steps: + + - uses: actions/checkout@v3 + + - name: Install Nix + uses: DeterminateSystems/nix-installer-action@main + with: + logger: pretty + log-directives: nix_installer=trace + backtrace: full + + - name: Nix cache + uses: DeterminateSystems/magic-nix-cache-action@main + + - name: Build development environment + run: | + nix-build + + - name: Check if previous runs exists + id: runs-exist + run: git ls-remote --exit-code --heads origin targets-runs + continue-on-error: true + + - name: Checkout previous run + if: steps.runs-exist.outcome == 'success' + uses: actions/checkout@v2 + with: + ref: targets-runs + fetch-depth: 1 + path: .targets-runs + + - name: Restore output files from the previous run + if: steps.runs-exist.outcome == 'success' + run: | + nix-shell default.nix --run "Rscript -e 'for (dest in scan(\".targets-runs/.targets-files\", what = character())) { + source <- file.path(\".targets-runs\", dest) + if (!file.exists(dirname(dest))) dir.create(dirname(dest), recursive = TRUE) + if (file.exists(source)) file.rename(source, dest) + }'" + + - name: Run model + run: | + nix-shell default.nix --run "Rscript -e 'targets::tar_make()'" + + - name: Identify files that the targets pipeline produced + run: git ls-files -mo --exclude=renv > .targets-files + + - name: Create the runs branch if it does not already exist + if: steps.runs-exist.outcome != 'success' + run: git checkout --orphan targets-runs + + - name: Put the worktree in the runs branch if the latter already exists + if: steps.runs-exist.outcome == 'success' + run: | + rm -r .git + mv .targets-runs/.git . + rm -r .targets-runs + + - name: Upload latest run + run: | + git config --local user.name "GitHub Actions" + git config --local user.email "actions@github.com" + rm -r .gitignore .github/workflows + git add --all -- ':!renv' + for file in $(git ls-files -mo --exclude=renv) + do + git add --force $file + done + git commit -am "Run pipeline" + git push origin targets-runs + + - name: Prepare failure artifact + if: failure() + run: rm -rf .git .github .targets-files .targets-runs + + - name: Post failure artifact + if: failure() + uses: actions/upload-artifact@main + with: + name: ${{ runner.os }}-r${{ matrix.config.r }}-results + path: . \ No newline at end of file