Create a CI/CD Pipeline in Minutes With GitLab
Edwin Torres
Posted on March 7, 2021
This article is a quick tutorial on creating a simple CI/CD pipeline in GitLab.
What is CI/CD?
CI/CD stands for continuous integration and delivery/deployment. It is a way to develop software in small, automated batch cycles with continuous builds, tests, and deployment. CI/CD is a fundamental concept in DevOps environments. GitLab has built-in tools that make it easy to create CI/CD pipelines.
See the excellent GitLab CI/CD documentation here.
What you will need
Tutorial
1. Version your software application.
Create a GitLab repo for your software application. Add your programs(s) to the repo. Here is a sample Python program for this CI/CD tutorial:
# hello.py
print('Hello world.')
2. Enable a GitLab Runner.
From a web browser, go to your GitLab repo. Go to Settings > CI / CD > Runners > Shared runners. Make sure Enable shared runners for this project is checked.
3. Create a .gitlab-ci.yml
file.
Create a file named .gitlab-ci.yml
in the top folder of your repo. The YAML file defines the CI/CD pipeline:
image: python:latest
job0:
stage: build
script:
- echo "build phase..."
- uname -a
job1:
stage: test
script:
- echo "test phase..."
- python helloworld.py
job2:
stage: deploy
script:
- echo "deploy phase..."
This .gitlab-ci.yml
file defines a basic pipeline for the Python program. There are three basic stages: build, test, and deploy. Since this is a basic program, the phases are simple. More complicated software applications have more steps in the phases.
There is a wide selection of CI/CD image templates available for different types of software applications. Define the .gitlab-ci.yml
file to meet your needs.
4. Commit and push the .gitlab-ci.yml
to your repo.
When you commit the .gitlab-ci.yml
to your repo, it triggers the CI/CD pipeline. In fact, any commit to your remote GitLab repo triggers the pipeline.
After you push the file(s) to your repo, open a web browser and go to your GitLab project. In the left sidebar, click CI / CD > Pipelines. This is where you view the pipeline:
View information about the pipeline as it executes. Here is a log file for the test (job1) phase:
This pipeline completes successfully, three green checkmarks:
5. Introduce an error in your program.
Modify the helloworld.py
program and introduce an error to it. Commit and push the program to the repo. This breaks the pipeline:
The bad program causes the pipeline to fail. The pipeline continuously integrates our software changes and provides feedback on whether or not it succeeds or fails. You see the status of the pipeline in the browser and receive emails for failed pipelines.
What next?
This tutorial demonstrates how to build a basic CI/CD pipeline in GitLab. It is just a start. Build more advanced CI/CD pipelines to suit you needs.
Customize and expand the pipeline stages. For example, your web application may need additional build steps to load external modules. Your test phase may include automated unit tests or program linting. Your deploy phase may deploy the software to a production environment for customers.
This is just a start. Build your first CI/CD pipeline in GitLab and see how far you can take it!
Thanks for reading. 😃
Follow me on Twitter @realEdwinTorres
for more programming tips and help.
Posted on March 7, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.