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
Configure SSL with LetsEncrypt and nginx Ashwin Kumar

Configuring SSL is beneficial not only for security purpose but also for SEO too.
Linuxsoftware foundation's initiated a program called Let’s Encrypt to give ssl ...

Continue Reading...
Setting Up Gitlab container Registry on own Domain. Jagadeesh V

GitLab Container Registry is a secure and private registry for Docker images integrated completely in Gitlab. In this tutorial we will setup and use GitLab ...

Continue Reading...
MySQL Setup and Security Jagadeesh V

In This Post you'll learn to install and secure your MYSQL Setup to interact with external applications.

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