2

I have a GitHub repo that I use to deploy to Platform.sh, which is a website hosting PaaS.

If I am pushing to my testing branch, I want to sync the environment on Platform.sh before pushing so that my tests run on a clean copy of my database/files.

So, based on the .git/hooks example, I added a pre-push hook. When I push, if I am pushing to the testing branch, then I want to run the sync command (platform environment:synchronize data -y), and if the sync is successful, I want to push.

I have tested my hook and it works correctly when I am not on the testing branch or when the sync command returns an error (such as when syncing fails because another sync is already in progress).

However, when the sync command is successful, the hook completes (I see the message "Pushing to github!"), but the files are not actually pushed to github.

If I comment out the sync command to test the logic of the hook, then the files are pushed to github as I expect.

How can I get the code to actually push when the sync command is successful?

Reference

The sync command prints out a few hundred lines of text, the final line of which is post_deploy processing complete! if the sync is successful.

Here's my pre-push hook:

#!/bin/bash

# We need to fail if platform.sh is busy.
set -eo pipefail

# An example hook script to verify what is about to be pushed.  Called by "git
# push" after it has checked the remote status, but before anything has been
# pushed.  If this script exits with a non-zero status nothing will be pushed.
#
# This hook is called with the following parameters:
#
# $1 -- Name of the remote to which the push is being done
# $2 -- URL to which the push is being done
#
# If pushing without using a named remote those arguments will be equal.
#
# Information about the commits which are being pushed is supplied as lines to
# the standard input in the form:
#
#   <local ref> <local sha1> <remote ref> <remote sha1>
#
# This sample shows how to prevent push of commits where the log message starts
# with "WIP" (work in progress).

# git_remote ="$1"
git_origin_url="$2"

z40=0000000000000000000000000000000000000000

# https://stackoverflow.com/a/21434677
current_branch=$(git symbolic-ref HEAD | sed -e 's,.*/\(.*\),\1,')
testing_branch='mytestbranch'
github_url='github:myorg/repo.git'

while read local_ref local_sha remote_ref remote_sha
do
    if [ "$local_sha" = $z40 ]
    then
        # Handle delete
        :
    else
        if [ "$remote_sha" = $z40 ]; then
            # New branch, examine all commits
            range="$local_sha"
        else
            # Update to existing branch, examine new commits
            range="$remote_sha..$local_sha"
        fi

        if [ "$current_branch" == "$testing_branch" ] && [ "$git_origin_url" == "$github_url" ]; then
            # Synchronize data from platform.sh
            echo "Starting data sync for $current_branch due to $github_url"
            # Also capture stderr: 2>&1.
            sync_output="$(platform environment:synchronize data -y 2>&1)"
            test_complete_message="post_deploy processing complete!"
            if [ "$(echo "$sync_output" | grep "$test_complete_message")" ]; then
                echo "Data sync complete for $current_branch"
            else
                echo "PUSH SYNC ERROR: $sync_output";
                exit 1;
            fi
        fi
    fi
done

# Go ahead and push!
echo "Pushing to github!"
exit 0

Script output when executed:

Starting data sync for mytestbranch due to github:myorganization/repo.git
Connection to github.com closed by remote host.
Data sync complete for mytestbranch
Pushing to github!

I'm not sure what this line "Connection to github.com closed by remote host." indicates in this context, but otherwise everything appears fine.

Patrick Kenny
  • 4,515
  • 7
  • 47
  • 76
  • 3
    The pre-push hook starts up *after* you're connected *to* the system the push will go to (because Git has to fetch, from that system, the references that it will dump into the pre-push hook). This gives you a limited amount of time to do your verification. My guess—it's just a guess here at this point—is that the sync takes too long and the other end gives up on you before the sync finishes, so that the push can't do anything. – torek Feb 07 '22 at 09:32
  • 2
    If that's the case, you'll want to either fancy up the hook a lot, or perhaps (much simpler) don't use a pre-push hook at all, and don't run `git push` directly. Run some other command, with the other command deciding whether to do the sync first and then run `git push`, or to run `git push` without doing a sync first. – torek Feb 07 '22 at 09:33
  • @torek Thanks, I see what's happening now. In fact, the sync takes about 10 minutes, so I will write a bash script and use that to push instead. If you could re-post your comment as an answer, I'd be happy to accept. – Patrick Kenny Feb 07 '22 at 15:11

0 Answers0