Skip to content

Instantly share code, notes, and snippets.

@adamlundrigan
Created April 13, 2012 18:13
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adamlundrigan/2378880 to your computer and use it in GitHub Desktop.
Save adamlundrigan/2378880 to your computer and use it in GitHub Desktop.
Having problems running Git command via a Phing exectask in a Git post-receive hook

Problem

Running git command through a Phing ExecTask:

<exec command="git remote rm origin" dir="${build.src}" checkreturn="false" />

This works when phing build.setup is executed directly from command prompt. However, the same process fails when run from a Git post-receive hook with this message:

fatal: Not a git repository: '.'

The output (see RUN_POSTRECEIVE.txt below) of this Phing command:

<exec command="{ echo $USER; pwd ; ls -l .git ; git remote rm origin; } > /tmp/git.log" dir="${build.src}" checkreturn="false" />

Shows that the command is being executed from the right location by the right user, and the .git folder exists and has git-y contents. But immediately thereafter git complains that the folder is not a repository.

<?xml version="1.0" encoding="UTF-8"?>
<project name="gittest" basedir="." default="help">
<property name="build.src" value="src" />
<property name="build.src.repo" value="/var/git/cdliportal" />
<property name="build.src.branch" value="release" />
<property name="build.src.revision" value="0f0bb605" />
<target name="build.setup">
<echo msg="Configuring build environment..." />
<!-- Clone Source Git Repository -->
<if>
<available file="${build.src}/.git" type="dir" />
<then>
<echo msg="Updating clone of source repository..." />
<!-- Update origin and fetch it -->
<exec command="{ echo $USER; pwd ; ls -l .git ; git remote rm origin; } > /tmp/git.log" dir="${build.src}" checkreturn="false" />
<exec command="git remote add origin ${build.src.repo}" dir="${build.src}" checkreturn="true" />
<exec command="git fetch origin" dir="${build.src}" checkreturn="true" />
<!-- Reset the repository to the desired state -->
<exec command="git checkout --force ${build.src.branch}" dir="${build.src}" checkreturn="true" />
<exec command="git reset --hard ${build.src.revision}" dir="${build.src}" checkreturn="true" />
</then>
<else>
<echo msg="Cloning source repository..." />
<!-- Clone the desired repository and branch plus all submodules -->
<mkdir dir="${build.src}" />
<exec command="git clone --recursive -b ${build.src.branch} ${build.src.repo} ." dir="${build.src}" checkreturn="true" />
</else>
</if>
</target>
</project>
#!/bin/bash
read oldrev newrev refname;
if [ "$refname" = "refs/heads/release" ]
then
echo "Deploying $newrev...";
{
cd /var/git/deploy/test;
phing build.setup -logfile /tmp/cdliportal-deploy.log -Dbuild.src.revision=$newrev;
cat /tmp/cdliportal-deploy.log | mail -s "[DEPLOY] $refname@$newrev" adamlundrigan@cdli.ca
}
fi
[adam@dev test]$ phing -verbose build.setup
Buildfile: /var/git/deploy/test/build.xml
Override ignored for user property phing.file
parsing buildfile build.xml
Project base dir set to: /var/git/deploy/test
Build sequence for target 'build.setup' is: build.setup
Complete build sequence is: build.setup
gittest > build.setup:
[echo] Configuring build environment...
Property ${build.src} => src
[echo] Updating clone of source repository...
Property ${build.src} => src
[exec] Executing command: git remote rm origin 2>&1
[exec] error: Could not remove config section 'remote.origin'
Property ${build.src.repo} => /var/git/cdliportal
Property ${build.src} => src
[exec] Executing command: git remote add origin /var/git/cdliportal 2>&1
Property ${build.src} => src
[exec] Executing command: git fetch origin 2>&1
[exec] From /var/git/cdliportal
[exec] * [new branch] master -> origin/master
[exec] * [new branch] release -> origin/release
[exec] * [new branch] staging -> origin/staging
Property ${build.src.branch} => release
Property ${build.src} => src
[exec] Executing command: git checkout --force release 2>&1
[exec] Already on 'release'
Property ${build.src.revision} => 0f0bb605
Property ${build.src} => src
[exec] Executing command: git reset --hard 0f0bb605 2>&1
[exec] HEAD is now at 0f0bb60 Update CdliJS view helper to get path from config file
BUILD FINISHED
Total time: 1.2056 second
Contents of /tmp/cdliportal-deploy.log after post-receive hook is run:
======================================================================
Buildfile: /var/git/deploy/test/build.xml
gittest > build.setup:
[echo] Configuring build environment...
[echo] Updating clone of source repository...
[if] Error in IfTask
Execution of target "build.setup" failed for the following reason: /var/git/deploy/test/build.xml:13:4: /var/git/deploy/test/build.xml:20:30: Task exited with code 128
BUILD FAILED
/var/git/deploy/test/build.xml:13:4: /var/git/deploy/test/build.xml:20:30: Task exited with code 128
Total time: 0.1187 seconds
Contents of /tmp/git.log after post-receive hook is run:
(PHING: <exec command="{ echo $USER; pwd ; ls -l .git ; git remote rm origin; } > /tmp/git.log" dir="${build.src}" checkreturn="false" />)
======================================================================
adam
/var/git/deploy/test/src
total 360
drwxr-xr-x 2 adam users 4096 Apr 13 15:22 branches
-rw-r--r-- 1 adam users 584 Apr 13 15:29 config
-rw-r--r-- 1 adam users 73 Apr 13 15:22 description
-rw-r--r-- 1 adam users 284 Apr 13 15:29 FETCH_HEAD
-rw-r--r-- 1 adam users 24 Apr 13 15:29 HEAD
drwxr-xr-x 2 adam users 4096 Apr 13 15:22 hooks
-rw-r--r-- 1 adam users 252817 Apr 13 15:29 index
drwxr-xr-x 2 adam users 4096 Apr 13 15:22 info
drwxr-xr-x 3 adam users 4096 Apr 13 15:22 logs
drwxr-xr-x 3 adam users 4096 Apr 13 15:22 modules
drwxr-xr-x 255 adam users 4096 Apr 13 15:22 objects
-rw-r--r-- 1 adam users 41 Apr 13 15:22 ORIG_HEAD
-rw-r--r-- 1 adam users 894 Apr 13 15:22 packed-refs
drwxr-xr-x 5 adam users 4096 Apr 13 15:22 refs
fatal: Not a git repository: '.'
@adamlundrigan
Copy link
Author

Simple solution: In post-receive hook, SSH into same host and run command:

#!/bin/bash

read oldrev newrev refname;
if [ "$refname" = "refs/heads/release" ]
then
    echo "Deploying $newrev...";
    {
        ssh $(hostname) "cd /var/git/deploy ; phing build.setup -logfile /tmp/cdliportal-deploy.log -Dbuild.src.revision=$newrev >/dev/null"
        cat /tmp/cdliportal-deploy.log | mail -s "[DEPLOY] $refname@$newrev" adamlundrigan@cdli.ca
    }
fi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment