GitLab’s Continuous Integration and Continuous Deployment (CI/CD) pipelines are a game-changer for modern software development. They allow for automated testing, building, and deploying of code changes, ensuring a smooth and efficient workflow. In this guide, we’ll walk through setting up a basic GitLab CI/CD pipeline with an example, showcasing how you can automate your development process and increase productivity.
Prerequisites
- GitLab account
- Git repository hosted on GitLab
- Basic understanding of Git and GitLab
Step 1: Create a .gitlab-ci.yml
File
1.1 What is .gitlab-ci.yml
?
The .gitlab-ci.yml
file is where you define the steps of your CI/CD pipeline in YAML format.
1.2 Create .gitlab-ci.yml
In your GitLab repository, create a file named .gitlab-ci.yml
in the root directory.
1.3 Example .gitlab-ci.yml
Here’s a simple example to get started:
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building the application..."
test_job:
stage: test
script:
- echo "Running tests..."
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
In this example:
- We have defined three stages:
build
,test
, anddeploy
. - Each stage has a corresponding job with a
script
section. This is where you define the commands to be executed for each job.
Step 2: Define CI/CD Jobs
2.1 Build Job
The build
job could be used to compile your code, build Docker images, or perform any pre-deployment tasks.
2.2 Test Job
The test
job could run unit tests, integration tests, or any other type of automated tests.
2.3 Deploy Job
The deploy
job is responsible for deploying your application to your desired environment, such as staging or production.
Step 3: Commit and Push .gitlab-ci.yml
Commit the .gitlab-ci.yml
file to your repository and push it to GitLab:
git add .gitlab-ci.yml
git commit -m "Add .gitlab-ci.yml"
git push origin master
Step 4: Set Up CI/CD Pipeline in GitLab
4.1 GitLab CI/CD Configuration
- Go to your GitLab repository on the GitLab website.
- Navigate to
Settings
->CI/CD
->Runners
. - Ensure you have shared runners available (either shared or specific to your project).
4.2 Run Pipeline
- With the
.gitlab-ci.yml
file in place, GitLab will automatically detect it and run the pipeline when you push changes. - Go to
CI/CD
->Pipelines
in your GitLab repository to view the pipeline status and logs.
Step 5: Example with Node.js Application
Let’s create a simple Node.js application and add a CI/CD pipeline for building, testing, and deploying.
5.1 Node.js Application
Create a basic Node.js application:
- Create a new directory for your project:
mkdir node-app
cd node-app
- Initialize a new Node.js project:
npm init -y
- Install a test framework (like Mocha) for testing:
npm install mocha --save-dev
- Create a simple
app.js
file:
// app.js
const sum = (a, b) => a + b;
module.exports = sum;
- Create a simple test file
test.js
:
// test.js
const sum = require('./app');
const assert = require('assert');
describe('Sum function', () => {
it('should return the sum of two numbers', () => {
assert.equal(sum(1, 2), 3);
});
});
5.2 Update .gitlab-ci.yml
Now, update the .gitlab-ci.yml
file in your repository:
stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building the application..."
- npm install
test_job:
stage: test
script:
- echo "Running tests..."
- npm test
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
5.3 Commit and Push Changes
Commit your changes and push them to GitLab:
git add .
git commit -m "Add Node.js app and CI/CD pipeline"
git push origin master
5.4 Monitor Pipeline
- Go to
CI/CD
->Pipelines
in your GitLab repository. - You should see the pipeline running.
- The
build
job will install dependencies, thetest
job will run tests, and thedeploy
job (in this example, just an echo) will be executed.
Conclusion
This pipeline can be expanded to include more complex build and deployment steps as needed for your projects. GitLab CI/CD offers a wide range of features, such as caching, environment variables, and more, to customize and optimize your pipeline. Explore the GitLab CI/CD documentation for advanced configuration options and best practices.