如何使用Gitlab CI运行程序设置非常简单的部署? [英] How do I set up a very simple deployment using a Gitlab CI runner?

查看:81
本文介绍了如何使用Gitlab CI运行程序设置非常简单的部署?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将新工作从我的Gitlab存储库自动部署到在生产服务器上运行的实时网站.实时网站是live分支的GIT回购克隆.

I'd like to automatically deploy new work from my Gitlab repository to a live website running on a production server. The live website is a GIT repo clone of the live branch.

我的问题:

  • 每次发生构建"时,跑步者似乎都将我的存储库重新克隆到~/builds/...中.这是强制性行为吗?我想我真正想要的只是生产网站目录中的git pull.

  • each time a 'build' happens, the runner seems to clone my repo afresh, into ~/builds/.... Is this mandatory behaviour? All I think I really want is a git pull inside my production website directory.

如果确实必须每次都克隆存储库,为什么不git reset呢?至少这样可以节省大量的带宽,不是吗?

if it really has to clone the repo each time, why doesn't it git reset it? At least that would save a ton of bandwidth over time, wouldn't it?

如何运行存储库根目录中的deploy.sh?我目前在gitlab.com构建信息中收到此错误:bash: line 23: deploy.sh: command not found

how do I run my deploy.sh which is in the repository root? I currently get this error in my gitlab.com builds info: bash: line 23: deploy.sh: command not found

我的.gitlab-ci.yml文件:

deploy_to_production:
  script:
    - deploy.sh
  only:
    - live
  tags:
    - prod

详细信息

为此,每当我推送到仓库的特定分支(在我的情况下为live分支)时,我都想运行一个在生产服务器上编写的简单shell脚本(deploy.sh).这个Shell脚本与我的.gitlab-ci.yml一起位于我的GIT存储库中.

Details

To do this, I want to run a simple shell script that I wrote (deploy.sh) on the production server, whenever I push to a specific branch of my repo (the live branch, in my case). This shell script is in my GIT repository, alongside my .gitlab-ci.yml.

该脚本基本上只执行git resetfetchpull,使生产版本与存储库中的内容保持最新.

The script basically just does a git reset, fetch and pull, bringing the production version up to date with what's in the repository.

我已经在服务器上安装了一个"shell"多运行程序,并将其连接到gitlab.com,在那里我可以看到它处于活动状态.

I have installed a "shell" multi-runner on my server, and connected that to gitlab.com, where I can see it active.

总的来说,我在吠错一棵树吗?我应该更改我的deploy.sh以便不对新工作进行git checkout,而是使用cp或也许rsync将新代码从~/builds/...移至生产网站吗?

Overall, I am barking up the wrong tree? Should I change my deploy.sh so that it doesn't do a git checkout of the new work, but rather uses cp or perhaps rsync to move the new code from ~/builds/... into the production website?

推荐答案

问:克隆是强制性行为吗?

据我所知,是因为假设是在构建过程中您使用代码.

As far as I know it is because the assumption is that during the build process you work with the code.

如果这不是您的情况,并且您只想告诉生产服务器"从您的存储库中提取代码,那么也许您可以采用其他方法并使用

If that's not your case and you only want to "tell the production server" to pull the code from your repo, then maybe you can take a different approach and use a webhook instead. GitLab would make a HTTP request to your server and pass the information about the event (push, merge, etc.) in the JSON format. The receiving script would then parse it and make a decision whether to do the git pull.

如果在部署之前的构建过程中确实还有其他任务要对代码执行,那么可以,更改策略并使用运行程序已为您签出的代码.我可能会尝试以某种方式进行更改 atomic (同步到live目录中,会使应用程序在运行时处于不确定状态,如果由于某种原因而失败则陷入混乱),例如通过将实时代码放置在符号链接的目录中.然后,部署脚本会将新的代码目录复制到其最终目标,进行必要的调整,并将符号链接的目标从旧的实时目录更改为新的目录.

If you do have other tasks to perform on the code during the build process before deployment, then yes, alter the strategy and use the code that the runner has already checked out for you. I'd probably attempt to have the change atomic in some way (rsyncing into the live directory would leave your application in some uncertain state when it runs and in a mess if it fails for some reason), e.g. by having the live code in a directory that is symlinked. The deployment script would then copy the new code directory to its final destination, make any necessary adjustments and change the symlink's target from the old live directory to the new one.

问:如果确实必须每次都克隆存储库,为什么不进行git reset?

对于开发人员来说,这更是一个问题.任何人的答案都只是猜测.

That's more of a question for the developers. Anybody else's answer would be just speculation.

问:如何运行存储库根目录中的deploy.sh?

如果 deploy.sh 脚本设置了可执行位,则使用./deploy.sh,否则使用bash deploy.sh.

If the deploy.sh script has the executable bit set, then use ./deploy.sh, otherwise bash deploy.sh.

这篇关于如何使用Gitlab CI运行程序设置非常简单的部署?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆