使用 Gitlab CI 将每个构建部署到服务器 [英] Deploy every build to a server using Gitlab CI
问题描述
我已经建立了自己的 Gitlab 服务器,其中包含一个项目和一个为其配置的 Gitlab 运行器.我是持续集成服务器的新手,因此不知道如何完成以下操作.
I've set up my own Gitlab server with one project and a Gitlab runner configured for it. I'm new to continuous integration server and therefore don't know how to accomplish the following.
每次我提交到项目的主分支时,我都想将存储库部署到另一台服务器并在那里运行两个 shell 命令(npm install
和forever restartall
)
Every time I commit to the master branch of my project I would like to deploy the repository to another server and run two shell-commands there (npm install
and forever restartall
)
我该怎么做?我是否也需要在项目部署到的机器上安装一个跑步者?
How would I do this? Do I need a runner on the machine which the project is deployed to as well?
推荐答案
您可以使用 gitlab-ci 和 gitlab-runner [runners.ssh] 部署到单个或多个服务器.
You could use gitlab-ci and gitlab-runner [runners.ssh] to deploy to single or mutiple servers.
流程:
(git_project with yml file) --> (gitlab && gitlab-ci) --> (gitlabrunner) ---runners.ssh---> (deployed_server,[deploye_server2])
您需要在 gitlab-ci 上注册 gitlab-runner 并将标签设置为 delpoyServer on gitlab web ./etc/gitlab-runner/config.toml:
you need register gitlab-runner to gitlab-ci and set the tag to delpoyServer on gitlab web . /etc/gitlab-runner/config.toml:
[[runners]]
url = "http://your.gitlab.server/ci"
token = "1ba879596cf3ff778ee744e6decedd"
name = "deployServer1"
limit = 1
executor = "ssh"
builds_dir = "/data/git_build"
[runners.ssh]
user = "you_user_name"
host = "${the_destionation_of_deployServer_IP1}"
port = "22"
identity_file = "/home/you_user_name/.ssh/id_rsa"
[[runners]]
url = "http://your.gitlab.server/ci"
token = "1ba879596cf3ff778ee744e6decedd"
name = "deployServer2"
limit = 1
executor = "ssh"
builds_dir = "/data/git_build"
[runners.ssh]
user = "you_user_name"
host = "${the_destionation_of_deployServer_IP2}"
port = "22"
identity_file = "/home/you_user_name/.ssh/id_rsa"
runner.ssh 表示,runner 将登录到 ${the_destionation_of_deployServer_IP1}
和 ${the_destionation_of_deployServer_IP2}
,然后将项目克隆到 builds_dir
.
the runner.ssh means, the runner will login into ${the_destionation_of_deployServer_IP1}
and ${the_destionation_of_deployServer_IP2}
, then clone the project to builds_dir
.
写入yml文件例如:.gitlab-ci.yml
write the yml file for example: .gitlab-ci.yml
job_deploy:
stage: deploy
tags: delpoyServer1
script:
- npm install && forever restartall
job_deploy:
stage: deploy
tags: delpoyServer2
script:
- npm install && forever restartall
将你的 gitlab-runner 设置为 delpoyServer1
和 delpoyServer2
标签在 'http://your.gitlab.server/ci/admin/runners'
- 当你将代码推送到 gitlab 时
- gitlab-ci 服务器将解析您项目中的
.gitlab-ci.yml
文件,选择带有以下标签的运行器:deployServer1
或deployServer2代码>;
- 带有 deployServer1 标签的
gitlab-runner
将使用 ssh 登录到${the_destionation_of_deployServer_IP1}
和${the_destionation_of_deployServer_IP2}
,克隆项目到builds_dir
,然后执行你的脚本: npm install &&永远重启.
- when you push you code to gitlab
- the gitlab-ci server will parser your
.gitlab-ci.yml
file in your project, choose a runner with the tags:deployServer1
ordeployServer2
; - the
gitlab-runner
with the deployServer1 tag will login into${the_destionation_of_deployServer_IP1}
and${the_destionation_of_deployServer_IP2}
with ssh , clone the project tobuilds_dir
, then execute you script: npm install && forever restartall.
链接:
这篇关于使用 Gitlab CI 将每个构建部署到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!