Aller au contenu

Corrections des 5 TP

TP1

Ici, on utilise la variable GitLab CI_COMMIT_SHORT_SHA : est un identifiant court du commit, idéal pour versionner automatiquement un artefact ou une image Docker

Mais il y en a plein d’autres selon nos besoins :

Variable Contenu Exemple
CI_COMMIT_SHA Hash complet du commit (40 caractères) 4a7f2b93cdab12983f7c51f1...
CI_COMMIT_SHORT_SHA Hash court (8 caractères) 4a7f2b93
CI_COMMIT_REF_NAME Nom de la branche ou du tag main / feature-x / v1.0
CI_COMMIT_BRANCH Nom de la branche (si pas tag) main
CI_COMMIT_TAG Nom du tag (si pipeline déclenché par un tag) v1.0
CI_COMMIT_MESSAGE Message du commit “Fix bug API”
stages:
  - test

hello-world:
  stage: test
  script:
    - echo "Coucou de GitLab CI/CD"
    - echo "commit: $CI_COMMIT_SHORT_SHA"
    - uname -a
    - ls -la

TP2

stages:
  - build
  - test

default:
  image: maven:3.9-eclipse-temurin-17
  cache:
    key: "maven-cache"
    paths:
      - .m2/repository/
    policy: pull-push

maven-build:
  stage: build
  script:
    - mvn -B -ntp clean compile
  artifacts:
    paths:
      - target/
    expire_in: 1h

maven-test:
  stage: test
  script:
    - mvn -B -ntp test
  artifacts:
    when: always
    reports:
      junit: target/surefire-reports/*.xml
    paths:
      - target/surefire-reports/
    expire_in: 1h

TP3

stages:
  - lint
  - test

default:
  image: maven:3.9-eclipse-temurin-17
  cache:
    key: "maven-cache"
    paths: [ .m2/repository/ ]
    policy: pull-push

lint-code:
  stage: lint
  script:
    - mvn -B -ntp checkstyle:check spotless:check

maven-test:
  stage: test
  script:
    - mvn -B -ntp test
  artifacts:
    when: always
    reports:
      junit: target/surefire-reports/*.xml

TP4

stages:
  - build
  - docker

maven-build:
  stage: build
  image: maven:3.9-eclipse-temurin-17
  script:
    - mvn -B -ntp clean package -DskipTests
  artifacts:
    paths:
      - target/
    expire_in: 1h

docker-build:
  stage: docker
  image: docker:latest
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
  before_script:
    - echo "$CI_JOB_TOKEN" | docker login -u "$CI_REGISTRY_USER" --password-stdin $CI_REGISTRY
  script:
    - docker build -t $CI_REGISTRY_IMAGE:latest .
    - docker push $CI_REGISTRY_IMAGE:latest

TP5

stages: [build, run]

# Ces jobs exigent un runner local Windows taggé: shell, windows
maven-build:
  stage: build
  tags:
    - shell
    - windows
  image: maven:3.9-eclipse-temurin-17
  script:
    - mvn -B -ntp clean package -DskipTests
  artifacts:
    paths: [ target/ ]
    expire_in: 1 week

run-local:
  stage: run
  tags:
    - shell
    - windows
  when: manual
  script:
    - echo "Lancement local du JAR"
    - java -jar target/*.jar