Gitlab and Docker - Continuous Integration, Deployment and Continuous Delivery
2022-07-20Continuous 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
We will Provide Docker Consulting services and Server Maintenance Services. To Know more about our services contact [email protected]