首页 文章

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

提问于
浏览
22

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

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

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

2 回答

  • 29

    您可以使用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 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 .

    • 编写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设置为'http://your.gitlab.server/ci/admin/runners'中的 delpoyServer1delpoyServer2 标签

    • 当你将代码推送到gitlab时

    • gitlab-ci服务器将解析您项目中的 .gitlab-ci.yml 文件,选择带有标签的跑步者: deployServer1deployServer2 ;

    • 带有deployServer1标签的 gitlab-runner 将使用ssh登录 ${the_destionation_of_deployServer_IP1}${the_destionation_of_deployServer_IP2} ,将项目克隆为 builds_dir ,然后执行脚本:npm install && forever restartall .

    链接:

  • 21

    您应该能够使用gitlab-ci.yml documentation将单独的 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
    

    例如)

相关问题