【发布时间】:2023-03-13 22:45:01
【问题描述】:
我正在尝试制作一个 git 挂钩来自动部署我的战争 .我创建了一个 bash 脚本来创建存储库,使用 Maven 构建并将战争自动部署到 jetty webapps 文件夹
3) echo "Creating new Maven (Java) repository"
echo "Please enter a project name : "
read REPO
if [ -n $REPO ]; then
if [ -d /home/$GITUSER/webapps/$REPO ]; then
echo "Destination deployment directory already exists"
else
echo "Creating new repository deployment directory"
mkdir /home/$GITUSER/webapps/$REPO
echo "Creating new repository"
mkdir /git/$REPO.git && cd /git/$REPO.git && git init --bare --shared
echo "Please enter a description for the repository or hit return for none"
read DESC
if [ -n "$DESC" ]; then
cat /dev/null > /git/$REPO.git/description
echo "$DESC" > /git/$REPO.git/description
fi
echo "Creating repository deployment hook"
touch /git/$REPO.git/hooks/post-receive
echo "#!/bin/bash\n" >> /git/$REPO.git/hooks/post-receive
echo "git --work-tree=/home/$GITUSER/webapps/$REPO --git-dir=/git/$REPO.git checkout -f" >> /git/$REPO.git/hooks/post-receive
echo "cd /home/$GITUSER/webapps/$REPO && mvn package" >> /git/$REPO.git/hooks/post-receive "\n"
echo "cp find /home/$GITUSER/webapps/$REPO/target/*.war /opt/jetty/webapps/$REPO.war" >> /git/$REPO.git/hooks/post-receive
chmod +x /git/$REPO.git/hooks/post-receive
echo "Repository created successfuly"
echo "Use git clone ssh://$GITUSER@$GITDOMAIN:$GITPORT/git/$REPO.git"
echo "Thank you !"
exit
fi
else
echo "Project name cannot be empty"
fi;;
问题出在我试图复制战争的这一行
echo "cp find /home/$GITUSER/webapps/$REPO/target/*.war /opt/jetty/webapps/$REPO.war" >> /git/$REPO.git/hooks/post-receive
你能给我一个建议吗? 谢谢!
【问题讨论】:
-
[ -n $REPO ]如果您没有收到任何输入,将返回 true,请尝试一下。你想要[ -n "$REPO" ]。一般来说,总是引用你的变量。 -
那行有什么问题?它不工作吗?钩子里的那条线以后不工作了吗?什么?
-
在将内容回显到文件之前,没有理由
cat /dev/null > /git/$REPO.git/description。同样没有理由在向其发送内容之前touch /git/$REPO.git/hooks/post-receive。 -
该行不在 post-receive 钩子中,我试图把它放在那里,而是在写入文件之前由 bash 执行。我粘贴的是我的“外部”脚本的一部分,它创建了钩子,所以问题是该命令是在 repo 提供源之前执行的。
-
echo "cp find /home/$GITUSER/webapps/$REPO/target/*.war /opt/jetty/webapps/$REPO.war" >> /git/$REPO.git/hooks/post-receive行不在此时运行cp命令。您确实在该行之前的行尾有一个奇怪/流浪的"\n",尽管这肯定会使某些事情感到困惑。你从你的脚本中看到了什么输出正是? (如果你将set -x添加到该块的顶部呢?)
标签: java git bash maven ubuntu-12.04