| 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/24 02:04] – [Configurations] jcheron | web:framework:spring:deployment [2023/12/17 17:41] (Version actuelle) – [Configuration Tomcat sur VM] jcheron |
|---|
| </sxh> | </sxh> |
| |
| Rechargr le deamon service pour prendre en compte le nouveau service : | Recharger le deamon service pour prendre en compte le nouveau service : |
| <sxh bash;gutter:false> | <sxh bash;gutter:false> |
| systemctl daemon-reload | systemctl daemon-reload |
| * <nowiki>http[s]://srv2-vm-[number].sts-sio-caen.info/manager/</nowiki> | * <nowiki>http[s]://srv2-vm-[number].sts-sio-caen.info/manager/</nowiki> |
| |
| | ==== VHost Tomcat ==== |
| | Créer un VHOST Tomcat correspondant à l'adresse DNS de votre VM : |
| | |
| | A ajouter dans **/opt/tomcat/conf/server.xml** |
| | <sxh xml;title:> |
| | <Engine> |
| | ... |
| | <Host name="srv2-vm-xxx.sts-sio-caen.info" appBase="webapps/paris-2024" |
| | unpackWARs="true" autoDeploy="true"> |
| | <Valve className="org.apache.catalina.valves.AccessLogValve" directory="logs" |
| | prefix="xxx_access_log" suffix=".txt" |
| | pattern="%h %l %u %t "%r" %s %b" /> |
| | </Host> |
| | ... |
| | </Engine> |
| | |
| | </sxh> |
| |
| ==== Mise en place CI/CD ==== | ==== Mise en place CI/CD ==== |
| - sshpass -e scp -o StrictHostKeyChecking=no -P $PORT target/$WAR $USER@$HOST:/home/$USER | - 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 echo $PATH |
| - sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST sudo mv /home/$USER/$WAR /opt/tomcat/webapps | - sshpass -e ssh -tt -o StrictHostKeyChecking=no -p $PORT $USER@$HOST sudo mv /home/$USER/$WAR /opt/tomcat/webapps/paris-2024/ROOT.war |
| - 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 |
| |
| 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> |
| |
| 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> |
| |
| |
| <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 |
| |
| servlet.context.path=/paris-2024 | |
| </sxh> | </sxh> |
| |
| spring.jpa.properties.hibernate.format_sql=true | spring.jpa.properties.hibernate.format_sql=true |
| |
| servlet.context.path=/ | server.servlet.context-path=/ |
| </sxh> | </sxh> |
| |
| Génération du package pour les tests | Génération du package pour les tests |
| <sxh bash> | <sxh bash> |
| stage: build | stage: test |
| 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> |
| | |
| |
| |