Déployer en production un projet est une étape cruciale dans le cycle de développement d’une application. Il est important de s’assurer que le code déployé est fiable et fonctionnel. L’utilisation d’un dépôt git, conjointement à des hooks, peut faciliter ce processus en automatisant certaines tâches et en garantissant que seul le code validé soit déployé en production. Cet article expliquera comment mettre en place ce workflow de déploiement en utilisant des hooks git pour automatiser le processus de déploiement en production.
Créer un dépôt « nu »
git init --bare PROJECT-NAME.git
Créer un « hook »
cd BARE-REPOSITORY-FOLDER/PROJECT-NAME.git/hooks nano post-receive
#!/bin/bash
TARGET="DEPLOY-FOLDER"
GIT_DIR="BARE-REPOSITORY-FOLDER/PROJECT-NAME.git"
BRANCH1="BRANCH-TO-DEPLOY"
while read oldrev newrev ref
do
if [[ $ref = refs/heads/$BRANCH1 ]];
then
echo "Ref $ref received. Deploying ${BRANCH1} branch to preprod..."
git --work-tree=$TARGET --git-dir=$GIT_DIR checkout $BRANCH1 -f
chown www-data:www-data -R $TARGET
else
echo "Ref $ref received. Doing nothing: only the ${BRANCH1} branch may be deployed on this server."
fi
done
A remplacer:
DEPLOY-FOLDER: dossier de déploiement (ex: /var/www)
BARE-REPOSITORY-FOLDER: dossier du dépot nu (ex: /home/deploy)
PROJECT-NAME: nom du projet (ex: project)
BRANCH-TO-DEPLOY: la branch de déploiement (en prod par exemple: master)Ajoute un dépôt externe
cd WORKING-DIRECTORY git remote add REMOTE-SERVER DISTANT-USER@SERVER_HOST:BARE-REPOSITORY-FOLDER/PROJECT-NAME.git WORKING-DIRECTORY: le dossier dans lequel vous développez et avez un dépot GIT en cours REMOTE-SERVER: le serveur distant, par exemple “production” ou “preprod”
Premier déploiement
git push REMOTE-SERVER BRANCH-TO-DEPLOY