【问题标题】:Bash git autodeploy warBash git 自动部署战争
【发布时间】: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


【解决方案1】:

您不能将多个文件复制到一个文件中。

你的cpcp find /path/to/*.war /path/to/file.war

这有很多问题。

find 在那里没有做任何有用的事情(它被视为要复制的文件)。

输出目标是一个文件(不是目录)。

您需要弄清楚您实际上想要在那里做什么并修复该复制行。

可能是cp /home/paul/webapps/test/target/*.war /opt/jetty/webapps/test 之类的?

【讨论】:

  • 但我希望 .war 与存储库具有相同的名称,并且我不知道原始文件名。这是我的问题
  • @PaulDumitru 如果/home/paul/webapps/test/target/*.war 只匹配一个文件,那么只需从该行中删除错误的find,它应该可以工作。但如果该 glob 匹配多个文件,它将中断。
  • 谢谢,我会按照你说的去做
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-30
  • 2018-02-10
  • 1970-01-01
  • 2011-01-24
相关资源
最近更新 更多