Monday, May 01, 2017

Continuous Integration/Deployment with Meteor on GitLab CI

In a previous post, I wrote about implementing Continuous Integration/Deployment using Codeship, Docker and Amazon ECS. The project I'm currently working on is hosted on GitLab. GitLab has CI/CD services for free so I set out to take advantage of that.

As usual, there are a several moving parts involved in getting things configured. This article will not cover everything in detail so you should already be familiar with:


Make sure your work the kinks out of your test and deployment scripts on your local machine before you attempt to configure CI/CD. It can take 10+ minutes to run a job so the debug cycle is time-consuming. It's more efficient to get things working locally where it's easy to make adjustments and re-run a job. Taking a test/deployment process that works on your local machine to GitLab CI is fairly straightforward process of setting up a .gitlab-ci.yml configuration file.
image: blurri/meteor-node

variables:
  METEOR_ALLOW_SUPERUSER: "true"
  METEOR_WATCH_FORCE_POLLING: "true"
  NODE_ENV: 'development'

stages:
  - test
  - deploy

test:
  stage: test
  only:
    - master
    - develop
  script:
    - meteor npm install chimp
    - meteor npm install
    - meteor npm rebuild
    - meteor npm run test-for-ci

deploy:
  stage: deploy
  only:
    - master
    - develop
  when: manual
  script:
    # Install ssh-agent if not already installed, it is required by Docker.
    # (change apt-get to yum if you use a CentOS-based image)
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'

    # Run ssh-agent (inside the build environment)
    - eval $(ssh-agent -s)

    # Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    - ssh-add <(echo "$SSH_PRIVATE_KEY")

    # For Docker builds disable host key checking. Be aware that by adding that
    # you are suspectible to man-in-the-middle attacks.
    # WARNING: Use this only with the Docker executor, if you use it with shell
    # you will overwrite your user's SSH config.
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    # write the ssh key to a file
    - echo "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa

    # install pm2-meteor
    - meteor npm install pm2-meteor -g

    - meteor npm install
    - meteor npm rebuild
    - meteor npm run deploy

Testing

If you are using Chimp to run full acceptance tests, you are probably used to running your Meteor in one terminal and executing the tests in another terminal. When running under a CI pipeline, you'll want to make it so you can execute the test in one script. This discussion has an example of how to do just that. The test script simply installs chimp, installs any packages from package.json, rebuilds and then executes the tests.

Deployment

When deploying with pm2-meteor, you'll need to pass a path to your ssh key so the script can connect to the remote server. This article, provided the information to do that. Once the ssh connection is established, the deploy script can be run.

Good luck setting up your CI/CD pipeline!

Resources: