Skip to content

Commit bcb686d

Browse files
GitHub Actions: check lesson template (#489)
Co-authored-by: Maxim Belkin <[email protected]> Co-authored-by: François Michonneau <[email protected]>
1 parent 32e788b commit bcb686d

4 files changed

Lines changed: 199 additions & 31 deletions

File tree

.github/workflows/template.yml

Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
name: Template
2+
on:
3+
push:
4+
branches: gh-pages
5+
pull_request:
6+
jobs:
7+
check-template:
8+
name: Test lesson template
9+
if: github.repository == 'carpentries/styles'
10+
runs-on: ${{ matrix.os }}
11+
strategy:
12+
fail-fast: false
13+
matrix:
14+
lesson: [swcarpentry/shell-novice, datacarpentry/r-intro-geospatial, librarycarpentry/lc-git]
15+
os: [ubuntu-latest, macos-latest, windows-latest]
16+
defaults:
17+
run:
18+
shell: bash # forces 'Git for Windows' on Windows
19+
steps:
20+
- name: Set up Ruby
21+
uses: actions/setup-ruby@main
22+
with:
23+
ruby-version: '2.7.1'
24+
25+
- name: Set up Python
26+
uses: actions/setup-python@v2
27+
with:
28+
python-version: '3.x'
29+
30+
- name: Install GitHub Pages, Bundler, and kramdown gems
31+
run: |
32+
gem install github-pages bundler kramdown
33+
34+
- name: Install Python modules
35+
run: |
36+
if [[ $RUNNER_OS == macOS || $RUNNER_OS == Linux ]]; then
37+
python3 -m pip install --upgrade pip setuptools wheel pyyaml==5.3.1 requests
38+
elif [[ $RUNNER_OS == Windows ]]; then
39+
python -m pip install --upgrade pip setuptools wheel pyyaml==5.3.1 requests
40+
fi
41+
42+
- name: Checkout the ${{ matrix.lesson }} lesson
43+
uses: actions/checkout@master
44+
with:
45+
repository: ${{ matrix.lesson }}
46+
path: lesson
47+
fetch-depth: 0
48+
49+
- name: Look for R-markdown files
50+
id: check-rmd
51+
working-directory: lesson
52+
run: |
53+
echo "::set-output name=count::$(shopt -s nullglob; files=($(find . -iname '*.Rmd')); echo ${#files[@]})"
54+
55+
- name: Set up R
56+
if: steps.check-rmd.outputs.count != 0
57+
uses: r-lib/actions/setup-r@master
58+
with:
59+
r-version: 'release'
60+
61+
- name: Install needed packages
62+
if: steps.check-rmd.outputs.count != 0
63+
run: |
64+
install.packages(c('remotes', 'rprojroot', 'renv', 'desc'))
65+
shell: Rscript {0}
66+
67+
- name: Query dependencies
68+
if: steps.check-rmd.outputs.count != 0
69+
working-directory: lesson
70+
run: |
71+
source('bin/dependencies.R')
72+
deps <- identify_dependencies()
73+
create_description(deps)
74+
saveRDS(remotes::dev_package_deps(dependencies = TRUE), ".github/depends.Rds", version = 2)
75+
writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
76+
shell: Rscript {0}
77+
78+
- name: Cache R packages
79+
if: runner.os != 'Windows' && steps.check-rmd.outputs.count != 0
80+
uses: actions/cache@v1
81+
with:
82+
path: ${{ env.R_LIBS_USER }}
83+
key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
84+
restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-
85+
86+
- name: Install system dependencies for R packages
87+
if: runner.os == 'Linux' && steps.check-rmd.outputs.count != 0
88+
working-directory: lesson
89+
run: |
90+
while read -r cmd
91+
do
92+
eval $cmd
93+
done < <(Rscript -e 'cat(remotes::system_requirements("ubuntu", "18.04"), sep = "\n")')
94+
95+
- name: Install R lessons package dependencies
96+
if: steps.check-rmd.outputs.count != 0
97+
working-directory: lesson
98+
run: |
99+
remotes::install_deps(dependencies = TRUE)
100+
file.remove("DESCRIPTION")
101+
shell: Rscript {0}
102+
103+
- name: Determine the proper reference to use
104+
id: styles-ref
105+
run: |
106+
if [[ -n "${{ github.event.pull_request.number }}" ]]; then
107+
echo "::set-output name=ref::refs/pull/${{ github.event.pull_request.number }}/head"
108+
else
109+
echo "::set-output name=ref::gh-pages"
110+
fi
111+
112+
- name: Sync lesson with carpentries/styles
113+
working-directory: lesson
114+
run: |
115+
git config --global user.email "[email protected]"
116+
git config --global user.name "The Carpentries Bot"
117+
git remote add styles https://github.com/carpentries/styles.git
118+
git config --local remote.styles.tagOpt --no-tags
119+
git fetch styles ${{ steps.styles-ref.outputs.ref }}:styles-ref
120+
git merge -s recursive -Xtheirs --no-commit styles-ref
121+
git commit -m "Sync lesson with carpentries/styles"
122+
123+
- run: make site
124+
working-directory: lesson

Makefile

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ endif
3232

3333

3434
# Controls
35-
.PHONY : commands clean files
35+
.PHONY : commands clean files install-rmd-deps
3636

3737
# Default target
3838
.DEFAULT_GOAL := commands
@@ -119,10 +119,15 @@ HTML_DST = \
119119
$(patsubst _extras/%.md,${DST}/%/index.html,$(sort $(wildcard _extras/*.md))) \
120120
${DST}/license/index.html
121121

122+
## * install-rmd-deps : Install R packages dependencies to build the RMarkdown lesson
123+
install-rmd-deps:
124+
Rscript -e 'source("bin/dependencies.R"); install_dependencies(identify_dependencies())'
125+
122126
## * lesson-md : convert Rmarkdown files to markdown
123127
lesson-md : ${RMD_DST}
124128

125-
_episodes/%.md: _episodes_rmd/%.Rmd
129+
_episodes/%.md: _episodes_rmd/%.Rmd install-rmd-dependencies
130+
@mkdir -p _episodes
126131
@bin/knit_lessons.sh $< $@
127132

128133
## * lesson-check : validate lesson Markdown

bin/dependencies.R

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
find_root <- function() {
2+
if (!requireNamespace("rprojroot", quietly = TRUE)) {
3+
install.packages("rprojroot", lib = lib, repos = repos)
4+
}
5+
6+
cfg <- rprojroot::has_file_pattern("^_config.y*ml$")
7+
root <- rprojroot::find_root(cfg)
8+
9+
root
10+
}
11+
12+
identify_dependencies <- function(lib = NULL, repos = getOption("repos")) {
13+
14+
if (is.null(lib)) {
15+
lib <- .libPaths()
16+
}
17+
18+
if (!requireNamespace("renv", quietly = TRUE)) {
19+
install.packages("renv", lib = lib, repos = repos)
20+
}
21+
22+
root <- find_root()
23+
24+
required_pkgs <- unique(c(
25+
## Packages for episodes
26+
renv::dependencies(file.path(root, "_episodes_rmd"), progress = FALSE, error = "ignore")$Package,
27+
## Packages for tools
28+
renv::dependencies(file.path(root, "bin"), progress = FALSE, error = "ignore")$Package
29+
))
30+
31+
required_pkgs
32+
}
33+
34+
install_dependencies <- function(required_pkgs,
35+
lib = NULL, repos = getOption("repos"),
36+
update = FALSE, ...) {
37+
38+
if (missing(lib)) {
39+
lib <- .libPaths()
40+
}
41+
42+
missing_pkgs <- setdiff(required_pkgs, rownames(installed.packages()))
43+
44+
if (length(missing_pkgs)) {
45+
message("Installing missing required packages: ",
46+
paste(missing_pkgs, collapse=", "))
47+
install.packages(missing_pkgs, lib = lib, repos = repos)
48+
}
49+
50+
if (update) {
51+
update.packages(
52+
lib.loc = lib, repos = repos,
53+
ask = FALSE, checkBuilt = TRUE, ...
54+
)
55+
}
56+
57+
if (require("knitr") && packageVersion("knitr") < '1.9.19') {
58+
stop("knitr must be version 1.9.20 or higher")
59+
}
60+
61+
}
62+
63+
create_description <- function(required_pkgs) {
64+
require("desc")
65+
d <- description$new("!new")
66+
lapply(required_pkgs, function(x) d$set_dep(x))
67+
d$write("DESCRIPTION")
68+
}

bin/generate_md_episodes.R

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,5 @@
11
generate_md_episodes <- function() {
22

3-
if (!requireNamespace("renv", quietly = TRUE)) {
4-
install.packages("renv", repos = c(CRAN = "https://cloud.r-project.org/"))
5-
}
6-
7-
if (!requireNamespace("rprojroot", quietly = TRUE)) {
8-
install.packages("rprojroot", repos = c(CRAN = "https://cloud.r-project.org/"))
9-
}
10-
11-
cfg <- rprojroot::has_file_pattern("^_config.y*ml$")
12-
root <- rprojroot::find_root(cfg)
13-
14-
required_pkgs <- unique(c(
15-
## Packages for episodes
16-
renv::dependencies(file.path(root, "_episodes_rmd"), progress = FALSE, error = "ignore")$Package,
17-
## Pacakges for tools
18-
renv::dependencies(file.path(root, "bin"), progress = FALSE, error = "ignore")$Package
19-
))
20-
21-
missing_pkgs <- setdiff(required_pkgs, rownames(installed.packages()))
22-
23-
if (length(missing_pkgs)) {
24-
message("Installing missing required packages: ",
25-
paste(missing_pkgs, collapse=", "))
26-
install.packages(missing_pkgs)
27-
}
28-
29-
if (require("knitr") && packageVersion("knitr") < '1.9.19')
30-
stop("knitr must be version 1.9.20 or higher")
31-
323
## get the Rmd file to process from the command line, and generate the path
334
## for their respective outputs
345
args <- commandArgs(trailingOnly = TRUE)

0 commit comments

Comments
 (0)