使用git将网站部署到多个环境

发布于 2024-12-05 20:43:01 字数 954 浏览 0 评论 0原文

目前,我使用 git 将项目部署到生产环境,使用 git push production master 到具有以下接收后挂钩的存储库:

#!/bin/bash
GIT_WORK_TREE=/home/username/www.example.com/myproject/ git checkout -f

product 是远程的,通过 添加git 远程添加生产 ssh://server/home/username/projects/myproject.git

但现在我需要将一个单独的分支部署到服务器上的单独路径。我确实想出了一个解决方案,但我想还有更好的方法。

我所做的是在服务器上创建一个新的存储库 myproject-alternate.git,并使用类似的 post-receive 挂钩(将 /myproject/ 替换为 /myproject -alternate/),使用 git remote add replacement ssh://server/home/username/projects/myproject-alternate.git 添加了这个新存储库。现在我可以使用 git Push Alternative Branchname:master 部署到备用路径。

这可行,但我有一些问题:

  1. 部署到备用服务器的命令不是我所期望的 - 我不止一次忘记了最后的 :master 并且服务器的存储库收到了一个新分支并且接收后挂钩没有被触发。
  2. 我不确定在服务器上创建新存储库是否是最佳解决方案,并且我想知道更大的项目会发生什么。

是否有其他方法可以完成此部署流程而不会出现上述问题?也许有一个更好的接收后挂钩,可以使用收到的分支名称部署到正确的路径? (这可能吗?)

Currently I deploy a project to production with git, using git push production master to a repository with the following post-receive hook:

#!/bin/bash
GIT_WORK_TREE=/home/username/www.example.com/myproject/ git checkout -f

production is a remote, added via git remote add production ssh://server/home/username/projects/myproject.git.

But now I need to deploy a separate branch to a separate path on the server. I did come up with a solution, but I suppose there's a better way to do it.

What I did was create a new repository on the server, myproject-alternate.git, with a similar post-receive hook (replacing /myproject/ with /myproject-alternate/), added this new repository with git remote add alternate ssh://server/home/username/projects/myproject-alternate.git. Now I can deploy to the alternate path with git push alternate branchname:master.

This works, but I have some issues:

  1. The command to deploy to the alternate server is not what I was expecting—more than once I forgot the :master at the end and the server's repository received a new branch and the post-receive hook wasn't triggered.
  2. I'm not sure if creating a new repository on the server was the best solution, and I wonder what would happen with a larger project.

Are there other ways to accomplish this deploy flow without the mentioned issues? Maybe a better post-receive hook that uses the received branchname to deploy to the right path? (is this even possible?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

兔姬 2024-12-12 20:43:01

我写了一篇 博客文章 关于我用来将网站部署到临时服务器和实时服务器的设置。你可以做类似的事情。关键是配置您要在本地存储库的 .git/config 文件中推送哪些分支,如下所示:

[remote "alternate"]
    url = ssh://server/home/username/projects/myproject-alternate.git
    fetch = +refs/heads/master:refs/remotes/alternate/master
    pushurl = ssh://server/home/username/projects/myproject-alternate.git
    push = refs/heads/branchname:refs/heads/master
[remote "production"]
    url = ssh://server/home/username/projects/myproject.git
    fetch = +refs/heads/master:refs/remotes/production/master
    pushurl = ssh://server/home/username/projects/myproject.git
    push = refs/heads/master:refs/heads/master

这将对其进行设置,以便每当您键入时

git push alternate

都会自动将本地 branchname 分支推送到备用存储库中的远程 master 分支。

但是,由于您的备用工作树和生产工作树位于同一台计算机上,因此您可能只需创建一个存储库并将其签出到两个不同的位置即可。为此,请忽略上一段,而是在 post-receive 挂钩中添加类似的内容:(

#!/bin/bash
checkout_alt=
checkout_prod=
while read oldrev newrev refname; do
    case "$refname" in
        ( "refs/heads/branchname" )
            export checkout_alt=1 ;;
        ( "refs/heads/master" )
            export checkout_prod=1 ;;
    esac
done
test -n "$checkout_alt" && GIT_WORK_TREE=/home/diazona/tmp/gittest/alt/ git checkout -f branchname
test -n "$checkout_prod" && GIT_WORK_TREE=/home/diazona/tmp/gittest/prod/ git checkout -f master

显然您没有 使用 if 语句)。如果您这样做,我建议将存储库设为裸露,以便它不会存储自己的工作副本,只是为了简单起见。这样,您只需要一个远程,每当您推送 master 分支时,它都会更新生产工作树,每当您推送 branchname 分支时,它都会更新生产工作树。将更新备用工作树。

免责声明:我还没有实际测试过这个,所以首先在测试文件夹中尝试一下。如果我发现任何错误,我会回来编辑。

I've written a blog post about a setup I use to deploy my website to a staging server and the live server. You could do something similar. The key is to configure which branches you're going to be pushing in the .git/config file of your local repository, something like this:

[remote "alternate"]
    url = ssh://server/home/username/projects/myproject-alternate.git
    fetch = +refs/heads/master:refs/remotes/alternate/master
    pushurl = ssh://server/home/username/projects/myproject-alternate.git
    push = refs/heads/branchname:refs/heads/master
[remote "production"]
    url = ssh://server/home/username/projects/myproject.git
    fetch = +refs/heads/master:refs/remotes/production/master
    pushurl = ssh://server/home/username/projects/myproject.git
    push = refs/heads/master:refs/heads/master

This will set it up so that whenever you type

git push alternate

it will automatically push the local branchname branch to the remote master branch in the alternate repository.

However, since your alternate and production work trees are on the same computer, you can probably get away with only creating one repository and just checking it out to two different places. To do that, ignore the previous paragraph, and instead put something like this in your post-receive hook:

#!/bin/bash
checkout_alt=
checkout_prod=
while read oldrev newrev refname; do
    case "$refname" in
        ( "refs/heads/branchname" )
            export checkout_alt=1 ;;
        ( "refs/heads/master" )
            export checkout_prod=1 ;;
    esac
done
test -n "$checkout_alt" && GIT_WORK_TREE=/home/diazona/tmp/gittest/alt/ git checkout -f branchname
test -n "$checkout_prod" && GIT_WORK_TREE=/home/diazona/tmp/gittest/prod/ git checkout -f master

(obviously you don't have to use an if statement). If you do this, I suggest making the repository bare so that it doesn't store its own working copy, just for simplicity. This way, you only need to have one remote, and whenever you push the master branch, it will update the production work tree, and whenever you push the branchname branch, it will update the alternate work tree.

Disclaimer: I haven't actually tested this, so try it out in a test folder first. If I find any errors I'll come back and edit.

箜明 2024-12-12 20:43:01

我还就我使用的设置撰写了一篇博客文章,这允许我将项目中的每个分支推送到我的远程,将每个分支部署到各自的目录中。

例如:

Branch      | Directory on remote
--------------------------------------
master      | /www/foo.com/master
staging     | /www/foo.com/staging
production  | /www/foo.com/produciton

因此,要执行此操作,请将以下函数添加到您的服务器点文件中:

function gitorigin() {
    mkdir -p "$1"
    cd "$1"
    git init --bare
    git config core.bare false
    git config receive.denycurrentbranch ignore
    cat <<EOF >hooks/post-receive
#!/bin/bash

while read oldrev newrev ref
do
    branch=\`echo \$ref | cut -d/ -f3\`
    mkdir -p ../\$branch
    git --work-tree=../\$branch checkout -f \$branch
    echo Changes pushed to \$branch
done
EOF
    chmod +x hooks/post-receive
    cd ..
}

在远程上,创建一个存储库:

$ cd /www/foo.com
$ gitrepo foo.git

在本地上,推送一个分支

$ cd ~/Sites/foo.com
$ git remote add origin ssh:///<USER>@<HOST>/www/foo.com/foo.git
$ git push origin <BRANCH>

将您的主分支推送到远程,进入 /www/foo.com /<分支>/

I've also written a blog post on the set up I use, which allows me to git push each branch in my project up to my remote, deploying each into their own respective directories.

For example:

Branch      | Directory on remote
--------------------------------------
master      | /www/foo.com/master
staging     | /www/foo.com/staging
production  | /www/foo.com/produciton

So, to do this, add the following function to your server dotfiles:

function gitorigin() {
    mkdir -p "$1"
    cd "$1"
    git init --bare
    git config core.bare false
    git config receive.denycurrentbranch ignore
    cat <<EOF >hooks/post-receive
#!/bin/bash

while read oldrev newrev ref
do
    branch=\`echo \$ref | cut -d/ -f3\`
    mkdir -p ../\$branch
    git --work-tree=../\$branch checkout -f \$branch
    echo Changes pushed to \$branch
done
EOF
    chmod +x hooks/post-receive
    cd ..
}

On Remote, create a repository:

$ cd /www/foo.com
$ gitrepo foo.git

On Local, push a branch

$ cd ~/Sites/foo.com
$ git remote add origin ssh:///<USER>@<HOST>/www/foo.com/foo.git
$ git push origin <BRANCH>

Will push your master branch up to remote, into /www/foo.com/<BRANCH>/.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文