Deploy every build to a server using Gitlab CI

Hedge picture Hedge · Nov 17, 2015 · Viewed 23.2k times · Source

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.

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)

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

Answer

michael picture michael · Nov 21, 2015

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: