使用Gitlab CI将每个构建部署到服务器

Hed*_*dge 24 continuous-integration node.js gitlab gitlab-ci gitlab-ci-runner

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

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

我该怎么做?我是否需要在部署项目的机器上使用转轮?

mic*_*ael 30

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

流程:

(git_project with yml file)  --> (gitlab && gitlab-ci) --> (gitlabrunner) ---runners.ssh---> (deployed_server,[deploye_server2])
Run Code Online (Sandbox Code Playgroud)
  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"
    
    Run Code Online (Sandbox Code Playgroud)

在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
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在' http://your.gitlab.server/ci/admin/runners '中设置你的gitlab-runner delpoyServer1delpoyServer2标签

    • 当你把代码推送到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.

链接:


Ale*_*lex 21

您应该能够使用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=
Run Code Online (Sandbox Code Playgroud)

Gitlab CI将遍历它找到的每个阶段,按顺序运行它们.如果一个阶段通过,那么它将继续前进到下一个阶段.

不幸的是,Gitlab CI不能直接进行部署(尽管你可以安装dplRuby Gem并在你的.gitlab-ci.yml文件中调用它,如下所示:

job_deploy:
  - gem install dpl
  - dpl --provider=heroku --app=my-app-staging --api-key=$HEROKU_STAGING_API_KEY
only:
  - master
Run Code Online (Sandbox Code Playgroud)

例如)