Skip to content
Snippets Groups Projects
Commit 9007c4af authored by Matthieu Boileau's avatar Matthieu Boileau
Browse files

Update to clean exos

parent b07913ea
No related merge requests found
...@@ -2,4 +2,4 @@ ...@@ -2,4 +2,4 @@
set -x set -x
nbfile="tp-gitlab-ci.ipynb" nbfile="tp-gitlab-ci.ipynb"
jupyter-nbconvert $nbfile --to slides --post serve $2 --allow-errors jupyter-nbconvert $nbfile --to slides --post serve --execute --allow-errors
...@@ -5,7 +5,7 @@ set -x ...@@ -5,7 +5,7 @@ set -x
notebooks="tp-gitlab-ci.ipynb" notebooks="tp-gitlab-ci.ipynb"
revealprefix="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.3.0" revealprefix="https://cdnjs.cloudflare.com/ajax/libs/reveal.js/3.3.0"
jupyter-nbconvert $notebooks --to slides --reveal-prefix $revealprefix --output-dir=public jupyter-nbconvert $notebooks --to slides --reveal-prefix $revealprefix --output-dir=public --execute --allow-errors
jupyter-nbconvert index.ipynb --to html --output-dir=public jupyter-nbconvert index.ipynb --to html --output-dir=public
cp *.ipynb public/ cp *.ipynb public/
cp *.png public/ cp *.png public/
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Journée X/Stra-Dev Intégration continue ### Journée X/Stra-Dev Intégration continue
21 novembre 2017 21 novembre 2017
--- ---
# L'intégration continue avec GitLab # L'intégration continue avec GitLab
![image.png](tanuki.png) ![image.png](tanuki.png)
## *Session pratique* ## *Session pratique*
--- ---
Matthieu Boileau - <matthieu.boileau@math.unistra.fr> Matthieu Boileau - <matthieu.boileau@math.unistra.fr>
*Contenu sous licence [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0)* *Contenu sous licence [CC BY-SA 4.0](https://creativecommons.org/licenses/by-sa/4.0)*
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
# GitLab CI # GitLab CI
GitLab CI propose une chaîne complète d'intégration continue intégrée à son système de forge logicielle. GitLab CI propose une chaîne complète d'intégration continue intégrée à son système de forge logicielle.
![](cicd_pipeline_infograph.png) ![](cicd_pipeline_infograph.png)
<span style="font-size:0.7em;"><https://docs.gitlab.com/ee/ci/README.html></span> <span style="font-size:0.7em;"><https://docs.gitlab.com/ee/ci/README.html></span>
**Les avantages :** **Les avantages :**
- la simplicité de mise en oeuvre - la simplicité de mise en oeuvre
- la robustesse d'une solution intégrée - la robustesse d'une solution intégrée
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Le gitlab runner ## Le gitlab runner
### Introduction ### Introduction
- `gitlab-runner` est un démon installé sur une machine destinée à exécuter les tâches d'intégration continue. - `gitlab-runner` est un démon installé sur une machine destinée à exécuter les tâches d'intégration continue.
- Pour chaque *runner* enregistré, `gitlab-runner` écoute l'instance de gitlab correspondante dans l'attente d'une tâche - Pour chaque *runner* enregistré, `gitlab-runner` écoute l'instance de gitlab correspondante dans l'attente d'une tâche
- Cette tâche est déclenchée par des évènements (`push`, `merge`, etc.) sur le projet avec lequel il est lié - Cette tâche est déclenchée par des évènements (`push`, `merge`, etc.) sur le projet avec lequel il est lié
- Ce lien est créé à l'enregistrement du *runner* par un token associé au projet. - Ce lien est créé à l'enregistrement du *runner* par un token associé au projet.
[**Doc en ligne**](https://docs.gitlab.com/ce/ci/runners/#shared-vs-specific-runners) [**Doc en ligne**](https://docs.gitlab.com/ce/ci/runners/#shared-vs-specific-runners)
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Runners : *shared* vs *specific* ### Runners : *shared* vs *specific*
- *shared* : n'importe quel projet peut s'en servir. Seul l'administrateur du GitLab peut enregistrer des runners partagés. - *shared* : n'importe quel projet peut s'en servir. Seul l'administrateur du GitLab peut enregistrer des runners partagés.
- *specific* : seul le créateur du runner peut s'en servir dans d'autres projets - *specific* : seul le créateur du runner peut s'en servir dans d'autres projets
> **Attention** : les runners partagés sont pratiques mais peuvent présenter des [problèmes de sécurité](https://docs.gitlab.com/ee/ci/runners/README.html#be-careful-with-sensitive-information). > **Attention** : les runners partagés sont pratiques mais peuvent présenter des [problèmes de sécurité](https://docs.gitlab.com/ee/ci/runners/README.html#be-careful-with-sensitive-information).
### Runners : *locked* vs *unlocked runners* ### Runners : *locked* vs *unlocked runners*
- *locked* : le runner ne peut être utilisé que pour le projet avec lequel il est lié - *locked* : le runner ne peut être utilisé que pour le projet avec lequel il est lié
- *unlocked* : le créateur du runner peut l'utiliser dans ses autres projets - *unlocked* : le créateur du runner peut l'utiliser dans ses autres projets
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Installer `gitlab-runner` ### Installer `gitlab-runner`
La **[procédure](https://docs.gitlab.com/runner/install/)** dépend du système d'exploitation. La **[procédure](https://docs.gitlab.com/runner/install/)** dépend du système d'exploitation.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Fork du TP ## Fork du TP
Faire un *fork* du projet sur votre compte GitLab en allant sur la page d'accueil du projet Faire un *fork* du projet sur votre compte GitLab en allant sur la page d'accueil du projet
![fork.png](fork.png) ![fork.png](fork.png)
ou en suivant [ce lien](https://git.unistra.fr/m.boileau/tp-gitlab-ci/forks/new). ou en suivant [ce lien](https://git.unistra.fr/m.boileau/tp-gitlab-ci/forks/new).
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Clone du TP ## Clone du TP
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
- Pour paramétrer les commandes de ce notebook, positionner les variables d'environnement suivantes : - Pour paramétrer les commandes de ce notebook, positionner les variables d'environnement suivantes :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
export USERNAME="m.boileau" # votre login sur git.unistra.fr export USERNAME="m.boileau" # votre login sur git.unistra.fr
export TPBASEDIR=$HOME # le répertoire où vous souhaitez installer le TP export TPBASEDIR=$HOME # le répertoire où vous souhaitez installer le TP
export TPDIR=$TPBASEDIR/tp-gitlab-ci export TPDIR=$TPBASEDIR/tp-gitlab-ci
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
- Cloner localement votre projet - Cloner localement votre projet
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
cd $TPBASEDIR cd $TPBASEDIR
git clone git@git.unistra.fr:$USERNAME/tp-gitlab-ci.git git clone git@git.unistra.fr:$USERNAME/tp-gitlab-ci.git
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Enregistrer un nouveau runner ## Enregistrer un nouveau runner
> Si `gitlab-runner` n'est pas installé sur votre machine, rdv à la section **Exo1** > Si `gitlab-runner` n'est pas installé sur votre machine, rdv à la section **Exo1**
Engistrer un runner consiste à créer un nouveau runner associé à un projet. Engistrer un runner consiste à créer un nouveau runner associé à un projet.
Si vous n'êtes pas administrateur du GitLab, il s'agit forcément d'un runner spécifique. Celui-ci se trouve dans Si vous n'êtes pas administrateur du GitLab, il s'agit forcément d'un runner spécifique. Celui-ci se trouve dans
votre projet, rubrique : votre projet, rubrique :
``` ```
Settings > CI / CD > Runners settings Settings > CI / CD > Runners settings
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Dans le terminal, maintenant : Dans le terminal, maintenant :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
export CI_SERVER_URL=https://git.unistra.fr/ # Do not change for this session export CI_SERVER_URL=https://git.unistra.fr/ # Do not change for this session
export RUNNER_NAME=a_runner_for_$USERNAME export RUNNER_NAME=a_runner_for_$USERNAME
export REGISTRATION_TOKEN=xxx # Replace by the value provided by the runner setting page export REGISTRATION_TOKEN=xxx # Replace by the value provided by the runner setting page
export REGISTER_NON_INTERACTIVE=true export REGISTER_NON_INTERACTIVE=true
#gitlab-runner register --executor shell # sous MacOS #gitlab-runner register --executor shell # sous MacOS
#sudo gitlab-runner register --executor shell # sous Linux #sudo gitlab-runner register --executor shell # sous Linux
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Ou bien lancez la commande en mode interactif et en suivant pas à pas la [procédure](http://docs.gitlab.com/runner/register/#registering-runners): Ou bien lancez la commande en mode interactif et en suivant pas à pas la [procédure](http://docs.gitlab.com/runner/register/#registering-runners):
``` ```
gitlab-runner register # sous MacOS gitlab-runner register # sous MacOS
sudo gitlab-runner register # sous Linux sudo gitlab-runner register # sous Linux
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
- Retournez sur la page de paramètrage des runners - Retournez sur la page de paramètrage des runners
- Repérez votre nouveau runner - Repérez votre nouveau runner
- Editez-le et ajouter les tags `specific, shell` - Editez-le et ajouter les tags `specific, shell`
Votre runner est maintenant prêt à travailler. Nous allons le tester en lui soumettant une tâche GitLab CI déclenchée par un `git push` sur votre projet. Votre runner est maintenant prêt à travailler. Nous allons le tester en lui soumettant une tâche GitLab CI déclenchée par un `git push` sur votre projet.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Exo1 : helloworld en bash ## Exo1 : helloworld en bash
### Mettre en place une action d'intégration continue ### Mettre en place une action d'intégration continue
Allez dans le répertoire racine du clone local de votre projet et basculez sur la branche `exo1` Allez dans le répertoire racine du clone local de votre projet et basculez sur la branche `exo1`
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
cd $TPDIR cd $TPDIR
cd tp-gitlab-ci
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
git pull git pull
git stash
git checkout exo1 git checkout exo1
git checkout exo1-start .gitlab-ci.yml # Clean previous modifications
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Repérer le fichier `.gitlab-ci.yml` Repérer le fichier `.gitlab-ci.yml`
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
ls -al ls -al
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
L'éditer avec l'éditeur de votre choix L'éditer avec l'éditeur de votre choix
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
cat .gitlab-ci.yml cat .gitlab-ci.yml
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
- `helloworld` indique le nom du job d'intégration continue - `helloworld` indique le nom du job d'intégration continue
- `tags` permet de sélectionner des runners sur la base de mot-clés - `tags` permet de sélectionner des runners sur la base de mot-clés
- `scripts` correspond aux lignes de commande `bash` que vous souhaitez exécuter - `scripts` correspond aux lignes de commande `bash` que vous souhaitez exécuter
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Si vous souhaitez utiliser un runner partagé ### Si vous souhaitez utiliser un runner partagé
Dans la rubrique : Dans la rubrique :
``` ```
Settings > CI / CD > Runners settings Settings > CI / CD > Runners settings
``` ```
Notez la présence de plusieurs runners partagés dont `clu1-tp-gitlab-ci` qui porte les tags `shell` et `docker` mais pas `specific`. Pour cibler ce runner, retirer le tag `specific` du fichier `.gitlab-ci.yml` Notez la présence de plusieurs runners partagés dont `clu1-tp-gitlab-ci` qui porte les tags `shell` et `docker` mais pas `specific`. Pour cibler ce runner, retirer le tag `specific` du fichier `.gitlab-ci.yml`
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Déclenchez une action d'intégration continue ### Déclenchez une action d'intégration continue
Modifiez la ligne de script, par exemple: Modifiez la ligne de script, par exemple:
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
echo ' - echo "bonjour, monde"' >> .gitlab-ci.yml echo ' - echo "bonjour, monde"' >> .gitlab-ci.yml
cat .gitlab-ci.yml cat .gitlab-ci.yml
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Enregistrez avec git Enregistrez avec git
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
git add .gitlab-ci.yml git add .gitlab-ci.yml
git commit -m "Update .gitlab-ci.yml" git commit -m "Update .gitlab-ci.yml"
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Pousser vos modifications Poussez vos modifications
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
git push git push
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Sur GitLab, observez l'exécution de votre job dans la rubrique de votre projet : Sur GitLab, observez l'exécution de votre job dans la rubrique de votre projet :
``` ```
CI / CD > Pipelines CI / CD > Pipelines
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Provoquez une erreur ### Provoquez une erreur
Dans `.gitlab-ci.yml`, introduisez une erreur dans une des lignes du script et observer l'effet dans `CI / CD > Pipelines` après avoir pousser les modifications. Dans `.gitlab-ci.yml`, introduisez une erreur dans une des lignes du script et observer l'effet dans `CI / CD > Pipelines` après avoir poussé les modifications.
> **Note :** les fichiers peuvent s'éditer directement dans GitLab. > **Note :** les fichiers peuvent s'éditer directement dans GitLab.
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
## Exo2 : helloworld en C ## Exo2 : helloworld en C
### Description ### Description
On écrit le programme `helloworld` en C qui doit afficher `hello, world`. On écrit le programme `helloworld` en C qui doit afficher `hello, world`.
### Mettre en place une action de *build* ### Mettre en place une action de *build*
Dans votre clone local, basculez sur la branche `exo2` Dans votre clone local, basculez sur la branche `exo2`
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
cd $TPDIR cd $TPDIR
git stash # si vous avez des modifications non enregistrées dans exo1 git stash # si vous avez des modifications non enregistrées dans exo1
git checkout exo2 git checkout exo2
git rm .gitlab-ci.yml # Clean previous commits
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Listez le contenu du répertoire Listez le contenu du répertoire
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
ls -al ls -al
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Vérifiez que le progamme compile Vérifiez que le progamme compile
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
make make
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
On crée le fichier `.gitlab-ci.yml` avec une étape (`stage`) de `build`. Il doit contenir : On crée le fichier `.gitlab-ci.yml` avec une étape (`stage`) de `build`. Il doit contenir :
```yaml ```yaml
build_hello: build_hello:
stage: build stage: build
tags: tags:
- shell - shell
script: script:
- make - make
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
cat > .gitlab-ci.yml <<- EOM cat > .gitlab-ci.yml <<- EOM
build_hello: build_hello:
stage: build stage: build
tags: tags:
- shell - shell
script: script:
- make - make
EOM EOM
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
On vérifie le contenu On vérifie le contenu
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
cat .gitlab-ci.yml cat .gitlab-ci.yml
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
On enregistre et on pousse. On enregistre et on pousse.
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
git add .gitlab-ci.yml git add .gitlab-ci.yml
git commit -m "Add .gitlab-ci.yml with a build stage" git commit -m "Add .gitlab-ci.yml with a build stage"
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
git push git push
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Sur GitLab, observez l'exécution de votre job dans la rubrique de votre projet : Sur GitLab, observez l'exécution de votre job dans la rubrique de votre projet :
``` ```
CI / CD > Pipelines CI / CD > Pipelines
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Ajouter une action de *test* ### Ajouter une action de *test*
On souhaite ajouter une étape de test réalisée avec `make test`. Tester l'exécution du test (il ne doit pas renvoyer d'erreur) : On souhaite ajouter une étape de test réalisée avec `make test`. Tester l'exécution du test (il ne doit pas renvoyer d'erreur) :
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
make test make test
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
Il faut ajouter le contenu suivant : Il faut ajouter le contenu suivant :
```yaml ```yaml
test_hello: test_hello:
stage: test stage: test
tags: tags:
- shell - shell
script: script:
- make test - make test
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
cat >> .gitlab-ci.yml <<- EOM cat >> .gitlab-ci.yml <<- EOM
test_hello: test_hello:
stage: test stage: test
tags: tags:
- shell - shell
script: script:
- make test - make test
EOM EOM
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
cat .gitlab-ci.yml cat .gitlab-ci.yml
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
On enregistre, on pousse et on vérifie l'exécution de la chaîne d'intégration continue dans `CI / CD > Pipelines` On enregistre, on pousse et on vérifie l'exécution de la chaîne d'intégration continue dans `CI / CD > Pipelines`
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
git add .gitlab-ci.yml git add .gitlab-ci.yml
git commit -m "Add a test stage to .gitlab-ci.yml" git commit -m "Add a test stage to .gitlab-ci.yml"
``` ```
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
git push git push
``` ```
%% Cell type:markdown id: tags: %% Cell type:markdown id: tags:
### Provoquez des erreurs ### Provoquez des erreurs
Modifier les fichiers Modifier les fichiers
- de build : notez que le test n'est pas exécuté - de build : notez que le test n'est pas exécuté
- de test : le - de test : le
%% Cell type:code id: tags: %% Cell type:markdown id: tags:
``` bash
Récupérer un artifact
```
%% Output
bash: Récupérer: command not found
## Récupérer un artifact
%% Cell type:code id: tags: %% Cell type:code id: tags:
``` bash ``` bash
``` ```
......
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