Applying project config changes with post-receive git hook I site I'm working on uses a post-receive git hook for deploying changes to the server.

At the moment, it looks like this:

#!/bin/sh

# default cpanel stuff
branch=$(/usr/local/cpanel/3rdparty/bin/git branch | awk '$1 == "*"{print $2}')
while read oldrev newrev ref
do
  if [ "x$ref" == "xrefs/heads/$branch" ]
  then
    echo "Recieved update on checked-out branch, queueing deployment."
    (cd .. ; /usr/bin/uapi VersionControlDeployment create repository_root=$PWD)
  fi

  branch=`echo $ref | cut -d/ -f3`

  cd ../
  # run from dir outside of .git
  # unset git dir because git ugh https://stackoverflow.com/a/10513684
  unset GIT_DIR

  if [ "master" == "$branch" ]; then
    # echo this is master
    # create new working tree in the desired directory
    git --work-tree=../prod-test checkout -f $branch
  fi

  if [ "develop" == "$branch" ]; then
    # echo this is develop
    git --work-tree=../staging checkout -f $branch
  fi

done &lt; /dev/stdin


That works fine. I push from my local and the changes get deployed to staging.

But I want project config and plugin update changes to be automatically applied rather than having to visit the CP and click the sync changes button.

I've seen elsewhere that the way to do that is to run

  ./craft project-config/sync
  ./craft clear-caches/all
  composer update


so I thought I could add it to the hook, just above done &lt; /dev/stdin but it doesn't work, I still have to manually update.

Has anyone else done something like this before?
Based on your comment that you get an error saying Craft is not installed, to me that suggests the bash script does not have access to your environment variables, (this can also be a problem when running cron jobs).

To remedy this, create an env text file to export all the same values as those in your production env variables. For this example name it something like .env_cli.txt and save it in your site in the same place as .env-example if you have one. Add it to your .gitignore too. Syntax should look something like this:

export ENVIRONMENT="live"
export SITE_URL="https://www.example.com"
export DB_DRIVER="mysql"
export DB_SERVER="123.456.789.10"
export DB_USER="aaa"
export DB_PASSWORD="bbb"
export DB_DATABASE="ccc"
export SECURITY_KEY="ddd"
export DB_TABLE_PREFIX="craft_"
export SOME_CUSTOM_VAR_YOU_MIGHT_HAVE="something"


Then in your git hook, you can use a leading . to "source" that env file (basically meaning to suck in and consume all those exported vars), and then run your command(s) afterwards like this:

. /path/to/your/env-file/.env_cli.txt &amp;&amp; /path/to/craft/./craft project-config/sync &amp;&amp; /path/to/craft/./craft clear-caches/all &amp;&amp; /path/to/craft/composer update


You could of course stick with relative paths to make it shorter, I just tend to use absolute paths for more certainty.