Deploy every build to a server using Gitlab CI

2019-01-30 12:14发布

问题:

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?

回答1:

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:

  • gitlab-runner register
  • runners.ssh


回答2:

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)