Ci:CD
Ci:CD
What is CI/CD?
Definition: Continuous Delivery extends CI by automating the release process so that changes can
be deployed to production (or a staging environment) with minimal manual intervention. The focus
is on making deployment predictable and error-free.
Purpose: To ensure software can be released at any time, with confidence that it meets quality
standards.
Key Features:
Code changes are automatically prepared for production release.
May include human approval or additional checks for compliance.
Definition: Continuous Deployment goes one step further than Continuous Delivery by fully
automating the deployment of code to production without any manual steps.
Purpose: To enable faster delivery of features and fixes to end-users.
Key Features:
Every successful build is automatically deployed.
Requires robust automated testing and monitoring to avoid issues.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/9
1. Code Commit: Developers commit code to a shared repository.
2. Automated Build: A CI/CD pipeline automatically builds the application.
3. Testing: Automated tests verify the functionality of the build.
4. Artifact Creation: Build artifacts (e.g., binaries, Docker images) are generated.
5. Delivery/Deployment:
Continuous Delivery: Prepares the code for deployment.
Continuous Deployment: Deploys the code to production automatically.
Benefits of CI/CD
CI/CD is fundamental to modern software development, enabling agility, reliability, and scalability in
delivering applications and services.
Using GitLab for CI/CD involves leveraging GitLab CI/CD—an integrated feature of GitLab that
automates building, testing, and deploying your code. Here’s how it works:
GitLab CI/CD pipelines are defined in a `.gitlab-ci.yml` file, located in the root of your repository. This
YAML file specifies the steps GitLab will execute.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/9
Structure of `.gitlab-ci.yml`
Stages: Define the lifecycle steps of the pipeline (e.g., `build`, `test`, `deploy`).
Jobs: Tasks to execute in each stage (e.g., running tests or building the application).
Scripts: Commands the job runs.
Runners: Machines or containers that execute jobs.
Example `.gitlab-ci.yml`:
yaml
stages:
- build
- test
- deploy
build:
stage: build
script:
- echo "Building the application..."
- make build # Example build command
test:
stage: test
script:
- echo "Running tests..."
- make test # Example test command
deploy:
stage: deploy
script:
- echo "Deploying the application..."
- ./deploy.sh # Custom deployment script
only:
- main # Only run deploy on the main branch
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/9
Example Trigger Scenarios:
GitLab provides a pipeline dashboard to monitor job statuses, logs, and progress:
Pipeline Status: See which jobs have succeeded, failed, or are in progress.
Job Logs: View detailed output from each job for debugging.
Define deployment scripts or use GitLab’s built-in Environments and Deployments features.
Deploy to servers, Kubernetes clusters, or cloud services using:
SSH scripts.
Docker containers.
Kubernetes integration.
yaml
deploy:
stage: deploy
script:
- kubectl apply -f k8s/deployment.yaml # Deploy to Kubernetes
environment:
name: production
url: https://your-production-site.com
only:
- tags
Advanced Features
yaml
deploy:
stage: deploy
script:
- echo "Deploying to $DEPLOY_ENV"
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/9
- ./deploy.sh
variables:
DEPLOY_ENV: production
GitLab CI/CD simplifies automating your development lifecycle, making it easier to maintain high-quality
code and deliver features faster.
A GitLab Runner is a lightweight application used to execute jobs in GitLab CI/CD pipelines. It acts as a
bridge between GitLab and the system where your CI/CD jobs run.
You can install GitLab Runner on various operating systems or in Docker containers.
For Linux:
bash
For Windows/MacOS: Follow the official installation steps from the GitLab Runner documentation.
Using Docker:
bash
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/9
2. Register the Runner
Once installed, you need to register the runner with your GitLab instance.
bash
gitlab-runner register
3. Runner Configuration
After registration, a `config.toml` file is created in the runner's working directory (default is
`/etc/gitlab-runner`).
This file stores the runner's configuration, including executor type, Docker images, caching
settings, etc.
toml
[[runners]]
name = "docker-runner"
url = "https://gitlab.com"
token = "YOUR_TOKEN"
executor = "docker"
[runners.docker]
image = "alpine:latest"
privileged = true
volumes = ["/cache"]
1. Pipeline Trigger:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/9
When a pipeline is triggered (e.g., code is pushed), GitLab checks for available runners.
2. Job Assignment:
GitLab assigns jobs to runners based on tags, executor type, or availability.
3. Execution:
The runner pulls the pipeline job definitions from the `.gitlab-ci.yml` file.
Executes the defined `script` using the selected executor (e.g., Docker, shell).
4. Job Output:
The runner streams the job logs back to GitLab for real-time feedback.
5. Artifacts and Caching:
Runners store artifacts (e.g., build outputs) or cache dependencies between jobs.
Executor Types
By creating and configuring GitLab Runners, you ensure your CI/CD jobs execute reliably in
environments tailored to your application needs.
An instance runner is a generic term for a compute resource or service that executes tasks, jobs, or
workloads within a specific framework, often in a CI/CD pipeline or automated workflow. It is not specific
to GitLab and can be applied to various automation or orchestration systems.
1. Task Execution: The primary purpose is to execute tasks (e.g., compiling code, running tests, or
deploying software) defined by a controlling system.
2. Dynamic or Persistent: Instance runners may be dynamically created for individual tasks or
persist to handle multiple jobs over time.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/9
3. Resource Provisioning: They utilize system resources such as CPU, memory, and storage to
execute tasks.
4. Environment Management: Runners can provide isolated environments (e.g., containers or
virtual machines) to ensure consistency and security.
1. Cloud-Based Runners:
Managed by cloud providers (e.g., AWS Lambda, Google Cloud Functions).
Dynamically scale based on demand.
Examples: Serverless functions, cloud build agents.
2. On-Premises Runners:
Hosted on physical or virtual machines within an organization's infrastructure.
Used when compliance, security, or performance requires local execution.
Examples: Jenkins agents on private servers.
3. Shared Runners:
Offered as shared resources by platforms (e.g., GitLab, GitHub Actions).
Can be used by multiple users or projects.
Suitable for smaller or less frequent workloads.
4. Dedicated Runners:
Dedicated to specific projects, users, or tasks.
Provide consistent performance and are more secure for sensitive data or operations.
5. Hybrid Runners:
Combine on-premises and cloud-based runners.
Provide flexibility to balance cost, performance, and compliance.
1. Job Assignment:
A controlling system (e.g., CI/CD server, workflow engine) assigns a task to the runner.
2. Environment Preparation:
The runner sets up the required environment for the job, such as pulling Docker images or
initializing dependencies.
3. Task Execution:
The runner executes the provided script, build process, or deployment steps.
4. Output Reporting:
The runner collects job logs, results, and artifacts and sends them back to the controlling
system.
5. Environment Cleanup:
Cleans up resources after task completion to ensure no residual effects on future jobs.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/9
Infrastructure Management: Executing scripts for provisioning or maintaining cloud
infrastructure.
Application Deployment: Deploying applications to staging, testing, or production environments.
Instance runners are integral to modern automation systems, enabling efficient and reliable execution
of jobs across diverse environments.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/9