Skip to content

Instantly share code, notes, and snippets.

@juanpablocs
Last active February 15, 2017 23:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save juanpablocs/e1abe532c90bf143993eeb75ba217ba0 to your computer and use it in GitHub Desktop.
Save juanpablocs/e1abe532c90bf143993eeb75ba217ba0 to your computer and use it in GitHub Desktop.
Git hook post-receive, compile js files gulp or webpack etc and valid other files
#!/bin/bash
PROJECT="/home/nginx/domains/mywebiste/public"
PROJECT_STAGING="/home/nginx/domains/pre.mywebiste/public"
while read oldrev newrev ref
do
# guarda archivos modificados en variable
output_files="$(git diff-tree -r --name-only --no-commit-id $oldrev..$newrev)"
#comprueba la salida de los archivos modificados, si coincide con el primer param ejecuta el segundo param
check_run() {
echo "$output_files" | grep -E --quiet "$1" && eval "$2"
}
#valida la rama master
if [[ $ref =~ .*/master$ ]];
then
echo "Master recibido. Subiendo cambios a producción..."
git --work-tree=$PROJECT --git-dir=/home/nginx/repos/mywebiste.git checkout -f master
echo ""
echo "valid js or sass file and compile"
check_run ".js|.scss" "cd $PROJECT/frontend && npm run build:pro"
echo ""
echo "valid composer changed and install dependencies"
check_run "composer.json" "cd $PROJECT && composer install && composer update"
#valida rama staging
elif [[ $ref =~ .*/staging$ ]];
then
echo "Staging recibida, subiendo cambios al servidor de pruebas"
git --work-tree=$PROJECT_STAGING --git-dir=/home/nginx/repos/mywebiste.git checkout -f staging
echo "execute other commands"
else
echo "Rama $ref recibida. No hay reglas para está rama."
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment