我已经搭建好了自己的Gitlab服务器,其中有一个项目和为其配置好的Gitlab runner。由于我不熟悉持续集成服务器,因此不知道如何完成以下操作:
每次我提交到项目的主分支时,我想将仓库部署到另一台服务器并在那里运行两个shell命令 (npm install
和forever restartall
)。
我该怎么做?我需要在部署项目的机器上也安装一个runner吗?
我已经搭建好了自己的Gitlab服务器,其中有一个项目和为其配置好的Gitlab runner。由于我不熟悉持续集成服务器,因此不知道如何完成以下操作:
每次我提交到项目的主分支时,我想将仓库部署到另一台服务器并在那里运行两个shell命令 (npm install
和forever restartall
)。
我该怎么做?我需要在部署项目的机器上也安装一个runner吗?
您可以使用gitlab-ci和gitlab-runner [runners.ssh]将应用部署到单个或多个服务器。
具体流程如下:
(git_project with yml file) --> (gitlab && gitlab-ci) --> (gitlabrunner) ---runners.ssh---> (deployed_server,[deploye_server2])
你需要将gitlab-runner注册到gitlab-ci,并在gitlab网站上设置标签为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 的意思是,该 runner 将登录到 ${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 设置为 'deployServer1
' 和 'deployServer2
' 标签,并在 'http://your.gitlab.server/ci/admin/runners' 中配置。
.gitlab-ci.yml
文件,选择一个带有标签 "deployServer1
" 或 "deployServer2
" 的 Runner;gitlab-runner
将使用 SSH 登录到 ${the_destionation_of_deployServer_IP1}
和 ${the_destionation_of_deployServer_IP2}
,将项目克隆到 builds_dir
,然后执行您的脚本:npm install && forever restartall。链接:
build
阶段添加到你的.gitlab-ci.yml
文件中。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不能直接进行部署(尽管您可以安装 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
例如)
$HEROKU_STAGING_API_KEY
,然后在项目的“设置”->“CI/CD”中添加受保护的环境变量。 - Alex
/home/change_me/.ssh/id_rsa
)配置ssh密钥文件。在此之前,您必须将公钥内容附加到运行服务器的~/.ssh/authorized_keys
文件中。 ssh密钥位于运行服务器和部署服务器之间:(运行服务器中的私有ssh密钥) --> (带有ssh公钥的部署服务器)
- michaelidentity_file = "/home/user_name/.ssh/id_rsa"
。当我加上之前的那一行时,运行程序就无法工作并显示相同的错误消息,在我的/etc/ssh/sshd_config
文件中,我放置了AuthorizedKeysFile /home/user_name/.ssh/authorized_keys
和HostKey /home/user_name/.shh/id_rsa
。为什么运行程序不使用 identity_file?我做错了什么吗? - MMax