Deploy every build to a server using Gitlab CI

23,976

Solution 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:

Solution 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)

Share:
23,976

Related videos on Youtube

Hedge
Author by

Hedge

Updated on June 23, 2020

Comments

  • Hedge
    Hedge almost 4 years

    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?

  • MMax
    MMax almost 8 years
    Hi! With ssh executor the runner will use the Deployment keys? I always get the same error:gitlab-ci-multi-runner 1.3.3 (6220bd5) WARNING: image is not supported by selected executor and shell WARNING: services is not supported by selected executor and shell Using SSH executor... ERROR: Build failed: open /home/user/.ssh/id_rsa: no such file or directory
  • michael
    michael almost 8 years
    Yes, your should config your ssh key file with your real path of ssh private key(e.g: /home/change_me/.ssh/id_rsa . Before that you must append the the public key content to ~/.ssh/authorized_keys file in the runner server's. The ssh key is between runner server and deloy servers : (private ssh key in runner server) --> (deploy servers with ssh public key)
  • MMax
    MMax almost 8 years
    thanks for the help, was very helpfull. Now it´s working :) But for my runner work proprely i remove this line identity_file = "/home/user_name/.ssh/id_rsa". When i put the previous line the runner doesn´t work and show the same error message, in my /etc/ssh/sshd_config file i put AuthorizedKeysFile /home/user_name/.ssh/authorized_keys and HostKey /home/user_name/.shh/id_rsa. Why the runner doesn´t work with the identity_file? i´m doing something wrong?
  • michael
    michael almost 8 years
    In short we must make the runner connect to deploy server with some way. I this case we use ssh-path. If your runner work properly when remove identify_file , Did you config the password ? You can get the detail from (the link)[gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/mast‌​er/docs/…. If not, I really cannot understand that unless you show your topology of CI servers and your configuration.
  • Nguyen Tran
    Nguyen Tran over 7 years
    Thanks for your answer. For the first deploy it run npm install and forever start OK. But once I made a new commit, the CI deploy and I got an error failed to remove node_modules/.... Do you have any idea about my situation?
  • Cynthia Sanchez
    Cynthia Sanchez almost 7 years
    I have a question, the --api-key is attached to my account in heroku, so if I have an open sourced project in gitlab anyone could deploy to my heroku servers. How can I make sure that only MY gitlab.ci can deploy with my api-key?
  • Alex
    Alex over 5 years
    You can add private project variables in Gitlab settings - leave the .gitlab-ci.yml file to reference (literally) $HEROKU_STAGING_API_KEY and then in Settings -> CI/CD for your project, add a protected environment variable.