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/25 14:21] jcheronweb:framework:spring:deployment [2023/12/17 17:41] (Version actuelle) – [Configuration Tomcat sur VM] jcheron
Ligne 5: Ligne 5:
 ==== Configuration VM ==== ==== Configuration VM ====
 Se connecter en root avec su : Se connecter en root avec su :
-<code bash;gutter:false>+<sxh bash;gutter:false>
 su -l su -l
-</code>+</sxh>
 === Installer Java === === Installer Java ===
 Choisir une version compatible (supérieure ou égale à la verion java déclarée dans le **pom.xml**): Choisir une version compatible (supérieure ou égale à la verion java déclarée dans le **pom.xml**):
  
-<code bash;gutter:false>+<sxh bash;gutter:false>
 cd /tmp cd /tmp
 wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.deb wget https://download.oracle.com/java/17/latest/jdk-17_linux-x64_bin.deb
 dpkg -i jdk-17_linux-x64_bin.deb dpkg -i jdk-17_linux-x64_bin.deb
 java -version java -version
-</code>+</sxh>
  
 === Installer Tomcat 10 === === Installer Tomcat 10 ===
 Ajouter un utilisateur **tomcat** : Ajouter un utilisateur **tomcat** :
  
-<code bash;gutter:false>+<sxh bash;gutter:false>
 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 :
-<code bash;gutter:false>+<sxh bash;gutter:false>
 cd /tmp cd /tmp
 wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.16/bin/apache-tomcat-10.1.16.tar.gz wget https://dlcdn.apache.org/tomcat/tomcat-10/v10.1.16/bin/apache-tomcat-10.1.16.tar.gz
 sudo tar xzvf apache-tomcat-10*tar.gz -C /opt/tomcat --strip-components=1  sudo tar xzvf apache-tomcat-10*tar.gz -C /opt/tomcat --strip-components=1 
-</code>+</sxh>
  
 Définir les permissions sur les fichiers : Définir les permissions sur les fichiers :
-<code bash;gutter:false>+<sxh bash;gutter:false>
 chown -R tomcat:tomcat /opt/tomcat/  chown -R tomcat:tomcat /opt/tomcat/ 
 chmod -R u+x /opt/tomcat/bin  chmod -R u+x /opt/tomcat/bin 
-</code>+</sxh>
  
 Créer le tomcat-manager pour manager les sites à distance via l'interface web : Créer le tomcat-manager pour manager les sites à distance via l'interface web :
  
-<code bash;gutter:false>+<sxh bash;gutter:false>
 nano /opt/tomcat/conf/tomcat-users.xml nano /opt/tomcat/conf/tomcat-users.xml
-</code>+</sxh>
  
-<code bash;gutter:false;title:/opt/tomcat/conf/tomcat-users.xml>+<sxh bash;gutter:false;title:/opt/tomcat/conf/tomcat-users.xml>
 <!-- user manager can access only manager section --> <!-- user manager can access only manager section -->
 <role rolename="manager-gui" /> <role rolename="manager-gui" />
Ligne 52: Ligne 52:
 <role rolename="admin-gui" /> <role rolename="admin-gui" />
 <user username="admin" password="_SECRET_PASSWORD_" roles="manager-gui,admin-gui" /> <user username="admin" password="_SECRET_PASSWORD_" roles="manager-gui,admin-gui" />
-</code>+</sxh>
  
 === Créer un service Tomcat === === Créer un service Tomcat ===
 Pour gérer tomcat plus facilement, créer un service Tomcat : Pour gérer tomcat plus facilement, créer un service Tomcat :
-<code bash;gutter:false>+<sxh bash;gutter:false>
 nano /etc/systemd/system/tomcat.service nano /etc/systemd/system/tomcat.service
-</code>+</sxh>
  
-<code bash;gutter:false>+<sxh bash;gutter:false>
 [Unit] [Unit]
 Description=Tomcat Description=Tomcat
Ligne 83: Ligne 83:
 [Install] [Install]
 WantedBy=multi-user.target WantedBy=multi-user.target
-</code>+</sxh>
  
 Recharger le deamon service pour prendre en compte le nouveau service : Recharger le deamon service pour prendre en compte le nouveau service :
-<code bash;gutter:false>+<sxh bash;gutter:false>
 systemctl daemon-reload systemctl daemon-reload
-</code>+</sxh>
  
 Démarrer Tomcat : Démarrer Tomcat :
-<code bash;gutter:false>+<sxh bash;gutter:false>
 systemctl start tomcat.service systemctl start tomcat.service
-</code>+</sxh>
  
 Activer Tomcat pour qu'il redémarre automatiquement à chaque reboot : Activer Tomcat pour qu'il redémarre automatiquement à chaque reboot :
-<code bash;gutter:false>+<sxh bash;gutter:false>
 systemctl enable tomcat.service systemctl enable tomcat.service
-</code>+</sxh>
  
 Afficher son statut : Afficher son statut :
-<code bash;gutter:false>+<sxh bash;gutter:false>
 systemctl status tomcat.service systemctl status tomcat.service
-</code>+</sxh>
  
 {{:web:framework:spring:pasted:20231121-113943.png}} {{:web:framework:spring:pasted:20231121-113943.png}}
Ligne 111: Ligne 111:
   * <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 &quot;%r&quot; %s %b" />
 +        </Host>
 +...
 +</Engine>
 +
 +</sxh>
  
 ==== Mise en place CI/CD ==== ==== Mise en place CI/CD ====
Ligne 122: Ligne 139:
   * Redémarrer SSH   * Redémarrer SSH
   * Faire en sorte qu'il n'ait pas besoin de saisir un password en utilisant **sudo** avec les commandes **mv**, **cp**, **systemctl**   * Faire en sorte qu'il n'ait pas besoin de saisir un password en utilisant **sudo** avec les commandes **mv**, **cp**, **systemctl**
-<code bash;gutter:false>+<sxh bash;gutter:false>
 adduser --quiet --shell $SHELL --disabled-password --gecos 'GitlabCI User' gitlab-ci adduser --quiet --shell $SHELL --disabled-password --gecos 'GitlabCI User' gitlab-ci
 usermod -a -G sudo gitlab-ci usermod -a -G sudo gitlab-ci
Ligne 129: Ligne 146:
 systemctl restart sshd systemctl restart sshd
 echo 'gitlab-ci ALL=(ALL) NOPASSWD: /bin/mv, NOPASSWD: /usr/bin/systemctl, NOPASSWD: /bin/cp' | sudo EDITOR='tee -a' visudo echo 'gitlab-ci ALL=(ALL) NOPASSWD: /bin/mv, NOPASSWD: /usr/bin/systemctl, NOPASSWD: /bin/cp' | sudo EDITOR='tee -a' visudo
-</code>+</sxh>
  
 === Variable CI/CD sur Gitlab === === Variable CI/CD sur Gitlab ===
Ligne 141: Ligne 158:
 === Configuration du projet Springboot === === Configuration du projet Springboot ===
 Modifier **pom.xml** pour définir le nom du fichier WAR déployé : Modifier **pom.xml** pour définir le nom du fichier WAR déployé :
-<code xml;title:pom.xml>+<sxh xml;title:pom.xml>
     <build>     <build>
         <finalName>ssh-deploy</finalName>         <finalName>ssh-deploy</finalName>
     </build>     </build>
-</code>+</sxh>
  
 Vérifiez que le déploiement sera bien fait en WAR : Vérifiez que le déploiement sera bien fait en WAR :
-<code xml;title:pom.xml>+<sxh xml;title:pom.xml>
     <packaging>war</packaging>     <packaging>war</packaging>
-</code>+</sxh>
  
 === Configuration Gitlab CI/CD === === Configuration Gitlab CI/CD ===
Ligne 156: Ligne 173:
 Créer ou modifier le fichier **.gitlab-ci.yml** : Créer ou modifier le fichier **.gitlab-ci.yml** :
  
-<code yml;title:.gitlab-ci.yml>+<sxh yml;title:.gitlab-ci.yml>
 stages: stages:
   - build   - build
Ligne 188: Ligne 205:
     - 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
  
-</code>+</sxh>
  
 <WRAP round info> <WRAP round info>
Ligne 208: Ligne 225:
  
 Ajouter la section **profiles** suivante au fichier **pom.xml** Ajouter la section **profiles** suivante au fichier **pom.xml**
-<code xml;title:pom.xml>+<sxh xml;title:pom.xml>
     <profiles>     <profiles>
         <profile>         <profile>
Ligne 232: Ligne 249:
         </profile>         </profile>
     </profiles>     </profiles>
-</code>+</sxh>
  
 ==== Configurations ==== ==== Configurations ====
Ligne 240: Ligne 257:
 === Configuration générale : application.properties === === Configuration générale : application.properties ===
  
-<code bash;application.properties>+<sxh bash;application.properties>
 spring.profiles.active=@activeProfile@ spring.profiles.active=@activeProfile@
  
Ligne 258: 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
-</code>+ 
 +servlet.context.path=/ 
 + 
 +</sxh>
  
 === Configuration spécifique au dev === === Configuration spécifique au dev ===
  
-<code bash;application-dev.properties>+<sxh bash;application-dev.properties>
 spring.datasource.url=jdbc:h2:file:./data/paris-2024;DB_CLOSE_ON_EXIT=FALSE spring.datasource.url=jdbc:h2:file:./data/paris-2024;DB_CLOSE_ON_EXIT=FALSE
 spring.jpa.show-sql=true spring.jpa.show-sql=true
 spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.format_sql=true
- +</sxh>
-servlet.context.path=/ +
-</code>+
  
 === Configuration spécifique à la prod === === Configuration spécifique à la prod ===
Ligne 279: Ligne 297:
 </WRAP> </WRAP>
  
-<code 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>
-</code>+
  
 === Configuration spécifique aux tests === === Configuration spécifique aux tests ===
  
-<code bash;application-test.properties>+<sxh bash;application-test.properties>
 spring.datasource.url=jdbc:h2:file:/home/runner/work/myparis/paris-2024/target/data;DB_CLOSE_ON_EXIT=FALSE spring.datasource.url=jdbc:h2:file:/home/runner/work/myparis/paris-2024/target/data;DB_CLOSE_ON_EXIT=FALSE
 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=/ +server.servlet.context-path=/ 
-</code>+</sxh>
  
 === Prise en compte dans .gitlab-ci.yml === === Prise en compte dans .gitlab-ci.yml ===
  
 Génération du package en production Génération du package en production
-<code bash>+<sxh bash>
   stage: build   stage: build
   script: "mvn clean package -P prod -DskipTests=true"   script: "mvn clean package -P prod -DskipTests=true"
-</code>+</sxh>
  
 Génération du package pour les tests Génération du package pour les tests
-<code bash>+<sxh bash>
   stage: test   stage: test
   script: "mvn --batch-mode --update-snapshots verify -P test -DskipTests=false"   script: "mvn --batch-mode --update-snapshots verify -P test -DskipTests=false"
-</code>+</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.1700918487.txt.gz
  • Dernière modification : il y a 17 mois
  • de jcheron