-
Notifications
You must be signed in to change notification settings - Fork 10
204 lines (192 loc) · 10.2 KB
/
main.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
name: build
on:
push:
branches-ignore:
- gh-pages
jobs:
rmd-render-job:
strategy:
fail-fast: false
matrix:
config:
- {os: ubuntu-20.04, r: '4.2'}
runs-on: ${{ matrix.config.os }}
steps:
- name: Set time zone
uses: szenius/[email protected]
with:
timezoneLinux: "Europe/Stockholm"
timezoneMacos: "Europe/Stockholm"
timezoneWindows: "W. Europe Standard Time"
- uses: actions/checkout@v3
- uses: r-lib/actions/setup-r@v2
with:
r-version: ${{ matrix.config.r }}
- uses: r-lib/actions/setup-pandoc@v2
- name: Install linux dependencies
run: |
sudo apt-get update
sudo apt-get install libxml2-dev libssl-dev libcurl4-openssl-dev libudunits2-dev libopenblas-base libgdal-dev libfreetype6-dev libpng-dev libtiff5-dev libjpeg-dev libfontconfig1-dev libharfbuzz-dev libfribidi-dev
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo apt install ./google-chrome-stable_current_amd64.deb
rm -rf google-chrome-stable_current_amd64.deb
- name: Define variables
run: |
echo "path_repo=$(pwd)" >> $GITHUB_ENV
echo "path_export=$(grep -E '^output_dir' _site.yml | sed 's/^output_dir://' | xargs)" >> $GITHUB_ENV
echo "path_export_canvas=$(grep -E '^output_dir' _site.yml | sed 's/^output_dir://' | xargs)-canvas" >> $GITHUB_ENV
echo "repo=$(echo $GITHUB_REPOSITORY | sed 's/.*\///')" >> $GITHUB_ENV
- name: Check R installation and variables
run: |
Rscript -e "print('R installation works!')"
echo "Github actor: ${GITHUB_ACTOR}"
echo "Github repository: ${GITHUB_REPOSITORY}"
echo "Github repo: ${repo}"
echo "Path repo: ${path_repo}"
echo "Path export: ${path_export}"
echo "Path export canvas: ${path_export_canvas}"
echo "---------------------------------------------------------------------------------------------"
echo "Folder contents at $(pwd):"
ls -lh
echo "---------------------------------------------------------------------------------------------"
printenv
sed --version
- name: Print R packages
run: |
# parse r packages from Rmd and md files
# grep -Eroh "(^library\([A-Za-z0-9]+\))|(^require\([A-Za-z0-9]+\))" *md | sed 's/library\|(\|)//g' | sort | uniq > r-packages.txt
# parse r packages from _site.yml
awk '/^[^ ]/{ f=/^packages:/; next } f{ if (sub(/:$/,"")) pkg=$2; else print pkg, $2 }' _site.yml | uniq | sort > r-packages.txt
cat r-packages.txt
- name: Cache R packages
id: cache-rp
uses: actions/cache@v3
with:
path: ${{ env.R_LIBS_USER }}
key: ${{ runner.os }}-${{ matrix.config.r }}-${{ hashFiles('r-packages.txt') }}
- name: Install R dependencies
if: steps.cache-rp.outputs.cache-hit != 'true'
env:
GITHUB_PAT: ${{ secrets.PAT }}
run: |
Rscript -e 'install.packages(c("yaml","pagedown"))'
Rscript -e 'if(!is.null(yaml::read_yaml("_site.yml")$packages$packages_cran_repo)) install.packages(yaml::read_yaml("_site.yml")$packages$packages_cran_repo)'
Rscript -e 'if(!is.null(yaml::read_yaml("_site.yml")$packages$packages_cran_student)) install.packages(yaml::read_yaml("_site.yml")$packages$packages_cran_student)'
Rscript -e 'if(!is.null(yaml::read_yaml("_site.yml")$packages$packages_bioc_repo)) BiocManager::install(as.character(yaml::read_yaml("_site.yml")$packages$packages_bioc_repo))'
Rscript -e 'if(!is.null(yaml::read_yaml("_site.yml")$packages$packages_bioc_student)) BiocManager::install(as.character(yaml::read_yaml("_site.yml")$packages$packages_bioc_student))'
Rscript -e 'if(!is.null(yaml::read_yaml("_site.yml")$packages$packages_github_repo)) remotes::install_github(yaml::read_yaml("_site.yml")$packages$packages_github_repo)'
Rscript -e 'if(!is.null(yaml::read_yaml("_site.yml")$packages$packages_github_student)) remotes::install_github(yaml::read_yaml("_site.yml")$packages$packages_github_student)'
Rscript -e 'rownames(installed.packages())'
echo "R directory size:"
du -sh ${{ env.R_LIBS_USER }}
- name: Render site, pull repo, copy folder, create index, push repo
run: |
# Add git info
git config --global user.email "[email protected]"
git config --global user.name "gh_action"
# Render website
echo "Rendering Rmd files ..."
Rscript -e "rmarkdown::render_site()"
# Render canvas version of website by changing _site.yml
# suffixes output directory with -canvas
# adds current output directory to be ignored
# renames navbar block to remove menu
# removes footer
#sed 's/\(output_dir:\)\s*\([0-9]*\)/\1 \2-canvas/' _site.yml | \
#sed "/^exclude/s/\]$/,\"${path_export}\"]/" | \
#sed 's/navbar:/unused:/' | \
#sed 's/include://' | \
#sed 's/after_body: assets\/footer-lab.html//' > _site.yml
echo "Removing lab and slide links from schedule for canvas ..."
Rscript -e 'x <- read.csv("schedule.csv",sep=";",header=T,stringsAsFactors=F);x$link_youtube <- x$link_lab <- x$link_slide <- NA;write.table(x,"schedule.csv",sep=";",col.names=T,row.names=F,quote=FALSE,na="")'
echo "Fixing _site.yml for canvas ..."
Rscript -e 'y <- yaml::read_yaml("_site.yml"); y$exclude <- append(y$exclude,y$output_dir); y$output_dir <- paste0(y$output_dir,"-canvas"); y$navbar <- NULL; yaml::write_yaml(y,"_site.yml");'
ls -lh
cat _site.yml
echo "Rendering Rmd files for canvas ..."
Rscript -e "rmarkdown::render_site()"
# Modify links and generate PDFs
cd ${path_export}
Rscript -e "l <- list.files('.',pattern='slide.+html');if(length(l)!=0) {for(i in 1:length(l)) pagedown::chrome_print(l[i])};"
cd ..
cd ${path_export_canvas}
# all links (except those starting with #) in all html files are set to open in new tab
# all links (those pointing within website and ends with html) in all html files are removed
# display links to be fixed
echo "Number of links to be corrected:"
grep -E '(<a href="[^#][^(https?)&^(www)?].+html.*">)([^(a>).]+)(<\/a>)' *.html | \
grep -E '(<a href="[^#][-a-zA-Z0-9@:%._\+~#=/?&]+")(>)' | wc -l
echo "List of links to be corrected:"
grep -E '(<a href="[^#][^(https?)&^(www)?].+html.*">)([^(a>).]+)(<\/a>)' *.html | \
grep -E '(<a href="[^#][-a-zA-Z0-9@:%._\+~#=/?&]+")(>)'
echo "Fixing bad links ..."
for i in *.html; do
sed -E 's/<h1 class="title toc-ignore">[^>.]*<\/h1>//g' $i | \
sed -E 's/<h3 class="subtitle">[^>.]*<\/h3>//g' | \
sed -E 's/<h4 class="author">[^>.]*<\/h4>//g' | \
sed -E 's/(<a href="[^#][^(https?)&^(www)?].+(html|pdf).*">)([^(a>).]+)(<\/a>)/[link removed]/g' | \
sed -E 's/(<a href="[^#][-a-zA-Z0-9@:%._\+~#=/?&]+")(>)/\1 target="_blank">/g' > temp
cat temp > $i
rm -rf temp
done
echo "Generating PDFs for HTML slides ..."
Rscript -e "l <- list.files('.',pattern='slide.+html');if(length(l)!=0) {for(i in 1:length(l)) pagedown::chrome_print(l[i])};"
cd ..
# List folder contents
echo "Folder contents at $(pwd):"
ls -lh
echo "Folder contents at ${path_repo}/${path_export}:"
ls -lh ${path_repo}/${path_export}
echo "Folder contents at ${path_repo}/${path_export_canvas}:"
ls -lh ${path_repo}/${path_export_canvas}
# Clone gh-pages branch and remove old directories with same name
git clone --single-branch --branch gh-pages "https://${{ secrets.TOKEN }}@github.com/${GITHUB_REPOSITORY}.git" tmprepo
cd tmprepo
if [ -d $path_export ]; then
echo "Directory ${path_export} already exists. Removing the directory."
git rm -r $path_export
git commit -m "Old directory ${path_export} deleted."
fi
if [ -d $path_export_canvas ]; then
echo "Directory ${path_export_canvas} already exists. Removing the directory."
git rm -r $path_export_canvas
git commit -m "Old directory ${path_export_canvas} deleted."
fi
cd ${path_repo}
cp -r $path_export tmprepo/
cp -r $path_export_canvas tmprepo/
cd tmprepo
echo "Folder contents at $(pwd):"
ls -lh
# Create index file
echo "Full repo: ${GITHUB_REPOSITORY}"
str_user=$(dirname ${GITHUB_REPOSITORY})
echo "User: $str_user"
repo=$(basename ${GITHUB_REPOSITORY})
echo "Repo: $repo"
# create index file
printf "<div class='wrapper-logo'><img class='logo' src='assets/logo.svg'></div>" > index.md
printf "The current and all previous instances of this workshop are listed below.\n" >> index.md
printf "<hr>\n" >> index.md
printf "<div class='workshop-list'>\n" >> index.md
dirs=$(ls -d [0-9]*[0-9]/ | sed 's/\///' | tac)
for i in ${dirs[@]}
do
if [ ${i:2:2} -lt 1 ] || [ ${i:2:2} -gt 12 ]
then echo "Month: ${i:2:2}. Last 2 values in output_dir must be a month between 01 and 12."
fi
# This code works for year 2000 and later
printf -v thedate '2000-01-01 +%s years +%s months -1 day' "${i:0:2}" "${i:2}"
dt=$(date -d "$thedate" +'%Y %b')
echo $dt
printf "<h3><a href='https://${GITHUB_REPOSITORY_OWNER}.github.io/${repo}/${i}/'>$dt</a></h3>" >> index.md
done
printf "</div>\n" >> index.md
# printf "<hr>\n" >> index.md
# printf "$(date +'%Y') • NBIS\n" >> index.md
echo "Folder contents at $(pwd):"
ls -lh
# Push changes back
git add .
git commit -m "Updated contents at $(date +%Y%m%d-%H%M%S)"
git push origin