我们目前使用VSTS和git进行开发。分支数量越来越多,为了不必每个月手动删除分支,我想实现自动删除超过60天未被触碰的分支的功能。我知道Jenkins可以做到这一点,但我们目前没有使用Jenkins,想知道是否可以通过某些服务钩子在VSTS中实现这一点?
我们目前使用VSTS和git进行开发。分支数量越来越多,为了不必每个月手动删除分支,我想实现自动删除超过60天未被触碰的分支的功能。我知道Jenkins可以做到这一点,但我们目前没有使用Jenkins,想知道是否可以通过某些服务钩子在VSTS中实现这一点?
VSTS目前不支持服务器端钩子。但是有其他方法可以自动删除本地机器上的旧分支。具体步骤如下:
1. 在某个目录(例如D:\script_for_git
)中,克隆VSTS git仓库(仅用于自动删除分支)。
2. 在根git仓库(D:\script_for_git\repo
)中添加一个shell脚本(del.sh
),以删除180天(6个月)前未更改的远程分支,shell脚本的内容如下:
git fetch origin
for reBranch in $(git branch -a)
do
{
if [[ $reBranch == remotes/origin* ]];
then
{
if [[ $reBranch ==remotes/origin/HEAD ]]; then
echo "HEAD is not a branch"
else
branch=$(echo $reBranch | cut -d'/' -f 3)
echo $branch
sha=$(git rev-parse origin/$branch)
dateo=$(git show -s --format=%ci $sha)
datef=$(echo $dateo | cut -d' ' -f 1)
Todate=$(date -d "$datef" +'%s')
current=$(date +'%s')
day=$(( ( $current - $Todate )/60/60/24 ))
echo $day
if [ "$day" -gt 180 ]; then
git push origin :$branch
echo "delete the old branch $branch"
fi
fi
}
fi
}
done
3. 安排运行此shell脚本。有很多种安排运行脚本的方式,这与操作系统有关。例如,如果您使用的是Windows系统,可以参考this post。如果您使用的是Linux系统,可以参考this post。