Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
git autodeploy script when it matches the string "[deploy]"
#!/bin/sh
#
# git autodeploy script when it matches the string "[deploy]"
#
# @author icyleaf <icyleaf.cn@gmail.com>
# @link http://icyleaf.com
# @version 0.1
#
# Usage:
# 1. put this into the post-receive hook file itself below
# 2. `chmod +x post-recive`
# 3. Done!
# Check the remote git repository whether it is bare
IS_BARE=$(git rev-parse --is-bare-repository)
if [ -z "$IS_BARE" ]; then
echo >&2 "fatal: post-receive: IS_NOT_BARE"
exit 1
fi
# Get the latest commit subject
SUBJECT=$(git log -1 --pretty=format:"%s")
# Deploy the HEAD sources to publish
IS_PULL=$(echo "$SUBJECT" | grep "\[deploy\]")
if [ -z "$IS_PULL" ]; then
echo >&2 "tips: post-receive: IS_NOT_PULL"
exit 1
fi
# Check the deploy dir whether it exists
DEPLOY_DIR=/home/icyleaf/php/icyleaf/
if [ ! -d $DEPLOY_DIR ] ; then
echo >&2 "fatal: post-receive: DEPLOY_DIR_NOT_EXIST: \"$DEPLOY_DIR\""
exit 1
fi
# Check the deploy dir whether it is git repository
#
#IS_GIT=$(git rev-parse --git-dir 2>/dev/null)
#if [ -z "$IS_GIT" ]; then
# echo >&2 "fatal: post-receive: IS_NOT_GIT"
# exit 1
#fi
# Goto the deploy dir and pull the latest sources
cd $DEPLOY_DIR
#env -i git reset --hard
env -i git pull
@Gerhut

This comment has been minimized.

Copy link

commented Sep 6, 2013

您好,我用这个脚本之后部署端只能pull到前一个版本的代码(就是我push之前的代码)。典型的输出是这样的:

$ git push
Counting objects: 5, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 276 bytes, done.
Total 3 (delta 2), reused 0 (delta 0)
remote: Already up-to-date.

我检查再三,确实是把这段代码粘贴到了post-receive这个hook里。
请问您有没有遇到过这种问题,是如何解决的。
谢谢

问题解决了。。我用http方式clone的,所以在git pull之前要先git update-server-info
所以这段脚本直接放到post-update钩子里就好了。。。

@lovecn

This comment has been minimized.

Copy link

commented Sep 4, 2014

赞,正需要呢

@kidlj

This comment has been minimized.

Copy link

commented Mar 3, 2016

请问脚本的最后一个命令为什么要用 env -i git pull,直接 git pull 为什么不可以?谢谢。

@hezll

This comment has been minimized.

Copy link

commented Apr 6, 2016

请问用户名和密码如何解决

@justjavac

This comment has been minimized.

Copy link

commented Dec 21, 2016

@hezll 证书啊

@gengxiankun

This comment has been minimized.

Copy link

commented May 30, 2017

有一事不明:
用SUBJECT=$(git log -1 --pretty=format:"%s")得到得不是裸库得log吗?因为在最后才执行得cd $DEPLOY_DIR到醒目目录,难道说使用裸库做得一次提交匹配[deploy]?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.