-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from b-rodrigues/nix_github_actions
Nix GitHub actions
- Loading branch information
Showing
5 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: . |