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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33768537/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 19:33:02  来源:igfitidea点击:

Deploy every build to a server using Gitlab CI

node.jscontinuous-integrationgitlabgitlab-cigitlab-ci-runner

提问by Hedge

I've set up my own Gitlab server with one project and a Gitlab runner configured for it. I'm new to continuous integration server and therefore don't know how to accomplish the following.

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

Every time I commit to the master branch of my project I would like to deploy the repository to another server and run two shell-commands there (npm installand forever restartall)

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

How would I do this? Do I need a runner on the machine which the project is deployed to as well?

我该怎么做?我是否也需要在项目部署到的机器上运行一个运行程序?

回答by michael

You could use gitlab-ci and gitlab-runner [runners.ssh] to deploy to single or mutiple servers.

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

the flow:

流程:

(git_project with yml file)  --> (gitlab && gitlab-ci) --> (gitlabrunner) ---runners.ssh---> (deployed_server,[deploye_server2])
  1. you need register gitlab-runner to gitlab-ci and set the tag to 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"
    
  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"
    

the runner.ssh means, the runner will login into ${the_destionation_of_deployServer_IP1}and ${the_destionation_of_deployServer_IP2}, then clone the project to builds_dir.

runner.ssh 意味着,runner 将登录${the_destionation_of_deployServer_IP1}${the_destionation_of_deployServer_IP2},然后将项目克隆到builds_dir

  1. write the yml file for example: .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. set the your gitlab-runner to delpoyServer1and delpoyServer2tags in 'http://your.gitlab.server/ci/admin/runners'

    • when you push you code to gitlab
    • the gitlab-ci server will parser your .gitlab-ci.ymlfile in your project, choose a runner with the tags: deployServer1or deployServer2;
    • the gitlab-runnerwith the deployServer1 tag will login into ${the_destionation_of_deployServer_IP1}and ${the_destionation_of_deployServer_IP2}with ssh , clone the project to builds_dir, then execute you script: npm install && forever restartall.
  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,转轮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。

link:

关联:

回答by Alex

You should be able to use gitlab-ci.yml documentationto add a separate buildstage into your .gitlab-ci.ymlfile.

您应该能够使用gitlab-ci.yml 文档将单独的build阶段添加到您的.gitlab-ci.yml文件中。

You will need some sort of deploy service (like capistranoor similar), or a webhook that'll initiate a deployment.

您将需要某种部署服务(类似capistrano或类似),或将启动部署的 webhook。

I.e. something like:

即类似:

---
stages:
  - test
  - deploy

job_runtests:
  stage: test
  script:
    - npm test

job_deploy:
  stage: deploy
  script:
    - curl -X POST https://deploymentservice.io/?key=

Gitlab CI will iterate through each stage it finds, running them sequentially. If a stage passes, then it moves on to the next.

Gitlab CI 将遍历它找到的每个阶段,依次运行它们。如果一个阶段过去了,那么它就会进入下一个阶段。

Unfortunately Gitlab CI can't do deployment directly (although you can install the dplRuby Gem and call that in your .gitlab-ci.ymlfile like so:

不幸的是,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

for example)

例如)