gityarnpkggit-post-receive

yarn install fails on cloning github packages in git post-receive hook


I have created a post-receive hook to deploy changes to an app and install packages via Yarn. It looks like this:

#!/bin/sh

echo "Checking out changes..."
git --work-tree=/home/me/apps/app --git-dir=/home/me/repos/repo.git 
checkout -f

echo "Yarn install..."
cd /home/me/apps/app
yarn install

Notes (stuff that is working):

The first portion works fine. The hook is definitely running. The primary app files are updated as expected.

When doing yarn install from the command line while ssh'd into the server, all packages are installed with no problem. (The user ssh'd into the server is the same user pushing commits and running the hook.)

All directories have been checked for permissions issues.

Problem:

The hook fails every time while trying to install packages via Yarn. Specifically, it fails while trying to install package dependencies from Github. It does seem to retrieve the packages (or at least it doesn't tell me there was any problem retrieving them), it just is doing something wrong when trying to move them into the node_modules directory.

I get one of two errors:

remote: error Command failed.
remote: Exit code: 128
remote: Command: git
remote: Arguments: pull
remote: Directory: 
/home/me/.cache/yarn/v2/.tmp/45d918f2ecb73f845db6f9b2f91617a3
remote: Output:
remote: fatal: Not a git repository: '.'

Or:

remote: error Command failed.
remote: Exit code: 128
remote: Command: git
remote: Arguments: clone https://github.com/Account/package.git /home/me/.cache/yarn/v2/.tmp/45d918f2ecb73f845db6f9b2f91617a3
remote: Directory: /home/me/apps/app
remote: Output:
remote: fatal: Working tree '/home/me/apps/app' already exists

Some of the package and user details above have been modified a little, but any git-related package installation is failing, but only in the post-receive hook.

In the second error, it's weird, because the git command seems to suggest it's trying to clone the repo into cache, but then has a message suggesting that it was trying to clone the package into the primary app directory.

I went so far as to use the hook to copy the package.json into a new directory, cd into it, and try to yarn install. Same sort of errors resulted.

I'm totally befuddled.


Solution

  • I was ignorant of the fact that Git operations utilize the GIT_DIR and GIT_WORK_TREE environment variables, so that the variables I was setting were interfering with Yarn's Git operations.

    Solution, unset the variables before yarn install:

    #!/bin/sh
    
    export GIT_WORK_TREE=/home/me/apps/app
    
    echo "Checking out changes..."
    git --work-tree=$GIT_WORK_TREE --git-dir=/home/me/repos/repo.git 
    checkout -f
    
    echo "Yarn install..."
    cd $GIT_WORK_TREE
    unset GIT_WORK_TREE
    yarn install