web:framework:spring:deployment

Différences

Ci-dessous, les différences entre deux révisions de la page.

Lien vers cette vue comparative

Les deux révisions précédentes Révision précédente
Prochaine révision
Révision précédente
web:framework:spring:deployment [2023/11/29 02:06] – [Mise en place CI/CD] jcheronweb:framework:spring:deployment [2023/12/17 17:41] (Version actuelle) – [Configuration Tomcat sur VM] jcheron
Ligne 275: Ligne 275:
 spring.h2.console.settings.web-allow-others=true spring.h2.console.settings.web-allow-others=true
 spring.h2.console.path=/h2-console spring.h2.console.path=/h2-console
 +
 +servlet.context.path=/
 +
 </sxh> </sxh>
  
Ligne 283: Ligne 286:
 spring.jpa.show-sql=true spring.jpa.show-sql=true
 spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.format_sql=true
- 
-servlet.context.path=/ 
 </sxh> </sxh>
  
Ligne 297: Ligne 298:
  
 <sxh bash;application-dev.properties> <sxh bash;application-dev.properties>
-spring.datasource.url=jdbc:h2:file:/data/h2/paris-2024/data;DB_CLOSE_ON_EXIT=FALSE+spring.datasource.url=jdbc:h2:file:/data/h2/paris-2024;DB_CLOSE_ON_EXIT=FALSE
 spring.jpa.show-sql=false spring.jpa.show-sql=false
 spring.jpa.properties.hibernate.format_sql=false spring.jpa.properties.hibernate.format_sql=false
  
-server.servlet.context-path=/paris-2024 
 </sxh> </sxh>
  
Ligne 327: Ligne 327:
   script: "mvn --batch-mode --update-snapshots verify -P test -DskipTests=false"   script: "mvn --batch-mode --update-snapshots verify -P test -DskipTests=false"
 </sxh> </sxh>
 +
 +===== Déploiement avec variables =====
 +Il est parfois indispensable, pour des raisons de sécurité, de stocker certaines constantes dans les variables CI de gitlab, pour les affecter au déploiement par l'intermédiaire de variables d'environnement plutôt que de les mettre en dur dans le code :
 +  * Mots de passe (BDD)
 +  * Clé de sécurité (Chiffrement)
 +
 +==== Configuration Tomcat sur VM ====
 +Il est nécessaire de modifier le service de démarrage de tomcat pour qu'il prenne en compte les variables d'environnement (via le fichier **setenv.sh**), en utilisant **catalina.sh** comme script de démarrage au lieu de **startup.sh** :
 +
 +<sxh bash>
 +
 +[Unit]
 +Description=Tomcat
 +After=network.target
 +
 +[Service]
 +Type=simple
 +
 +User=tomcat
 +Group=tomcat
 +
 +Environment="JAVA_HOME=/usr/lib/jvm/jdk-17-oracle-x64"
 +Environment="JAVA_OPTS=-Djava.security.egd=file:///dev/urandom"
 +Environment="CATALINA_BASE=/opt/tomcat"
 +Environment="CATALINA_HOME=/opt/tomcat"
 +Environment="CATALINA_PID=/opt/tomcat/temp/tomcat.pid"
 +Environment="CATALINA_OPTS=-Xms512M -Xmx1024M -server -XX:+UseParallelGC"
 +
 +ExecStart=/opt/tomcat/bin/catalina.sh run
 +ExecStop=/opt/tomcat/bin/catalina.sh stop
 +
 +[Install]
 +WantedBy=multi-user.target
 +
 +</sxh>
 +
 +Recharger le service et redémarer le :
 +<sxh bash;gutter:false>
 +systemctl daemon-reload
 +systemctl start tomcat.service
 +</sxh>
 +
 +
 +
 +==== Script de déploiement ====
 +Créer une variable **CI_APP_KEY** dans les variables CI de votre compte gitlab.
 +
 +Le script de déploiement doit maintenant ajouter la variable d'environnement **CI_APP_KEY** dans le fichier **setenv.sh** du serveur :
 +
 +<sxh yml;highlight:[34,35]>
 +stages:
 +  - build
 +  - deploy
 +
 +maven-build:
 +  image: maven:3.9.5-amazoncorretto-17-debian
 +  stage: build
 +  script: "mvn clean package -P prod -DskipTests=true"
 +  artifacts:
 +    paths:
 +      - target/paris-2024.war
 +
 +deploy-master:
 +  variables:
 +    HOST: "149.202.94.223"
 +    PORT: "78xx"
 +    USER: "gitlab-ci"
 +    WAR: "paris-2024.war"
 +    SITE_LOCATION: "/opt/tomcat/webapps/paris-2024"
 +  rules:
 +    - if: '$CI_COMMIT_BRANCH =~ /^main$/'
 +  before_script:
 +    - apt-get update -qq && apt-get install -y -qq sshpass sudo
 +    - echo "Host= $HOST"
 +  stage: deploy
 +  script:
 +    - sudo whoami  # Vérifiez si sudo est disponible
 +    - which mv
 +    - sshpass -V
 +    - export SSHPASS=$CI_USER_PASS
 +    - sshpass -e scp -o StrictHostKeyChecking=no -P $PORT target/$WAR $USER@$HOST:/home/$USER
 +    - sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST echo $PATH
 +    - sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST sudo mv /home/$USER/$WAR $SITE_LOCATION/ROOT.war
 +    - sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST "sudo chmod 755 /opt/tomcat/bin/setenv.sh"
 +    - sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST "sudo sh -c 'echo export CI_APP_KEY=$CI_APP_KEY >> /opt/tomcat/bin/setenv.sh'"
 +    - sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST sudo systemctl restart tomcat.service
 +</sxh>
 +
 +==== Utilisation de variable d'environnement ====
 +Pour utiliser la variable d'environnement **CI_APP_KEY** dans le projet SpringBoot :
 +
 +=== application.properties ===
 +Ajouter la ligne suivante à **application.properties** :
 +<sxh ini;gutter:false>
 +spring.data.encryption.key=${CI_APP_KEY}
 +</sxh>
 +
 +=== Utilisation en java ===
 +Dans un contrôleur, un service ou autre :
 +<sxh java ;classe java;gutter:false>
 +    @Value("${spring.data.encryption.key}")
 +    private String KEY;
 +</sxh>
 +
  
  
  • web/framework/spring/deployment.1701219991.txt.gz
  • Dernière modification : il y a 17 mois
  • de jcheron