Home > Software design >  Execute Bash Script After Git Post-Receive Hook executes
Execute Bash Script After Git Post-Receive Hook executes

Time:09-14

I have a bash script on my test server that will export my wordpress db, rsync the db to the prod server, and git push all of my files to prod sever.

Within the prod server's git repo I have a git post-receive hook correctly configured.

#!/bin/bash
#Receive Git Push from Test
git --work-tree=/home/username/public_html --git-dir=/home/username/public_html/git/production-site.git checkout -f

Within the working tree directory (WordPress directory) on the prod server I also have a bash script that will import the newly uploaded db. /home/username/public_html/db-import-script.sh

#!/bin/bash

#bunch of commands
...
...
...

Question:
How can I automatically execute the db import script immediately following a git push?

troubleshooting:
inside of post-receive, I have tried using an absolute paths to execute the script, no luck

#!/bin/bash
#Receive Git Push from Test
git --work-tree=/home/username/public_html --git-dir=/home/username/public_html/git/production-site.git checkout -f

#execute script with absolute path
/home/username/public_html/db-import-script.sh

db-import-script.sh does not execute. NOTE: this script must remain located in the Wordpress directory b/c it uses wp-cli commands for various actions.

any tips?

CodePudding user response:

I use e.g. gitea and on server one has to simply copy a script in post-receive.d/ folder. The post-receive hook (see below and you may use it as a template) will scan this folder and execute scripts in it.

#!/usr/bin/env bash
# AUTO GENERATED BY GITEA, DO NOT MODIFY
data=$(cat)
exitcodes=""
hookname=$(basename $0)
GIT_DIR=${GIT_DIR:-$(dirname $0)/..}

for hook in ${GIT_DIR}/hooks/${hookname}.d/*; do
  test -x "${hook}" && test -f "${hook}" || continue
  echo "${data}" | "${hook}"
  exitcodes="${exitcodes} $?"
done

for i in ${exitcodes}; do
  [ ${i} -eq 0 ] || exit ${i}
done

CodePudding user response:

kiss rule... (keep it simple stupid)

rather than spending days trying to learn sysdig well enough to trace a process that I have never previously heard of and it subprocesses. (no offence intended Charles, just need to actually get tasks done. Your bash debug-log snippet highly useful)

and rather than creating some git / gitea hybrid (no offence @m19v, I did try your solution, but didn't work)

knowing that production server db-import.sh worked properly and that my test server git push / db upload push.sh worked properly.

My final solution was to leave the production server's post-receive properly configured and to.... remotely execute my db-import.sh script via ssh directly within the directory in which it needs to be executed.

In a nutshell, I added this to the end of push.sh script on my test server:

#Remotely execute db import
ssh -p22 -i /home/username/.ssh/id_rsa [email protected]  'cd public_html && bash' << EOF
./db-import.sh
EOF

Bang problem solved...

  • Related