[DevOps] Build and Test Nativescript Project with Gitlab-CI

[DevOps] Build and Test  Nativescript Project with Gitlab-CI

To get more things Done we need more time, and to get more time there are things you must do, one of them is the automation.

Did you hear about DevOps? Yeah, me too, here's what Amazon says about :

DevOps is the combination of cultural philosophies, practices, and tools that increase an organization’s ability to deliver applications and services at high velocity

When I start coding in a new project the first thing I do is to prepare the git repository, there are many services allow you to host your repositories int they servers, the Famous one is Github but you need a membership to host private repositories, like all developers I start looking for an Alternatives until I found Gitlab , There's an open source version (Community) and  I installed on my oven server but It took all of the memory that I have, so I stick with the hosting they provide.

Everything seems Ok until start working with NativeScript and Angular to build mobile Apps, in the DEV world there's always more than environment, at least we have a development environment and production environment when you are in dev environment, you can't know how your code or the plugin that you've just installed gonna behave or will it work in prod environment.

Here there are two main roads, build the application each time I add a NativeScript plugin or code for days then build the App and I found out if everything seems Ok, If not,  you maybe need to change some plugins and rewrite a bunch of lines of code.

The solution?

While browsing the web I found some services contain a CI into they names, like CircleCI , Travis CI , and others, I got curious until I got deep into the subject, And found out that these are tools for Continuous Integration and delivery.

How does it work?

To Deliver a good product you need to do your test the app, by writing the unit tests or/and real humans interaction, you need to run your unit tests each time you add code to find out if you or somebody else broke the app.

Ok, but running tests and building each time in your machine will reduce the performance of your development environment, and for sure will make your nervous, How about building in somebody else machine? that's when I start looking for alternatives solutions, in the meanwhile Gitlab introduce the Continuous Integration feature, and it's for FREE.

Now when I (or anyone else in the same repository ) push a commit, Gitlab will run the test, build and generate an APK file (for Android) for me and show me If there are issues white building.

I will share with you how I've Done it.

You can achieve this workflow by adding .gitlab-ci.yml

image: runmymind/docker-android-sdk:ubuntu-standalone
stage: build
- master
- git submodule update --init --recursive
- wget -q http://nodejs.org/dist/v${NODE_VERSION}/node-v${NODE_VERSION}-linux-x64.tar.gz
- tar -xzf node-v${NODE_VERSION}-linux-x64.tar.gz
- mv node-v${NODE_VERSION}-linux-x64 /opt/node
- rm node-v${NODE_VERSION}-linux-x64.tar.gz
- PATH=${PATH}:/opt/node/bin
- npm install -g nativescript --unsafe-perm
- npm install nativescript --unsafe-perm
- $ANDROID_HOME/tools/bin/sdkmanager "tools" "platform-tools" "platforms;android-26" "build-tools;26.0.1" "extras;android;m2repository" "extras;google;m2repository" > /dev/null
- npm i
- echo $ANDROID_KEY_BASE64 | base64 -d > bsc-release-key.jks
- tns build android --bundle --env.snapshot --env.aot
- platforms/android/app/build/outputs/apk/
view raw .gitlab-ci.yml hosted with ❤ by GitHub

Let's break this file down.

First, we created a Job, then we pull a Docker image that contains a fresh Installed version of Android.

Then we installed the Node.js, after that, we installed NativeScript, updating the Android SDK and installing Node modules with NPM.

When everything is done, we build the App with NativeScript CLI (command line tools).

In the build process of NativeScript we pass by the unit tests, if there's an error it will be thrown with an Exception.

If there's no error and everything is fine we will get our APK available to download, This all happens while you're writing your code in peace, you didn't stress your machine but you got visibilities about what's gonna happen when you build for production.

Isn't a big deal? how do you think?

Last modified: May 19, 2018