Part 1: Introduction to GitLab CI/CD – Getting Started
What You'll Learn in Part 1
In this first part, we'll cover:
? What GitLab CI/CD is and why it’s essential.
? Setting up your first pipeline.
? Understanding the .gitlab-ci.yml file in detail.
? Basics: Stages, Jobs, and Tasks.
? Where to place the .gitlab-ci.yml file.
? Running your first build job.
What is GitLab CI/CD?
GitLab CI/CD (Continuous Integration/Continuous Deployment) is an automated process for building, testing, and deploying applications. It allows teams to deliver high-quality software faster and more efficiently.
Why GitLab CI/CD?
? Built-in Integration: No need for third-party tools—fully integrated into GitLab.
? Automation: Reduces manual effort by automating builds, tests, and deployments.
? Scalability: Works for small teams and large enterprises alike.
? Improved Code Quality: Detects errors early through continuous testing.
Understanding the .gitlab-ci.yml File
What is the .gitlab-ci.yml file?
The .gitlab-ci.yml file is the heart of your GitLab CI/CD pipeline. It defines the pipeline stages, jobs, and tasks to automate software delivery.
Where to Place the .gitlab-ci.yml File?
You must place this file at the root of your GitLab repository. Example:
my-project/
│── src/
│── tests/
│── .gitlab-ci.yml ? (Place it here)
│── README.md
Once this file is committed and pushed, GitLab automatically detects and triggers the pipeline.
Key Concepts: Stages, Jobs, and Tasks
1?? Stages
Stages define the pipeline phases (e.g., build, test, deploy).
Example: yaml file
stages:
- build
- test
- deploy
Think of stages as a sequence of steps in your software pipeline.
2?? Jobs
Jobs define specific actions performed within each stage.
Example: yaml file
build_job:
stage: build
script:
- echo "Building the application..."
A job is a unit of work that executes within a stage.
3?? Tasks (Commands within Jobs)
Tasks are commands that a job executes.
Example: yaml file
script:
- npm install
- npm run build
Tasks are individual commands inside a job that do actual work.
Your First .gitlab-ci.yml File
Here’s a basic pipeline with build, test, and deploy stages.
领英推荐
Example: yaml file
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Installing dependencies..."
- npm install
- echo "Building the application..."
- npm run build
test_job:
stage: test
script:
- echo "Running tests..."
- npm run test
deploy_job:
stage: deploy
script:
- echo "Deploying application..."
- scp -r dist/ user@server:/var/www/html
only:
- main
Breakdown of this Example:
? Stages: Defines the pipeline structure (build → test → deploy).
? Jobs: Defines specific steps like build_job, test_job, deploy_job.
? Script: Commands that execute inside each job.
? Only Main: Ensures deployment happens only on the main branch.
Triggering Your First Pipeline
Step 1: Commit and Push the .gitlab-ci.yml File
Once you push this file to GitLab, it automatically triggers the pipeline.
Step 2: View Your Pipeline
1?? Navigate to CI/CD → Pipelines in GitLab.
2?? Click on the running pipeline to see logs and job status.
Step 3: Manually Trigger a Pipeline (If Needed)
If you want to manually trigger a pipeline:
1?? Go to CI/CD → Pipelines
2?? Click Run Pipeline
Useful GitLab CI/CD Commands
See below shell (sh) commands:
?? Check Pipeline Status:
gitlab-runner status
?? View Logs of a Job:
gitlab-runner logs
?? Retry a Failed Job:
gitlab-runner retry <job_id>
?? Cancel a Running Job:
gitlab-runner cancel <job_id>
Summary Table: Where to Run GitLab CI/CD Commands:
Use Case: Automating Unit Tests
Imagine you’re working on a web application. You want to ensure every commit is automatically tested.
?? Without CI/CD: Developers manually run npm test, which is time-consuming.
?? With GitLab CI/CD: Every push triggers automated unit tests:
Example: yaml file
test_job:
stage: test
script:
- echo "Running automated tests..."
- npm run test
Now, every push ensures your application remains bug-free automatically!
What's Next?
This is just the beginning! ?? In Part 2, we’ll explore intermediate features of GitLab CI/CD, such as caching, artifacts, environment variables, and deploying to staging environments.
#GitLab #CI/CD #DevOps #Automation #GitLabCI
Senior Software Engineer | Mobile App Development & Automation | Expertise in Native & Hybrid Solutions | Mentor & Tech Lead | Team Collaboration
1 个月Nice! Suggestion: It would have been more helpful if you provide more insight of the .yml file. - what are stages, jobs, tasks - where should we place this file