Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Gitlab CI/CD to deploy a meteor project?

As claimed at their website Gitlab can be used to auto deploy projects after some code is pushed into the repository but I am not able to figure out how. There are plenty of ruby tutorials out there but none for meteor or node.

Basically I just need to rebuild an Docker container on my server, after code is pushed into my master branch. Does anyone know how to achieve it? I am totally new to the .gitlab-ci.yml stuff and appreciate help pretty much.

like image 293
user1255102 Avatar asked Mar 30 '16 14:03

user1255102


People also ask

Can GitLab be used for CI CD?

all tiers. GitLab CI/CD is a tool for software development using the continuous methodologies: Continuous Integration (CI) Continuous Delivery (CD)

How CI CD pipeline works in GitLab?

Description. GitLab CI (Continuous Integration) service is a part of GitLab that build and test the software whenever developer pushes code to application. GitLab CD (Continuous Deployment) is a software service that places the changes of every code in the production which results in every day deployment of production.


1 Answers

Brief: I am running a Meteor 1.3.2 app, hosted on Digital Ocean (Ubuntu 14.04) since 4 months. I am using Gitlab v. 8.3.4 running on the same Digital Ocean droplet as the Meteor app. It is a 2 GB / 2 CPUs droplet ($ 20 a month). Using the built in Gitlab CI for CI/CD. This setup has been running successfully till now. (We are currently not using Docker, however this should not matter.)

Our CI/CD strategy:

  1. We check out Master branch on our local laptop. The branch contains the whole Meteor project as shown below:

enter image description here

We use git CLI tool on Windows to connect to our Gitlab server. (for pull, push, etc. similar regular git activities)

  1. Open the checked out project in Atom editor. We have also integrated Atom with Gitlab. This helps in quick git status/pull/push etc. within Atom editor itself. Do regular Meteor work viz. fix bugs etc.

  2. Post testing on local laptop, we then do git push & commit on master. This triggers auto build using Gitlab CI and the results (including build logs) can be seen in Gitlab itself as shown below:

enter image description here

Below image shows all previous build logs:

enter image description here

Please follow below steps:

  1. Install meteor on the DO droplet.

  2. Install Gitlab on DO (using 1-click deploy if possible) or manual installation. Ensure you are installing Gitlab v. 8.3.4 or newer version. I had done a DO one-click deploy on my droplet. Start the gitlab server & log into gitlab from browser. Open your project and go to project settings -> Runners from left menu

enter image description here

  1. SSH to your DO server & configure a new upstart service on the droplet as root:

    vi /etc/init/meteor-service.conf
    

Sample file:

#upstart service file at /etc/init/meteor-service.conf
description "Meteor.js (NodeJS) application for eaxmple.com:3000"
author "[email protected]"

# When to start the service
start on runlevel [2345]

# When to stop the service
stop on shutdown

# Automatically restart process if crashed
respawn
respawn limit 10 5

script
    export PORT=3000
    # this allows Meteor to figure out correct IP address of visitors
    export HTTP_FORWARDED_COUNT=1
    export MONGO_URL=mongodb://xxxxxx:[email protected]:59672/meteor-db
    export ROOT_URL=http://<droplet_ip>:3000
    exec /home/gitlab-runner/.meteor/packages/meteor-tool/1.1.10/mt-os.linux.x86_64/dev_bundle/bin/node /home/gitlab-runner/erecaho-build/server-alpha-running/bundle/main.js >> /home/gitlab-runner/erecaho-build/server-alpha-running/meteor.log
end script
  1. Install gitlab-ci-multi-runner from here: https://gitlab.com/gitlab-org/gitlab-ci-multi-runner/blob/master/docs/install/linux-repository.md as per the instructions Cheatsheet:

    curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
    sudo apt-get install gitlab-ci-multi-runner
    sudo gitlab-ci-multi-runner register
    

    Enter details from step 2

  2. Now the new runner should be green or activate the runner if required

  3. Create .gitlab-ci.yml within the meteor project directory

    Sample file:

    before_script:
      - echo "======================================"
      - echo "==== START auto full script v0.1 ====="
      - echo "======================================"
    
    types:
      - cleanup
      - build
      - test
      - deploy
    
    job_cleanup:
      type: cleanup
      script:
      - cd /home/gitlab-runner/erecaho-build
      - echo "cleaning up existing bundle folder"
      - echo "cleaning up current server-running folder"
      - rm -fr ./server-alpha-running
      - mkdir ./server-alpha-running
      only:
      - master
      tags:
      - master
    
    job_build:
      type: build
      script:
      - pwd
      - meteor build /home/gitlab-runner/erecaho-build/server-alpha-running --directory --server=http://example.org:3000 --verbose
      only:
      - master
      tags:
      - master
    
    job_test:
      type: test
      script:
      - echo "testing ----"
      - cd /home/gitlab-runner/erecaho-build/server-alpha-running/bundle
      - ls -la main.js
      only:
      - master
      tags:
      - master
    
    job_deploy:
      type: deploy
      script:
      - echo "deploying ----"
      - cd /home/gitlab-runner/erecaho-build/server-alpha-running/bundle/programs/server/ && /home/gitlab-runner/.meteor/packages/meteor-tool/1.1.10/mt-os.linux.x86_64/dev_bundle/bin/npm install
      - cd ../..
      - sudo restart meteor-service
      - sudo status meteor-service
      only:
      - master
      tags:
      - master
    
  4. Check in above file in gitlab. This should trigger Gitlab CI and after the build process is complete, the new app will be available @ example.net:3000

Note: The app will not be available after checking in .gitlab-ci.yml for the first time, since restart meteor-service will result in service not found. Manually run sudo start meteor-service once on DO SSH console. Post this any new check-in to gitlab master will trigger auto CI/CD and the new version of the app will be available on example.com:3000 after the build is completed successfully.

P.S.: gitlab ci yaml docs can be found at http://doc.gitlab.com/ee/ci/yaml/README.html for your customization and to understand the sample yaml file above. For docker specific runner, please refer https://gitlab.com/gitlab-org/gitlab-ci-multi-runner

like image 123
roray Avatar answered Oct 02 '22 09:10

roray