Différences
Ci-dessous, les différences entre deux révisions de la page.
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/25 14:19] – jcheron | web:framework:spring:deployment [2023/12/17 17:41] (Version actuelle) – [Configuration Tomcat sur VM] jcheron | ||
---|---|---|---|
Ligne 21: | Ligne 21: | ||
Ajouter un utilisateur **tomcat** : | Ajouter un utilisateur **tomcat** : | ||
- | <code bash; | + | <sxh bash; |
useradd -m -d /opt/tomcat -U -s /bin/false tomcat | useradd -m -d /opt/tomcat -U -s /bin/false tomcat | ||
- | </code> | + | </sxh> |
Télécharger et dézipper Tomcat : | Télécharger et dézipper Tomcat : | ||
Ligne 111: | Ligne 111: | ||
* < | * < | ||
+ | ==== VHost Tomcat ==== | ||
+ | Créer un VHOST Tomcat correspondant à l' | ||
+ | |||
+ | A ajouter dans **/ | ||
+ | <sxh xml; | ||
+ | < | ||
+ | ... | ||
+ | <Host name=" | ||
+ | unpackWARs=" | ||
+ | <Valve className=" | ||
+ | prefix=" | ||
+ | pattern=" | ||
+ | </ | ||
+ | ... | ||
+ | </ | ||
+ | |||
+ | </ | ||
==== Mise en place CI/CD ==== | ==== Mise en place CI/CD ==== | ||
Ligne 188: | Ligne 205: | ||
- sshpass -e scp -o StrictHostKeyChecking=no -P $PORT target/$WAR $USER@$HOST:/ | - sshpass -e scp -o StrictHostKeyChecking=no -P $PORT target/$WAR $USER@$HOST:/ | ||
- sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST echo $PATH | - 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 / | + | - sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST sudo mv / |
- sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST sudo systemctl restart tomcat.service | - sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST sudo systemctl restart tomcat.service | ||
Ligne 258: | Ligne 275: | ||
spring.h2.console.settings.web-allow-others=true | spring.h2.console.settings.web-allow-others=true | ||
spring.h2.console.path=/ | spring.h2.console.path=/ | ||
+ | |||
+ | servlet.context.path=/ | ||
+ | |||
</ | </ | ||
Ligne 266: | 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=/ | ||
</ | </ | ||
Ligne 280: | Ligne 298: | ||
<sxh bash; | <sxh bash; | ||
- | spring.datasource.url=jdbc: | + | spring.datasource.url=jdbc: |
spring.jpa.show-sql=false | spring.jpa.show-sql=false | ||
spring.jpa.properties.hibernate.format_sql=false | spring.jpa.properties.hibernate.format_sql=false | ||
- | servlet.context.path=/ | ||
</ | </ | ||
Ligne 294: | Ligne 311: | ||
spring.jpa.properties.hibernate.format_sql=true | spring.jpa.properties.hibernate.format_sql=true | ||
- | servlet.context.path=/ | + | server.servlet.context-path=/ |
</ | </ | ||
Ligne 310: | Ligne 327: | ||
script: "mvn --batch-mode --update-snapshots verify -P test -DskipTests=false" | script: "mvn --batch-mode --update-snapshots verify -P test -DskipTests=false" | ||
</ | </ | ||
+ | |||
+ | ===== Déploiement avec variables ===== | ||
+ | Il est parfois indispensable, | ||
+ | * 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' | ||
+ | |||
+ | <sxh bash> | ||
+ | |||
+ | [Unit] | ||
+ | Description=Tomcat | ||
+ | After=network.target | ||
+ | |||
+ | [Service] | ||
+ | Type=simple | ||
+ | |||
+ | User=tomcat | ||
+ | Group=tomcat | ||
+ | |||
+ | Environment=" | ||
+ | Environment=" | ||
+ | Environment=" | ||
+ | Environment=" | ||
+ | Environment=" | ||
+ | Environment=" | ||
+ | |||
+ | ExecStart=/ | ||
+ | ExecStop=/ | ||
+ | |||
+ | [Install] | ||
+ | WantedBy=multi-user.target | ||
+ | |||
+ | </ | ||
+ | |||
+ | Recharger le service et redémarer le : | ||
+ | <sxh bash; | ||
+ | systemctl daemon-reload | ||
+ | systemctl start tomcat.service | ||
+ | </ | ||
+ | |||
+ | |||
+ | |||
+ | ==== 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' | ||
+ | |||
+ | <sxh yml; | ||
+ | stages: | ||
+ | - build | ||
+ | - deploy | ||
+ | |||
+ | maven-build: | ||
+ | image: maven: | ||
+ | stage: build | ||
+ | script: "mvn clean package -P prod -DskipTests=true" | ||
+ | artifacts: | ||
+ | paths: | ||
+ | - target/ | ||
+ | |||
+ | deploy-master: | ||
+ | variables: | ||
+ | HOST: " | ||
+ | PORT: " | ||
+ | USER: " | ||
+ | WAR: " | ||
+ | SITE_LOCATION: | ||
+ | rules: | ||
+ | - if: ' | ||
+ | before_script: | ||
+ | - apt-get update -qq && apt-get install -y -qq sshpass sudo | ||
+ | - echo "Host= $HOST" | ||
+ | stage: deploy | ||
+ | script: | ||
+ | - sudo whoami | ||
+ | - which mv | ||
+ | - sshpass -V | ||
+ | - export SSHPASS=$CI_USER_PASS | ||
+ | - sshpass -e scp -o StrictHostKeyChecking=no -P $PORT target/$WAR $USER@$HOST:/ | ||
+ | - 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 / | ||
+ | - sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST "sudo chmod 755 / | ||
+ | - sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST "sudo sh -c 'echo export CI_APP_KEY=$CI_APP_KEY >> / | ||
+ | - sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST sudo systemctl restart tomcat.service | ||
+ | </ | ||
+ | |||
+ | ==== Utilisation de variable d' | ||
+ | Pour utiliser la variable d' | ||
+ | |||
+ | === application.properties === | ||
+ | Ajouter la ligne suivante à **application.properties** : | ||
+ | <sxh ini; | ||
+ | spring.data.encryption.key=${CI_APP_KEY} | ||
+ | </ | ||
+ | |||
+ | === Utilisation en java === | ||
+ | Dans un contrôleur, | ||
+ | <sxh java ;classe java; | ||
+ | @Value(" | ||
+ | private String KEY; | ||
+ | </ | ||
+ | |||