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:21] – jcheron | web: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; | + | <sxh bash; |
| 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; | + | <sxh bash; |
| cd /tmp | cd /tmp | ||
| wget https:// | wget https:// | ||
| 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; | + | <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 : | ||
| - | <code bash; | + | <sxh bash; |
| cd /tmp | cd /tmp | ||
| wget https:// | wget https:// | ||
| 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; | + | <sxh bash; |
| chown -R tomcat: | chown -R tomcat: | ||
| chmod -R u+x / | chmod -R u+x / | ||
| - | </code> | + | </sxh> |
| Créer le tomcat-manager pour manager les sites à distance via l' | Créer le tomcat-manager pour manager les sites à distance via l' | ||
| - | <code bash; | + | <sxh bash; |
| nano / | nano / | ||
| - | </code> | + | </sxh> |
| - | <code bash; | + | <sxh bash; |
| <!-- user manager can access only manager section --> | <!-- user manager can access only manager section --> | ||
| <role rolename=" | <role rolename=" | ||
| Ligne 52: | Ligne 52: | ||
| <role rolename=" | <role rolename=" | ||
| <user username=" | <user username=" | ||
| - | </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; | + | <sxh bash; |
| nano / | nano / | ||
| - | </code> | + | </sxh> |
| - | <code bash; | + | <sxh bash; |
| [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; | + | <sxh bash; |
| systemctl daemon-reload | systemctl daemon-reload | ||
| - | </code> | + | </sxh> |
| Démarrer Tomcat : | Démarrer Tomcat : | ||
| - | <code bash; | + | <sxh bash; |
| 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; | + | <sxh bash; |
| systemctl enable tomcat.service | systemctl enable tomcat.service | ||
| - | </code> | + | </sxh> |
| Afficher son statut : | Afficher son statut : | ||
| - | <code bash; | + | <sxh bash; |
| systemctl status tomcat.service | systemctl status tomcat.service | ||
| - | </code> | + | </sxh> |
| {{: | {{: | ||
| 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 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; | + | <sxh bash; |
| adduser --quiet --shell $SHELL --disabled-password --gecos ' | adduser --quiet --shell $SHELL --disabled-password --gecos ' | ||
| usermod -a -G sudo gitlab-ci | usermod -a -G sudo gitlab-ci | ||
| Ligne 129: | Ligne 146: | ||
| systemctl restart sshd | systemctl restart sshd | ||
| echo ' | echo ' | ||
| - | </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; | + | <sxh xml; |
| < | < | ||
| < | < | ||
| </ | </ | ||
| - | </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; | + | <sxh xml; |
| < | < | ||
| - | </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; | + | <sxh yml; |
| stages: | stages: | ||
| - build | - build | ||
| 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 | ||
| - | </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; | + | <sxh xml; |
| < | < | ||
| < | < | ||
| Ligne 232: | Ligne 249: | ||
| </ | </ | ||
| </ | </ | ||
| - | </code> | + | </sxh> |
| ==== Configurations ==== | ==== Configurations ==== | ||
| Ligne 240: | Ligne 257: | ||
| === Configuration générale : application.properties === | === Configuration générale : application.properties === | ||
| - | <code bash; | + | <sxh bash; |
| 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=/ | spring.h2.console.path=/ | ||
| - | </code> | + | |
| + | servlet.context.path=/ | ||
| + | |||
| + | </sxh> | ||
| === Configuration spécifique au dev === | === Configuration spécifique au dev === | ||
| - | <code bash; | + | <sxh bash; |
| spring.datasource.url=jdbc: | spring.datasource.url=jdbc: | ||
| 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: | ||
| </ | </ | ||
| - | <code 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=/ | + | </sxh> |
| - | </code> | + | |
| === Configuration spécifique aux tests === | === Configuration spécifique aux tests === | ||
| - | <code bash; | + | <sxh bash; |
| spring.datasource.url=jdbc: | spring.datasource.url=jdbc: | ||
| 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" | ||
| - | </ | + | </sxh> |
| + | |||
| + | ===== 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; | ||
| + | </sxh> | ||