26

我已经建立了自己的 Gitlab 服务器,其中包含一个项目和一个为其配置的 Gitlab 运行器。我是持续​​集成服务器的新手,因此不知道如何完成以下工作。

每次我提交到项目的主分支时,我都想将存储库部署到另一台服务器并在那里运行两个 shell 命令(npm installforever restartall

我该怎么做?我是否也需要在项目部署到的机器上安装一个跑步者?

4

2 回答 2

34

您可以使用 gitlab-ci 和 gitlab-runner [runners.ssh] 部署到单个或多个服务器。

流程:

(git_project with yml file)  --> (gitlab && gitlab-ci) --> (gitlabrunner) ---runners.ssh---> (deployed_server,[deploye_server2])
  1. 您需要将 gitlab-runner 注册到 gitlab-ci 并将标签设置为 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 的意思是,跑步者将登录到${the_destionation_of_deployServer_IP1}and ${the_destionation_of_deployServer_IP2},然后将项目克隆到builds_dir.

  1. 编写 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
    
  2. 将您的 gitlab-runner 设置为“ http://your.gitlab.server/ci/admin/runners ”中delpoyServer1delpoyServer2标签

    • 当你将代码推送到 gitlab
    • gitlab-ci 服务器将.gitlab-ci.yml在您的项目中解析您的文件,选择带有标签的运行器:deployServer1deployServer2
    • 带有 deployServer1 标记的gitlab-runner将登录${the_destionation_of_deployServer_IP1}${the_destionation_of_deployServer_IP2}使用 ssh ,将项目克隆到builds_dir,然后执行您的脚本:npm install && forever restartall。

关联:

于 2015-11-21T06:46:21.203 回答
23

您应该能够使用gitlab-ci.yml 文档将单独的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 不能直接进行部署(尽管您可以安装Ruby Gem 并在文件dpl中调用它,如下所示:.gitlab-ci.yml

job_deploy:
  - gem install dpl
  - dpl --provider=heroku --app=my-app-staging --api-key=$HEROKU_STAGING_API_KEY
only:
  - master

例如)

于 2015-11-20T15:48:48.603 回答