一尘不染

使用Gitlab CI将每个版本部署到服务器

node.js

我已经设置了一个项目,并为其配置了一个Gitlab运行器,以设置自己的Gitlab服务器。我是持续​​集成服务器的新手,因此不知道如何完成以下任务。

每次我提交到项目的master分支时,我都希望将存储库部署到另一台服务器,并在其中运行两个shell命令(npm installforeverrestartall)。

我该怎么做?我也需要在部署项目的机器上安装运行程序吗?


阅读 384

收藏
2020-07-07

共1个答案

一尘不染

您可以使用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,并将标记设置为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手段,亚军将登录到${the_destionation_of_deployServer_IP1}${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
  1. 将您的gitlab-runner设置为,delpoyServer1delpoyServer2在’ http://your.gitlab.server/ci/admin/runners

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

链接:

2020-07-07