使用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
)
我该怎么做?
推荐答案
您可以使用gitlab-ci和gitlab
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])
-
-runner到gitlab-ci,并在gitlab web上将标签设置为delpoyServer。
/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"
<运行程序将登录到 $ {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 < http://您的网址中的code>和
delpoyServer2
.gitlab
set the your gitlab-runner to delpoyServer1
and delpoyServer2
tags in 'http://your.gitlab.server/ci/admin/runners'
- $ b
- gitlab-ci服务器将在您的项目中解析您的
.gitlab-ci.yml
文件,选择一个带有标签的运行器:deployServer1
或deployServer2
; - 使用deployServer1标签的
gitlab-runner
将登录$ {the_destionation_of_deployServer_IP1}
和$ {the_destionation_of_deployServer_IP2}
使用ssh,将项目克隆到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-runner register
- runners.ssh
这篇关于使用Gitlab CI将每个构建部署到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!