Skip to content

Instantly share code, notes, and snippets.

@lfalmeida
Last active October 21, 2023 17:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lfalmeida/4e2f68145e3eef95dc76b9a72b9c2bd1 to your computer and use it in GitHub Desktop.
Save lfalmeida/4e2f68145e3eef95dc76b9a72b9c2bd1 to your computer and use it in GitHub Desktop.
Hook post receive com push options
#!/bin/sh
# ---- Config
ENV_NAME="Homologação"
TAG_SUFFIX="homolog"
PROJECT_NAME="Scania Journey Event"
PROJECT_PATH_HOST="/home/lfalmeida/Projects/GitHooks/prod/"
PROJECT_PATH_DOCKER="/src/projeto"
SLACK_WEBHOOK=""
ENABLE_SLACK_POST=0
# ---- helpers
printSuccessMessage () {
echo -e "\x1b[1;32m✔ $@\e[0m"
}
printInfoMessage () {
echo -e "\x1b[1;34m• $@\e[0m"
}
printWarningMessage() {
echo -e "\x1b[1;33m* $@\e[0m"
}
printDangerMessage() {
echo -e "\x1b[1;31m✖ $@\e[0m"
}
printBulletMessage() {
echo "• $@"
}
printOutputMessage() {
echo -e "\x1b[1;36m\n${OUTPUT_MESSAGE//'*'/}\n\e[0m"
if [[ "$ENABLE_SLACK_POST" = 1 ]]; then
payload='{"text": "'${OUTPUT_MESSAGE}'", "mrkdwn": true}'
curl -s -X POST -H 'Content-type: application/json' --data "${payload}" ${SLACK_WEBHOOK} > /dev/null
fi
}
printHeader () {
echo ""
printInfoMessage "-------------------------------------------------"
printInfoMessage "Checkout realizado para a tag: ${LATEST_TAG}"
printInfoMessage "-------------------------------------------------"
echo ""
}
# ---- tarefas no projeto
runComposer() {
printInfoMessage "executando composer..."
mkdir -p $HOME/.composer/cache;
tty=
tty -s && tty=--tty
result=$(docker run \
$tty \
--interactive \
--rm \
-u $(id -u):$(id -g) \
-v /etc/passwd:/etc/passwd:ro \
-v /etc/group:/etc/group:ro \
-v $HOME/.composer:/tmp \
-v ~/.ssh:/home/$(whoami)/.ssh \
-v ~/.ssh/:/etc/ssh/ \
-v $PROJECT_PATH_HOST:/app \
composer update --ignore-platform-reqs --prefer-dist > /dev/null)
rc=$?;
if [[ $rc = 0 ]]; then
MESSAGE="Pacotes atualizdos com sucesso." ;
printSuccessMessage "Ok"
echo ''
else
MESSAGE="Ocorreram problemas." ;
printDangerMessage "erro"
echo ''
fi
OUTPUT_MESSAGE="$OUTPUT_MESSAGE *Composer*: ${MESSAGE}\n"
}
runMigrations() {
printInfoMessage "executando migrations..."
#docker exec -i php /src/pi_johnson/bin/cake migrations migrate
# limpa o cache das models
#printBulletMessage "executando migrations..."
#rm -Rf /var/www/src/pi_johnson/tmp/cache/models/*
rc=$?;
if [[ $rc = 0 ]]; then
MESSAGE="Executadas com sucesso." ;
printSuccessMessage "Ok"
echo ''
else
MESSAGE="Ocorreram problemas." ;
printDangerMessage "erro"
echo ''
fi
OUTPUT_MESSAGE="$OUTPUT_MESSAGE *Migrations*: ${MESSAGE}\n"
}
runNpmInstall() {
printInfoMessage "executando npm install..."
tty=
tty -s && tty=--tty
docker run \
$tty \
--interactive \
--rm \
--name npm-cmd \
-v $PROJECT_PATH_HOST:/app \
lfalmeida/gulp \
yarn > /dev/null
rc=$?;
if [[ $rc = 0 ]]; then
MESSAGE="Atualizados com sucesso." ;
printSuccessMessage "Ok"
echo ''
else
MESSAGE="Ocorreram problemas ao atualizar." ;
printDangerMessage "erro"
echo ''
fi
OUTPUT_MESSAGE="$OUTPUT_MESSAGE *Pacotes Nodejs*: ${MESSAGE}\n"
}
runFrontendBuild() {
printInfoMessage "executando frontend build..."
tty=
tty -s && tty=--tty
result=$(docker run \
$tty \
--interactive \
--rm \
--name npm-cmd \
-v $PROJECT_PATH_HOST:/app \
lfalmeida/gulp \
yarn build > /dev/null)
rc=$?;
if [[ $rc = 0 ]]; then
MESSAGE="Executado com sucesso." ;
printSuccessMessage "Ok"
echo ''
else
MESSAGE="Ocorreram problemas." ;
printDangerMessage "erro"
echo ''
fi
OUTPUT_MESSAGE="$OUTPUT_MESSAGE *Build frontend*: ${MESSAGE}\n"
}
# ---- git functions
processPushOptions() {
runComposer=0
runMigrations=0
runNpmInstall=0
runFrontendBuild=0
if test -n "$GIT_PUSH_OPTION_COUNT"
then
i=0
while test "$i" -lt "$GIT_PUSH_OPTION_COUNT"
do
eval "value=\$GIT_PUSH_OPTION_$i"
case "$value" in
run-composer) runComposer=1;;
run-migrations) runMigrations=1;;
run-npm-install) runNpmInstall=1;;
run-frontend-build) runFrontendBuild=1;;
*) printDangerMessage "push option não suportada: ${value}" >&2
esac
i=$((i + 1))
done
fi
if [[ "$runComposer" = 1 ]]; then
runComposer
fi
if [[ "$runMigrations" = 1 ]]; then
runMigrations
fi
if [[ "$runNpmInstall" = 1 ]];
then runNpmInstall
fi
if [[ "$runFrontendBuild" = 1 ]]; then
runFrontendBuild
fi
}
TOTAL_TAGS_RECEIVED=0
countTagsReceived () {
while read oldrev newrev ref
do
if [[ $ref = *"refs/tags/"* && $(echo $ref | cut -d'/' -f 3) = *"-${TAG_SUFFIX}" ]]; then
TOTAL_TAGS_RECEIVED=$((TOTAL_TAGS_RECEIVED + 1))
fi
done
}
getLatestTag() {
echo $(git describe --tags --match "*-${TAG_SUFFIX}" --abbrev=0 $(git rev-list --tags --max-count=1))
}
#inicializando com não zero, indicando que o checkout ainda não foi realizado.
CHECKOUT_STATUS_CODE=-1
checkoutLatestTag() {
LATEST_TAG=$(getLatestTag)
GIT_WORK_TREE=${PROJECT_PATH_HOST} git checkout -f ${LATEST_TAG} > /dev/null
CHECKOUT_STATUS_CODE=$?
}
main() {
countTagsReceived
if [[ "$TOTAL_TAGS_RECEIVED" -gt 0 ]]; then
checkoutLatestTag
if [[ "${CHECKOUT_STATUS_CODE}" = 0 ]]; then
OUTPUT_MESSAGE="*Deploy realizado no projeto ${PROJECT_NAME}*\n"
OUTPUT_MESSAGE="$OUTPUT_MESSAGE *Ambiente*: ${ENV_NAME}\n"
OUTPUT_MESSAGE="$OUTPUT_MESSAGE *Tag*: ${LATEST_TAG}\n"
printHeader
processPushOptions
printOutputMessage
else
printDangerMessage "Não foi possível fazer o checkout para a tag: ${LATEST_TAG}"
printDangerMessage "Erro: ${CHECKOUT_STATUS_CODE}"
fi
else
echo -e "\n"
printWarningMessage "Ambiente: ${ENV_NAME}"
printWarningMessage "Neste ambiente são publicadas apenas tags com o sufixo: ${TAG_SUFFIX}"
printWarningMessage "O push foi recebido. Nenhuma outra ação foi tomada."
echo -e "\n"
fi
}
# executa a função principal ---------------
main
# ---------------
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment