docs.gitlab.com - GitLab CI/CD examples
forum.gitlab.com - Running pipeline from another project and then using build
forum.gitlab.com - Test code on multiple Python versions with gitlab-ci.yml
about.gitlab.com - How to use GitLab CI to deploy to multiple environments
www.lambdatest.com - Build An Automated Testing Pipeline With GitLab CI/CD & ...
stages:
- build
- test
- release
- preprod
- integration
- prod
#list out the individual jobs, assign each job a stage
build:
stage: build
image: python:3.9
script:
- python -m venv venv
- source venv/bin/activate
- pip install --upgrade pip
- pip install -r requirements.txt
test:
stage: test
image: python:3.9
dependencies:
- build
script:
- source venv/bin/activate
- python -m unittest discover
release:
stage: release
image:
dependencies:
- test
script:
- docker build -t my-python-app .
- docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
- docker push $CI_REGISTRY/my-python-app:$CI_COMMIT_SHORT_SHA
only:
- main
preprod:
stage: preprod
image:
dependencies:
- release
script:
- kubectl config use-context preprod-cluster
- kubectl apply -f deployment-preprod.yaml
environment:
name: preprod
url: https://preprod.example.com
only:
- main
integration:
stage: integration
image: python:3.9
dependencies:
- preprod
script:
- source venv/bin/activate
- pip install requests
- python integration_tests.py
environment:
name: integration
url: https://integration.example.com
only:
- main
prod:
stage: prod
image:
dependencies:
- integration
script:
- kubectl config use-context prod-cluster
- kubectl apply -f deployment-prod.yaml
environment:
name: production
url: https://example.com
when: manual
only:
- main
This .gitlab-ci.yml script defines a CI/CD pipeline with six stages for a Python application [5].
stages: Defines the order of pipeline execution: build, test, release, preprod, integration, and prod.
build: Uses a python:3.9 Docker image to create a virtual environment, activate it, upgrade pip, and install dependencies from requirements.txt.
test: Uses python:3.9, depends on build [2], activates the virtual environment, and runs unit tests.
release: Uses a Docker image, depends on test, builds a Docker image for the app, logs into the GitLab Container Registry, and pushes the image tagged with the short commit SHA to the registry. This stage runs only on the main branch.
preprod: Uses a kubectl image, depends on release, configures kubectl for the pre-production cluster, and applies the deployment configuration from deployment-preprod.yaml. It sets up a preprod environment with a URL and runs only on the main branch [4].
integration: Uses python:3.9, depends on preprod, activates the virtual environment, installs the requests library, and runs integration tests defined in integration_tests.py. It sets up an integration environment with a URL and runs only on the main branch.
prod: Uses a kubectl image, depends on integration, configures kubectl for the production cluster, and applies the deployment configuration from deployment-prod.yaml. It sets up a production environment with a URL and is triggered manually, running only on the main branch [6].
The image: key is missing in the release, preprod, and prod stages in the provided script snippet and would need to be specified for the jobs to run.