[comment]: # (mdslides presentation.md --include media && cp custom.css presentation/dist/theme/) # CI/CD ---------- ASARD
## Définitions ### CI/CD Continuous Integration / Continuous Deployment - Assure la bonne qualité du code - Automatise des actions - Facilite le cycle de développement
### CI/CD - DevOps - MLOps - DataOps - SecOps - ...
### CI/CD Exemple d'actions - tests unitaires - formattage de code - documentation automatique - publication de package - gestion des versions - vérifications - monitoring
### Runner Environnement virtuel instancié sur une machine où s'exécute indépendamment chaque tâche.
### Job Tâche à éxécuter. 
### Stage Étapes successives. Un ou plusieurs jobs par stage. 
### Pipeline Toutes les étapes pour parvenir à une action. 
### Statuts  - running - success - failed - skipped - canceled - pending - scheduled - manual
## Initialisation ---------- gitlab.lisn.upsaclay.fr/asard/ateliers-gitlab/cicd
## Création de runner ---------- 🐋💻🦊
## Exercice 1 ---------- Tests automatisés
norms.py ```py [] from math import sqrt def manhattan(x): norm = 0 for v in x: norm += abs(v) return norm def euclidean(x): norm = 0 for v in x: norm += v**2 return sqrt(norm) ```
main.py ```py [] import norms x = [2,1,-3,2] # computes the manhattan norm for x l1 = norms.manhattan(x) print(l1) # computes the euclidean norm for x l2 = norms.euclidean(x) print(l2) ```
.gitlab-ci.yml ```yml [1-23|1|3-6|8-12|14-23] image: python:3.11 stages: - hello - test - deploy useless-job: stage: hello script: echo "Hello" rules: - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH tests: stage: test before_script: - python3 -m venv demoenv - source demoenv/bin/activate - pip install -r requirements.txt script: - pytest rules: - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH ```
.gitlab-ci.yml ```yml [14-28|17-21] image: python:3.11 stages: - hello - test - deploy useless-job: stage: hello script: echo "Hello" rules: - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH tests: stage: test before_script: - | if [ ! -f test_*.py ]; then echo "no test file provided" exit 1 fi - python3 -m venv demoenv - source demoenv/bin/activate - pip install -r requirements.txt script: - pytest rules: - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH ```
## Exercice 2 ---------- Couverture de code
coverage.py Valide si toutes les fonctions ont un test associé.
.gitlab-ci.yml ```yml [1-30|3-4|8-9|10-15|16-17|18-19] image: python:3.11 stages: - check - test - deploy coverage: stage: check before_script: - | if [ ! -f tests/test_*.py ]; then echo "no test file provided" exit 1 fi script: - python3 coverage.py rules: - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH tests: stage: test before_script: - python3 -m venv demoenv - source demoenv/bin/activate - pip install -r requirements.txt script: - pytest rules: - if: $CI_MERGE_REQUEST_TARGET_BRANCH_NAME == $CI_DEFAULT_BRANCH ```
## Exercice 3 ---------- Déploiement de documentation
Changement de structure ```sh . ├── demoenv ├── src │ └── demo │ ├── __init__.py │ └── norms.py ├── tests │ └── test_norms.py ├── coverage.py ├── main.py ├── requirements.txt ├── .gitignore └── .gitlab-ci.yml ```
.gitlab-ci.yml ```yml [32: 1-15|1-2|3|4-7|8-10|11-13|14-15] pages: #pages: true stage: deploy before_script: - python3 -m venv demoenv - source demoenv/bin/activate - pip install -r requirements.txt script: - cd docs - make html - cd .. - mkdir public - cp -r docs/build/html/* public rules: - if: '$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' ```
## Exercice 4 ---------- Badges
.gitlab-ci.yml ```yml [47: 1-18|1-2|3-4|5-7|8-11|12-16|17-18] tag-badge: stage: deploy variables: GIT_STRATEGY: clone before_script: - apt update -y - apt install -y jq - git config user.email "$GITLAB_USER_EMAIL" - git config user.name "$GITLAB_USER_LOGIN" - git remote set-url origin https://${GITLAB_USER_LOGIN}:${
_ACCESS_TOKEN}@gitlab.lisn.upsaclay.fr/
/cicd.git - git checkout -b dev script: - echo "$(jq --arg tag $CI_COMMIT_TAG '.message = $tag' mybadge.json)" > mybadge.json - git add mybadge.json - git commit -m "edit tag" - git push origin dev rules: - if: '$CI_COMMIT_TAG && $GITLAB_USER_ID == "
"' ```
## C'est fini ------------ Merci