Deploy every build to a server using Gitlab CI

吃可爱长大的小学妹 提交于 2019-11-29 20:12:04
michael

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

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"
    

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.

  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 delpoyServer1 and 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.yml file in your project, choose a runner with the tags: deployServer1 or 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.

link:

You should be able to use gitlab-ci.yml documentation to add a separate build stage into your .gitlab-ci.yml file.

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

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.

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

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

for example)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!