diskusi.tech (beta) Community

loading...

Deliver Product Faster with CI/CD -- Example Included

andhikayusup profile image andhikayusup ・2 min read

We are already know who is the first human landed on the moon; it's Neil Armstrong, right? But, can you tell me who is the second person who steps his/her foot on the moon?

Being the first in the market is what matter. But, being the first does not means delivering unfinished product. The product that we launch should have value and usable for the target user. Both of this thing can be achieved with CI.

Continuous Integration

Continuous Integration (CI) is the way to automate the process of integrating frequent changes from multiple developers. Changes in the source code will be tested and build automatically. The usual steps in CI/CD is shown as the following:

  1. Developers commit changes to GIT repository
  2. CI will test the code first
  3. if the tests passed, the current changes may be deployed
  4. if the tests fails, the current changes will not be deployed until it fixed on the next committed changes.

If you are using Gitlab, you might familiar with gitlab-ci.yml. This file is written in YAML and contains the instruction of what should it execute.

stages:
  - test
  - analysis
  - deploy

test:
  stage: test
  image: python:3.6

  before_script:
    - python -V
    - pip install -r requirements.txt

  services:
    - mongo

  variables:
    MONGO_URI: 'mongodb://mongo:27017/siveri'
    NODE_ENV: 'test'

  script:
    - python manage.py makemigrations
    - python manage.py migrate
    - python manage.py collectstatic --no-input
    - python manage.py test
    - coverage run --omit='.env/*,env/*,project/*,*/tests.py,manage.py' manage.py test
    - coverage report -m

black:
  stage: analysis
  image: milansuk/python-black:3.8
  script:
    - black --version
    - black --diff .
    - black --check .


staging:
  stage: deploy
  image: ruby:latest
  before_script:
    - gem install dpl
    - wget -qO- https://cli-assets.heroku.com/install-ubuntu.sh | sh
  script:
    - dpl --provider=heroku --app=$HEROKU_APP_NAME --api-key=$HEROKU_API_KEY
    - heroku run --app $HEROKU_APP_NAME python manage.py makemigrations
    - heroku run --app $HEROKU_APP_NAME python manage.py migrate --run-syncdb
  only:
    - staging
  environment:
    name: staging
    url: $HEROKU_APP_HOST

production:
  stage: deploy
  image: docker:latest
  services:
    - docker:dind
  before_script:
    - echo $CI_BUILD_TOKEN | docker login -u $CI_REGISTRY_USER --password-stdin $CI_REGISTRY
  script:
    - docker build -t $CI_REGISTRY_IMAGE -f Dockerfile .
    - docker push $CI_REGISTRY_IMAGE
  only:
    - master
  environment:
    name: production
Enter fullscreen mode Exit fullscreen mode

Deployment Environment

To be able to reap Continuous Integration full potential, we have to follow available best practices. One of them is by having special branches called staging and production.

Discussion

pic
Editor guide