Continuous Integration And Continuous Delivery With Gitlab And Docker

Reading Time : ~ .

Continuous integration and continuous deployment has become one of the most common use cases of Docker early adopters.
CI/CD merges development with testing, allowing developers to build code collaboratively, submit it the master branch,
and checked for issues. This allows developers to not only build their code, but also test their code in any environment type
and as often as possible to catch bugs early in the applications development lifecycle.

- An excerpt from Docker

In my previous blog post,  we have configured omnibus Gitlab Installation with CI Enabled, In this blog post, we will see how we can test - build - deploy code using gitlab and docker.

Test:

Add a new file ".gitlab-ci.yml" in your local git repository root level and add in following code

test:
    image: <docker-image-to-perform-operations-on>
    script:
      - step-1
      - step-2
      - step-3

Note: Steps are shell commands:

If you have CI configured as per my previous tutorial, you should see your project code being tested in docker image you mentioned by executing script section commands.

example:

test:
    image: python:2.7
    script:
      - pip install -r requirements.txt
      - python manage.py test

The above snippet is used by me generally to run django test cases. This will download python 2.7  Docker image, if it doesn't exist and install requirements.txt [pip packages needed for project] and run command python manage.py test. 

Build & Deploy:

You can have them on same server or different based on your choice. If you want to Build or Deploy, the servers you want to use for the purpose, should have all  required packages pre-installed and have gitlab-runner, for gitlab to commence build and deploy, so as many types of environments you have, you need runners on all those servers(test servers, stage servers, production servers.. etc., ). To make sure correct tasks are picked up by correct runners,, you need to add option called tags, which runner looks for[Note; Gitlab runner to listen on which tags will depend on your input during runner configuration.

example:

test:
    image: python:2.7
    script:
      - pip install -r requirements.txt
      - python manage.py test
    tags:
      - test

deploy_stage:
    script:
        - pip install -r requirements.txt
        - docker pull <image-name>
        - docker run -itd <image-name>
        - service nginx restart
    tags:
        - micropyramid-stage

So tags in the gitlab-ci.yml decide on execution of task on server allocated for that particular task.

In general, you can have a single server that can run all test cases and production and staging runners running on respective servers to pick up on tasks to perform to deploy it in respective environments for projects.

 

Related Links:
Live CI/CD demo - Video and Repository
Enabling container registry in gitlab
Setting up Gitlab omnibus edition - Blog Post and Video

    By Posted On
SENIOR DEVELOPER at MICROPYRAMID

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Web Hooks for Gitlab using PHP and Shell Scripts Jagadeesh V

Web-hooks play vital role if you are in Continuous Integration(CI). Higher Level organizations follow GitLab for CI purposes if they operate on open source solutions ...

Continue Reading...
ELK Stack for Parsing your Logs- Part 2 Jagadeesh V

In Previous Tutorial we looked into Setting up EL Server which can ingest your Syslog files. In this Post, you will learn about pushing your ...

Continue Reading...
HTTP2 and SPDY Protocols - Make HTTP Faster and Safer Dinesh Deshmukh

HTTP/2, next version of HTTP/1, http/1 can not handle the present web which has become more resource intensive, it cannot processes multiple requests in an ...

Continue Reading...

Subscribe To our news letter

Subscribe to our news letter to receive latest blog posts into your inbox. Please fill your email address in the below form.
*We don't provide your email contact details to any third parties