Skip to content

Commit

Permalink
Merge pull request #88 from b-rodrigues/nix_github_actions
Browse files Browse the repository at this point in the history
Nix GitHub actions
  • Loading branch information
b-rodrigues authored Oct 21, 2023
2 parents d7eb2d9 + a3b5bb2 commit 6705024
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 1 deletion.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
17 changes: 17 additions & 0 deletions R/nix_github_actions.R
Original file line number Diff line number Diff line change
@@ -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()
}
48 changes: 48 additions & 0 deletions dev/cicd.Rmd
Original file line number Diff line number Diff line change
@@ -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)
```

<!--
You need to run the 'description' chunk in the '0-dev_history.Rmd' file before continuing your code there.
-->

```{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.
13 changes: 13 additions & 0 deletions dev/config_fusen.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
97 changes: 97 additions & 0 deletions inst/extdata/run-pipeline.yaml
Original file line number Diff line number Diff line change
@@ -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: .

0 comments on commit 6705024

Please sign in to comment.