Build, test and deploy your Vue.js app easily with Gitlab


Continuous integration allows you to :

  • Deploy your app instantly, when new code is pushed into a repo
  • Build your app (in our case npm run build)
  • Trigger test scripts (and block deployment if a test fails)

It is definetely worth the effort if you update your app regularly.

Gitlab is a service that started as an open-source Github competitor, mostly to host code in Git repositories, and evolved into an amazing tool that I won’t introduce here, as it isn’t related to Vue.js. One thing though, they were one of the first major companies that uses Vue.js for their user interface.

Docker has to be mentioned as well. It is the most popular containerization service. It basically means you get to execute code in a secure environment, configured exactly like your dev/prod. Very useful when you need to make sure your code is executed with all its dependencies.

Each of these tools would require many posts to be covered. We’ll focus on setting up CI for your Vue.js project. We’ll assume you have no knowledge in the matter.

Gitlab CI is free for personal projects, I don’t know any other tool with such a beautiful UI that does that. If you do, please let me know.

The .gitlab-ci.yml file

Create a .gitlab-ci.yml file at the root of your repo. Gitlab will check for this file when new code is pushed. If the file is present, it will define a pipeline, executed by a Gitlab Runner. Click the links if you are curious, or keep reading to see a working example.

Default stages of a pipeline are :

  1. build
  2. test
  3. deploy

Again, you don’t need to master this, but this is the most common use case. You may not have set up unit tests, and if you haven’t, you may remove this step from the file, Gitlab won’t mind.

Here is our file, you may copy/paste it in your repo :

build site:
  image: node:6
  stage: build
  script:
    - npm install --progress=false
    - npm run build
  artifacts:
    expire_in: 1 week
    paths:
      - dist

unit test:
  image: node:6
  stage: test
  script:
    - npm install --progress=false
    - npm run unit

deploy:
  image: alpine
  stage: deploy
  script:
    - apk add --no-cache rsync openssh
    - mkdir -p ~/.ssh
    - echo "$SSH_PRIVATE_KEY" >> ~/.ssh/id_dsa
    - chmod 600 ~/.ssh/id_dsa
    - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
    - rsync -rav --delete dist/ :/your/project/path/

Test our file

Now commit and push the .gitlab-ci.yml file to your Gitlap repo.

Here is how it will look in the Pipelines tab of Gitlab UI :

The green checkmark indicates that the step has succeeded and you can see the logs when clicking it.

In the second example, the tests have failed, click the red mark to read the logs and understand what went wrong.

File anatomy

  • image is the link to the Docker image. I have chosen to use public official images, but you may use one from the Docker Hub or a private registry.

  • stage should be build, test or deploy if you use defaults. But that can be customised.

  • script are command lines executed inside our build environment.

  • artifacts describes a path to the build result. The files in this path can be used in the next build steps (in deploy in our example). You can download artifacts from Gitlab UI.

More about the .gitlab-ci.yml file options in the docs.

About the deployment script

I have described my use case here, but it may not be the simpliest. Relevant examples for deployment to Amazon S3 or other services can be found online.

To get it working, you’ll need to provide Gitlab a private SSH key. If you are no security expert, then it is time to take advice from one. The bottom line is do not give it your private SSH key, create one that is used only by Gitlab.

# create gitlab user
adduser gitlab

# generate a DSA SSH key
su -l gitlab
ssh-keygen -t dsa

# authorize the key to log in using the public key and output the private one
cd .ssh
mv id_dsa.pub authorized_keys
cat id_dsa && rm id_dsa

Then go to Gitlab UI “Settings” (the gear icon), then “Variables” and copy/paste the content of your terminal in “Value”. The “Key” should be SSH_PRIVATE_KEY. This private key will be used to do the rsync.

If you need more information, leave a comment I’ll be happy to help you if I can.

Latest posts

Use Vue.js to create custom web components

Include Vue.js components in any HTML/JS application

Use a global event bus instead of a state manager

Emit and listen to events across every Vue components of your app

Vue.js Component Style Guide

General purpose recommandations for writing Vue.js components code

How to use Docker containers for Vue.js applications

Get started with Docker and Vue.js

Hide elements during loading using "v-cloak"

You probably need this directive and didn't know it

Using Bootstrap with Vue.js

Question is : do you really need Boostrap ?

Another way to declare Vuex modules

`vuex-module` provides a syntax to write your store modules' state, actions, getters, etc. You may want to use it.

Simple state management, simpler than Vuex

Vue Stash makes it easy to share reactive data between components. An alternative to Vuex.

Hybrid Vue.js app in Cordova : desktop or mobile ?

A simple hack to know if the code is executed in a website or in the Cordova version of your app

Does Vue.js require jQuery ?

No it doesn't. But you might find it useful, here's why.