10/16/18

Make travis to push tags

What is travis you already know from the previous post. But what the kubernetes is?
According to the docs: "Kubernetes is a portable, extensible open-source platform for managing containerized workloads and services, that facilitates both declarative configuration and automation. It has a large, rapidly growing ecosystem. Kubernetes services, support, and tools are widely available"
Get the long things shorter: kubernetes is the way to upload applications to google cloud, and also it can do lots of other things.

My Goal

So, my goal was: to get things published to cloud after all the testing stuff was done. (or after the travis finished)
Google cloud builder - another google cloud tool can be activated in response to push to some branch or in response to publishing some tag

In other words - my goal is to make travis to push tag after successful testing of every commit.

After Success Hook

Fortunately I found that travis has a after_success hook which I can utilise for my scenario:


language: node_js
node_js:
  - "9"
sudo: required
branches:
  except:
    - /^v.*\_.*/ 
dist: trusty
addons:
  chrome: stable
cache:
  directories:
  - $HOME/.npm
env:
  matrix:
    - MONGODB=3.6.6
before_install:
  - export GITHUBKEY=put-here-your-github-auth-token-key
install:
  ...
  ...
script:
  # run build script specified in package.json
  - npm run tslint
  - npm run test
  - npm run build
after_success:
  # CREATE GIT TAG
  - bash ./scripts/push_tag.sh

And here is the bash script code:

#!/bin/sh
git config --global user.email "koko@moko.com"
git config --global user.name "koko"
# Version key/value should be on his own line
PACKAGE_VERSION=$(cat package.json \
  | grep version \
  | head -1 \
  | awk -F: '{ print $2 }' \
  | sed 's/[",]//g' \
  | tr -d '[[:space:]]')

# should start cloud build only on prod test dev branches 
if [[ "${TRAVIS_BRANCH}" = prod || "${TRAVIS_BRANCH}" = test || "${TRAVIS_BRANCH}" = dev ]]; then
    export GIT_TAG="v${PACKAGE_VERSION}_${TRAVIS_BUILD_NUMBER}"
    echo "tag is ${GIT_TAG}"
    git tag $GIT_TAG -a -m "Generated tag from TravisCI build $TRAVIS_BUILD_NUMBER [ci skip]" $TRAVIS_BRANCH
    git push --tags https://$GITHUBKEY@github.com/oyrname/yourrepo
fi

Notice, that travis will react to the tag pushed by himself unless except entry configured properly:

branches:
  except:
    - /^v.*\_.*/

10/15/18

Making travis to test nodejs app

Tra... what?

May be you have noticed .travis.yml file in some of open source GitHub repos. Travis tool - is a CI tool for github. CI - means continuous integration, which in turn - means that after every change of code some checks will be run automatically. It can be unit test, can be code standard checks (like lint) or whatever you want to be checked.

My Situation

In my case - In our company we have some nodejs + typescript + mongo application which has mocha/chai unit tests (pretty common stack nowdays).
Here is basic travis configuration which runs tslint and unit tests


language: node_js
node_js:
  - "9"
sudo: required
dist: trusty
addons:
  chrome: stable
cache:
  directories:
  - $HOME/.npm
env:
  matrix:
    - MONGODB=3.6.6
install:
  - wget http://fastdl.mongodb.org/linux/mongodb-linux-x86_64-${MONGODB}.tgz
  - tar xzf mongodb-linux-x86_64-${MONGODB}.tgz
  - ${PWD}/mongodb-linux-x86_64-${MONGODB}/bin/mongod --version
  - npm install
before_script:
  - mkdir ${PWD}/mongodb-linux-x86_64-${MONGODB}/data
  - ${PWD}/mongodb-linux-x86_64-${MONGODB}/bin/mongod --dbpath ${PWD}/mongodb-linux-x86_64-${MONGODB}/data --logpath ${PWD}/mongodb-linux-x86_64-${MONGODB}/mongodb.log --fork
script:
  # run build script specified in package.json
  - npm run tslint
  - npm run test
  - npm run build

Notice that mongo can be installed at testing machine travis creates each time when tests are run. Mongo is needed for run unit tests

8/31/18

magic of the dontenv

Recently i tried to solve the following problem: how to get react app to get backend Url from environment variable. The embarrassing truth is that until now my code was full of statements like this:

    fetch('http://localhost:8000/login', {
        method: "POST",
        credentials: 'include',
        headers: {
            "Content-Type": "application/json; charset=utf-8"
        },
        body: JSON.stringify( data )
    })
As you may notice - the url is hardcoded. The problem here is that this code will run only at developer environment where server running on 127.0.0.1 ("localhost") ip. But what about production environment where backend url can be http://yourappname.herokuapp.com ?

DONTENV

It appears the very cool solution for this problem is to use dotenv node plugin. Using it give us ability to define all environment specific variables in .nev file:

REACT_APP_API_HOST=http://localhost:8000
Now inside your react code you can access this variable this way:

 const requestOptions = {
        method: 'POST',
        headers: { 'Content-Type': 'application/json' },
        body: JSON.stringify({ username, password })
 };
 fetch(`${process.env.REACT_APP_API_HOST}/login`, requestOptions)
        .then(handleResponse)

CREATE REACT APP

Create react app tool supports using .env files (so there is no need to install dotenv). You can create different .env files for different environments: like .env.local and .env.production.
By default - the "serve"(npm start) task will use "local", while "build" (npm run build) - will use production... Cool!

Getting started with docker

It is very simple to get started usig docker. All you need to do-is download the docker desktop for your system Once you get docker syste...