-
Notifications
You must be signed in to change notification settings - Fork 10
161 lines (148 loc) · 8.18 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
name: build
on:
workflow_dispatch:
push:
branches-ignore:
- gh-pages
env:
REGISTRY: ghcr.io
ORG: nbisweden
IMAGE: workshop-r:latest
jobs:
rmd-render-job:
runs-on: ubuntu-latest
steps:
- name: Set time zone
uses: szenius/[email protected]
with:
timezoneLinux: "Europe/Stockholm"
timezoneMacos: "Europe/Stockholm"
timezoneWindows: "W. Europe Standard Time"
- name: Checkout
uses: actions/checkout@v3
- name: Pull image
run: docker pull ${{ env.REGISTRY }}/${{ env.ORG }}/${{ env.IMAGE }}
- 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=$(basename ${GITHUB_REPOSITORY})" >> $GITHUB_ENV
- name: Check docker and variables
run: |
docker run --rm -u $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}:/rmd ${{ env.REGISTRY }}/${{ env.ORG }}/${{ env.IMAGE }} Rscript -e "print('R in docker works!')"
echo "GITHUB_ACTOR: ${GITHUB_ACTOR}"
echo "GITHUB_REPOSITORY: ${GITHUB_REPOSITORY}"
echo "REPO: ${REPO}"
echo "env.PATH_REPO: ${{ env.PATH_REPO }}"
echo "PATH_EXPORT: ${PATH_EXPORT}"
echo "PATH_EXPORT_CANVAS: ${PATH_EXPORT_CANVAS}"
echo "GITHUB_REPOSITORY_OWNER: ${GITHUB_REPOSITORY_OWNER}"
echo "---------------------------------------------------------------------------------------------"
echo "Folder contents at $(pwd):"
ls -lh
echo "---------------------------------------------------------------------------------------------"
printenv
sed --version
- 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 ..."
docker run --rm -u $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}:/rmd ${{ env.REGISTRY }}/${{ env.ORG }}/${{ env.IMAGE }} Rscript -e 'rmarkdown::render_site()'
# Generate PDFs
cd ${PATH_EXPORT}
#echo "Generating PDFs ..."
#docker run --rm -u $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}:/rmd ${{ env.REGISTRY }}/${{ env.ORG }}/${{ env.IMAGE }} Rscript -e "l <- list.files('.',pattern='slide.+html');if(length(l)!=0) {for(i in 1:length(l)) pagedown::chrome_print(l[i],extra_args=c('--no-sandbox','--disable-dev-shm-usage','--disable-gpu'))};"
cd ..
# 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
echo "Removing lab and slide links from schedule for canvas ..."
docker run --rm -u $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}:/rmd ${{ env.REGISTRY }}/${{ env.ORG }}/${{ env.IMAGE }} 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 ..."
docker run --rm -u $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}:/rmd ${{ env.REGISTRY }}/${{ env.ORG }}/${{ env.IMAGE }} 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 ..."
docker run --rm -u $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}:/rmd ${{ env.REGISTRY }}/${{ env.ORG }}/${{ env.IMAGE }} Rscript -e 'rmarkdown::render_site()'
cd ${PATH_EXPORT_CANVAS}
# Fix links
# 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 ..."
#docker run --rm -u $(id -u ${USER}):$(id -g ${USER}) -v ${PWD}:/rmd ${{ env.REGISTRY }}/${{ env.ORG }}/${{ env.IMAGE }} Rscript -e "l <- list.files('.',pattern='slide.+html');if(length(l)!=0) {for(i in 1:length(l)) pagedown::chrome_print(l[i],extra_args=c('--no-sandbox','--disable-dev-shm-usage','--disable-gpu'))};"
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
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