Commit dd5dc4c8 authored by Romain Lesur's avatar Romain Lesur
Browse files

Merge branch 'environments' into 'master'

Add environments

See merge request !1
parents a65d6449 a15cbcc0
Pipeline #169055082 passed with stage
in 5 minutes and 11 seconds
......@@ -40,3 +40,6 @@ public
# Package bibliography
packages.bib
# GitLab review app
review-app.html
image: rocker/verse:3.4.1
image: rocker/verse:4.0.2
pages:
.bookdown:
stage: deploy
script:
- Rscript -e "bookdown::render_book('index.Rmd', 'all', output_dir = 'public')"
- Rscript -e "bookdown::render_book('index.Rmd', 'all', output_dir = 'public')"
artifacts:
paths:
- public
- public
pages:
extends: .bookdown
only:
- master
mr-review:
extends: .bookdown
after_script:
- echo "ENVIRONMENT_URL=https://$CI_PROJECT_NAMESPACE.$CI_PAGES_DOMAIN/-/$CI_PROJECT_NAME/-/jobs/$CI_JOB_ID/artifacts/public/index.html" >> deploy.env
artifacts:
reports:
dotenv: deploy.env
environment:
name: review/$CI_COMMIT_REF_NAME
url: $ENVIRONMENT_URL
only:
- master
- merge_requests
......@@ -7,10 +7,10 @@ A comprehensive documentation of `GitLab Pages` can be found here: https://docs.
`GitLab CI` configuration file is a `YAML` file.
The configuration file begins with `image` declaration. It indicates to the `GitLab Runner` which [`Docker`](https://www.docker.com/) image it has to pull for executing the `CI` job.
```{r comment=NA, class.output='yaml', echo=FALSE}
```{r find-image-name, comment=NA, class.output='yaml', echo=FALSE}
image <-
readLines("./.gitlab-ci.yml") %>%
stringr::str_subset("image") %T>%
stringr::str_subset("rocker") %T>%
cat() %>%
yaml::yaml.load() %$%
image %>%
......@@ -24,7 +24,7 @@ Using a `Rocker` image, **you ensure the reproducibility of your `bookdown` webs
In order to build a `GitLab Pages` website, you have to declare a `pages` job. That is the remaining lines of the `.gitlab-ci.yml` file:
```{r comment=NA, class.output='yaml', echo=FALSE}
```{r extract-pages-job, comment=NA, class.output='yaml', echo=FALSE}
readChar("./.gitlab-ci.yml", 1e5) %>%
strsplit("(?=\\npages:)", perl = TRUE) %>%
unlist() %>%
......
---
title: "Easily publish an R bookdown website with GitLab Pages"
title: "Get the full power of GitLab CI for your bookdown project"
author: "Romain Lesur"
description: "This book provides a demo of a bookdown website published on GitLab Pages"
date: "`r Sys.Date()`"
......@@ -12,6 +12,7 @@ favicon: "favicon.ico"
output:
bookdown::gitbook:
includes:
in_header: review-app.html
after_body: disqus.html
config:
toc:
......@@ -25,7 +26,7 @@ output:
citation_package: natbib
---
# Overview: a simple workflow {-}
# Overview: a powerful workflow {-}
```{r setup, include=FALSE}
library(magrittr)
```
......@@ -34,19 +35,47 @@ library(magrittr)
knitr::write_bib(c(.packages(), 'bookdown'), 'packages.bib')
```
Publishing an `R` [@R-base] `bookdown` [@R-bookdown] website with [`GitLab Pages`](https://about.gitlab.com/features/pages/) is as easy as:
```{r gitlab-review-app, echo=FALSE}
# this chunk builds an HTML fragment which contains a script tag
# this script adds a review app for merge requests
html_content <- ""
- hosting a repository on [`GitLab`](https://gitlab.com)
# the CI_MERGE_REQUEST_IID environment variable only exists when a job runs
# with only: [merge_requests]
if (nzchar(Sys.getenv("CI_MERGE_REQUEST_IID"))) {
fmt <- "<script defer data-project-id='%s' data-project-path='%s' data-merge-request-id='%s' data-mr-url='%s' id='review-app-toolbar-script' src='%s/assets/webpack/visual_review_toolbar.js'></script>"
vars <- Sys.getenv(c(
'CI_PROJECT_ID',
'CI_PROJECT_PATH',
'CI_MERGE_REQUEST_IID',
'CI_SERVER_URL',
'CI_SERVER_URL'
))
html_content <- do.call(sprintf, c(list(fmt = fmt), vars))
}
cat(html_content, file = "review-app.html")
```
Publishing a **bookdown** [@R-bookdown] website with [GitLab Pages](https://about.gitlab.com/features/pages/) is as easy as:
- hosting a repository on [GitLab](https://gitlab.com)
- adding a configuration file to the project
## Host a project on <code>GitLab</code> {-}
You will get a hosting service for your **bookdown** book (like GitHub Pages) and great features
which are not available with GitHub Actions:
As [`GitHub`](https://www.github.com), [`GitLab`](https://gitlab.com) is a web-based [`Git`](https://git-scm.com/) repository manager. Creating a new project on [`GitLab`](https://gitlab.com) is fairly intuitive for [`GitHub`](https://www.github.com) users. [`GitLab`](https://gitlab.com) users can create unlimitate private projects for free (see [here](https://about.gitlab.com/gitlab-com/)).
- **pull requests previews**
- **a review application dedicated to non git users that allows them to comment on these previews**
[`GitLab`](https://gitlab.com) also offers a continuous integration service ([`GitLab CI`](https://about.gitlab.com/features/gitlab-ci-cd/)) and a static websites hosting service ([`GitLab Pages`](https://about.gitlab.com/features/pages/)) in its free plan ^[CI pipelines are limited to 2,000 minutes per month].
These features are also present in the open source software [`GitLab Community Edition (CE)`](https://about.gitlab.com/products/).
## Host a project on GitLab {-}
## Add a `GitLab CI` configuration file {-}
As [GitHub](https://www.github.com), [GitLab](https://gitlab.com) is a web-based [Git](https://git-scm.com/) repository manager. Creating a new project on [GitLab](https://gitlab.com) is fairly intuitive for [GitHub](https://www.github.com) users. [GitLab](https://gitlab.com) users can create unlimitate private projects for free (see [here](https://about.gitlab.com/gitlab-com/)).
[GitLab](https://gitlab.com) also offers a continuous integration service ([GitLab CI](https://about.gitlab.com/features/gitlab-ci-cd/)) and a static websites hosting service ([GitLab Pages](https://about.gitlab.com/features/pages/)) in its free plan ^[CI pipelines are limited to 2,000 minutes per month].
These features are also present in the open source software [GitLab Community Edition (CE)](https://about.gitlab.com/products/) which is widely used in industry as a self hosted git repository manager.
## Add a GitLab CI configuration file {-}
Add the following `.gitlab-ci.yml` file in the root of the project:
```{r insert-ci-file, comment=NA, class.output='yaml', echo=FALSE}
......@@ -57,8 +86,7 @@ For explanations, see section \@ref(CI-file-details).
## See the result {-}
When the `CI` job is done, the website is served on `GitLab Pages`.
When the CI job is done, the website is served on GitLab Pages.
CI jobs status can be found in `CI / CD menu > Jobs`. An example, [here](https://gitlab.com/RLesur/bookdown-gitlab-pages/-/jobs).
The address of the `GitLab Pages` project can be found in `Settings > Pages`. An example, [here](https://gitlab.com/RLesur/bookdown-gitlab-pages/pages).
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment