使用Gitlab CI将每个构build部署到服务器
我已经为其configuration了一个项目和一个Gitlab运行器,build立了自己的Gitlab服务器。 我是新来的持续集成服务器,因此不知道如何完成以下。
每次我承诺我的项目主分支我想部署到另一台服务器的存储库,并在那里运行两个shell命令( npm install
和forever restartall
)
我将如何做到这一点? 我也需要在项目部署的机器上使用跑步者吗?
您可以使用gitlab-ci和gitlab-runner [runners.ssh]将其部署到单个或多个服务器。
stream程:
(git_project with yml file) --> (gitlab && gitlab-ci) --> (gitlabrunner) ---runners.ssh---> (deployed_server,[deploye_server2])
-
你需要将gitlab-runner注册到gitlab-ci,并将标签设置为gitlab web上的delpoyServer。 /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意味着运行器将login到${the_destionation_of_deployServer_IP1}
和${the_destionation_of_deployServer_IP2}
,然后将该项目克隆到builds_dir
。
-
编写yml文件例如:.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设置为' http://your.gitlab.server/ci/admin/runners '中的
delpoyServer1
和delpoyServer2
标签,- 当你把你的代码推到gitlab
- gitlab-ci服务器将parsing你项目中的
.gitlab-ci.yml
文件,select标签为:deployServer1
或者deployServer2
; - 带有deployServer1标签的
gitlab-runner
将使用sshlogin到${the_destionation_of_deployServer_IP1}
和${the_destionation_of_deployServer_IP2}
,将项目克隆到builds_dir
,然后执行脚本:npm install && forever restartall。
链接:
- gitlab-runner寄存器
- runners.ssh
你应该可以使用gitlab-ci.yml文件在你的.gitlab-ci.yml
文件中添加一个独立的build
阶段。
您将需要某种部署服务(如capistrano
或类似),或者将启动部署的webhook。
即像是:
--- stages: - test - deploy job_runtests: stage: test script: - npm test job_deploy: stage: deploy script: - curl -X POST https://deploymentservice.io/?key=
Gitlab CI将遍历它发现的每个阶段,并依次运行它们。 如果一个舞台通过,那么它就转到下一个舞台。
不幸的是,Gitlab CI不能直接进行部署(尽pipe你可以安装dpl
Ruby Gem并在你的.gitlab-ci.yml
文件中调用它,如下所示:
job_deploy: - gem install dpl - dpl --provider=heroku --app=my-app-staging --api-key=$HEROKU_STAGING_API_KEY only: - master
例如)