Good day,

I have two clones on:

  • local machine - local
  • remote machine A - RM-A

and git repository on remote machine B - RM-B

How to register a hook on RM-B , so that it connects via ssh to RM-A and does a pull, after pushing through with local ?

I have registered such a post-receive hook, but the script comes out with an error and with manual git pull on RM-A system is updated to the latest version, although, if the script went well, it should have indicated that the clone is already detected.

 #!/bin/bash IP="$(ip addr show eth0 | grep 'inet ' | cut -f2 | awk '{ print $2}')" LOG_FILE=/var/log/git.post-receive.log export DEPLOY_APP_NAME=APP export GIT_DIR="$(cd $(dirname $(dirname $0));pwd)" REMOTE_USERNAME="user" REMOTE_HOSTS="host.example.tld" echo "$(date): ${IP} tries to change '$(hostname -f)'" >> ${LOG_FILE} BRANCHES=() BRANCHES+=('alpha') BRANCHES+=('beta') BRANCHES+=('stable') while read oldrev newrev refname do export DEPLOY_BRANCH=$(git rev-parse --symbolic --abbrev-ref $refname) export DEPLOY_OLDREV="$oldrev" export DEPLOY_NEWREV="$newrev" export DEPLOY_REFNAME="$refname" export DEPLOY_ROOT="/opt/${DEPLOY_APP_NAME}/${DEPLOY_BRANCH}/project/" # export GIT_WORK_TREE="${DEPLOY_ROOT}" for REMOTE_HOST in ${REMOTE_HOSTS}; do if [[ " ${BRANCHES[*]} " == *"${DEPLOY_BRANCH}"* ]]; then echo "$(date): Deploying ${DEPLOY_BRANCH} by ${IP}" >> ${LOG_FILE} ssh -o StrictHostKeyChecking=no -l ${REMOTE_USERNAME} ${REMOTE_HOST} 'cd ${DEPLOY_ROOT} && git branch "${DEPLOY_BRANCH}"' || echo "$(date): Custom error output" ssh -o StrictHostKeyChecking=no -l ${REMOTE_USERNAME} ${REMOTE_HOST} 'cd ${DEPLOY_ROOT} && git checkout -f "${DEPLOY_BRANCH}"' || echo "$(date): Custom error output" ssh -o StrictHostKeyChecking=no -l ${REMOTE_USERNAME} ${REMOTE_HOST} 'cd ${DEPLOY_ROOT} && git branch --set-upstream-to=origin/${DEPLOY_BRANCH} ${DEPLOY_BRANCH}' ssh -o StrictHostKeyChecking=no -l ${REMOTE_USERNAME} ${REMOTE_HOST} 'cd ${DEPLOY_ROOT} && git pull' ssh -o StrictHostKeyChecking=no -l ${REMOTE_USERNAME} ${REMOTE_HOST} 'cd ${DEPLOY_ROOT} && git reset --hard "${DEPLOY_NEWREV}"' || echo "$(date): Custom error output" && exit 1 echo "$(date): Deployed safely" >> ${LOG_FILE} fi done done exit 0 

If I write to the shell command

 ssh -o StrictHostKeyChecking=no -l ${REMOTE_USERNAME} ${REMOTE_HOST} 'cd ${DEPLOY_ROOT} && git reset --hard "${DEPLOY_NEWREV}"' 

then I get the error - fatal: Could not parse object 'e88056..12d964' ,
but if I write

 ssh -o StrictHostKeyChecking=no -l ${REMOTE_USERNAME} ${REMOTE_HOST} \ 'cd ${DEPLOY_ROOT} && git pull' 

and then

 ssh -o StrictHostKeyChecking=no -l ${REMOTE_USERNAME} ${REMOTE_HOST} \ 'cd ${DEPLOY_ROOT} && git reset --hard "${DEPLOY_NEWREV}"' 

then everything works, but not through the script

The example used with github thomasfr

  • But in general, this is far from the best deployment scheme (subjectively, yes). There are a number of reasons for not having a repository where your application (site) is hosted. It is enough to copy it there using rsync. Read more in Setting Up and Deploying a Project Using Git - Nick Volynkin
  • скрипт выходит с ошибкой - with what? - Nick Volynkin
  • Nick, the error does not explicitly display, only that the output is non-zero, i.e. works ... || echo "$(date): Custom error output" && exit 1 ... || echo "$(date): Custom error output" && exit 1 and after logging in through ssh to the slave. the directory, making pull an update occurs, although it should display a message that the clone has already been updated. Maybe there is another way to do deploy. I still have a cron for php in my head that will do git pull - xoxn-- 1'w3k4n
  • The provided link solves the question if the repository and the site are on the same server, on the same machine. In my case, 3 different cars. Where remote servers with a site have access to a remote server with a repository via pub. key. - xoxn-- 1'w3k4n
  • the method is described there via rsync, it works just fine with different machines and ssh access. - Nick Volynkin

0