Purpose: Understand the stages in a CI/CD Pipeline
CI - Continuous Integration
CD - Continuous Delivery/Continuous Deployment
Follow Along in a Real GITLAB CI/CD Project: https://gitlab-ce-f0d8bzvm.lab.practical-devsecops.training/root/django-nv
Staging in gitlab ci/cd
stages:
- build
- test
- integration
- deploy
Build - compiling code
Test - Apply static and dynamic testing
Integration - Code passes applicable tests and put back into the code base
Deploy - code put into production
image: docker:20.10
services:
- docker:dind
stages:
- build
- test
- integration
- deploy
django_build:
stage: build
image: python:3.6
before_script:
- pip3 install --upgrade virtualenv
script:
- virtualenv env
- source env/bin/activate
- pip install -r requirements.txt
- python manage.py check
django_test:
stage: test
image: python:3.6
before_script:
- pip3 install --upgrade virtualenv
script:
- virtualenv env
- source env/bin/activate
- pip install -r requirements.txt
- python manage.py test taskManager
django_integration:
stage: integration
script:
- echo "This is an integration step"
- exit 1
allow_failure: true # Even if the job fails, continue to the next stages
django_deployment:
stage: deploy
script:
- echo "This is a deploy step."
when: manual # Continuous Delivery
Explanation:
gitlab-ci.yml file will contain this information
image: docker:20.10: This line, although present, doesn't seem to be actively used in this specific job definition. The django_build job explicitly uses the python:3.6 image. It's possible this was intended for a service or a different job not shown here.
services: -: This section typically defines linked services (like databases) that the job might need. Here, it's empty, indicating no additional services are linked for this job.
stages: - build: This defines the stages the pipeline will go through. This configuration has a single stage named "build".
django_build:: This defines a job named "django_build".
stage: build: This assigns the "django_build" job to the "build" stage.
image: python:3.6: This specifies that the "django_build" job will run in a Docker container based on the python:3.6 image.
before_script:: This section lists commands to run before the main script.
- pip3 install --upgrade virtualenv: This command upgrades the virtualenv package within the container.
script:: This section contains the main commands for the job.
- virtualenv env: This creates a virtual environment named "env".
- source env/bin/activate: This activates the created virtual environment. Subsequent pip install commands will install packages within this isolated environment [2].
- pip install -r requirements.txt: This installs all the Python dependencies listed in the requirements.txt file [1, 2, 3].
- python manage.py check: This is a Django management command that checks for any potential problems in the Django project.
django_test:: This defines a job named "django_test".
stage: test: This assigns the "django_test" job to the "test" stage. Stages organize the CI/CD pipeline.
image: python:3.6: This specifies that the "django_test" job will run in a Docker container based on the python:3.6 image.
before_script:: This section lists commands to run before the main script.
- pip3 install --upgrade virtualenv: This command upgrades the virtualenv package within the container.
script:: This section contains the main commands for the job.
- virtualenv env: This creates a virtual environment named "env" to isolate project dependencies [3].
- source env/bin/activate: This activates the created virtual environment. Subsequent pip install commands will install packages within this isolated environment.
- pip install -r requirements.txt: This installs all the necessary Python packages listed in the requirements.txt file [4].
- python manage.py test taskManager: This runs the Django unit tests specifically for the "taskManager" application. It's part of a Continuous Integration (CI) process to automate testing