# Basics of Gitlab CICD

### GitLab Pipeline Overview

A **GitLab pipeline** is a powerful automation feature that helps automate the continuous integration and continuous delivery (CI/CD) process for your code. It defines a series of stages and jobs that execute in a predefined sequence, ensuring that your code is tested, built, and deployed efficiently.

Pipelines in GitLab are defined using a configuration file called `.gitlab-ci.yml`, which is stored in the root directory of the repository. The `.gitlab-ci.yml` file describes how the pipeline should run, including the jobs, stages, and scripts to be executed.

### Key Concepts of GitLab Pipelines

1. **Pipeline**:
    
    * A pipeline is a collection of jobs organized into stages. A pipeline is triggered every time changes are made to the repository.
        
2. **Stages**:
    
    * Stages are the different steps in the pipeline. Each stage consists of one or more jobs that are executed concurrently, and once all jobs in a stage finish, the next stage begins.
        
    * Typical stages include:
        
        * **Build**: Compiling code or preparing assets.
            
        * **Test**: Running unit, integration, and other tests.
            
        * **Deploy**: Deploying the application to different environments (e.g., staging, production).
            
3. **Jobs**:
    
    * Jobs are the individual units of work in a pipeline. Each job runs a specific command or script (e.g., `npm install`, `python` [`test.py`](http://test.py), `docker build`).
        
    * Jobs are executed on GitLab **Runners**, which are agents responsible for running the jobs.
        
4. **GitLab Runners**:
    
    * **GitLab Runners** are agents that run the jobs defined in the pipeline. They can be shared (available for all projects) or specific to a particular project or group.
        
5. **Artifacts**:
    
    * Artifacts are files generated by jobs (e.g., test results, compiled code, documentation) that are passed between stages or saved for later use.
        
6. **Dependencies**:
    
    * Jobs can depend on other jobs to run. This is managed using **dependencies** and ensures that a job only runs after certain jobs have completed successfully.
        

![](https://miro.medium.com/v2/resize:fit:700/1*3stkQyMCIZHjDII0dUOaJw.png align="left")

### Step 1: Create a New GitLab Project

1. **Log in to GitLab**: Go to your GitLab instance and log in with your credentials.
    
2. **Create a New Project**:
    
    * On the GitLab dashboard, click the `New project` button.
        
    * Choose the type of project (e.g., **Blank project**).
        
    * Fill in the necessary details such as:
        
        * **Project name**: Choose a name for your project.
            
        * **Visibility level**: Select whether you want the project to be **Private**, **Internal**, or **Public**.
            
    * Click **Create project**.
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735021087756/9c0bda6a-31d7-47f4-a9cf-9742af795ca9.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735021178610/adde76cf-6b16-45d9-a790-cfcdd5715bf1.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735021280208/e54499c0-4c04-4340-bc63-058aa0ced8df.png align="center")

### Step 2: Set Up a GitLab CI Pipeline Configuration (`.gitlab-ci.yml`)

1. **Navigate to your project**: Once the project is created, you'll be taken to the project’s repository page.
    
2. **Create the** `.gitlab-ci.yml` file:
    
    * In the root directory of your project, create a file named `.gitlab-ci.yml`. This file contains the configuration for your CI pipeline.
        

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735102753276/62a95346-e28c-4756-b336-61c29cc71f7b.png align="center")

### Step 3: Commit the `.gitlab-ci.yml` File to Your Repository

1. **Commit the** `.gitlab-ci.yml` file:
    
    * Once the `.gitlab-ci.yml` file is created, commit it to your GitLab repository.
        
    
    1. **Job 3**: `deploy`, which depends on `test` and also stores artifacts
        
    
    ```yaml
    yamlCopy codestages:
      - build
      - test
      - deploy
    
    # Job to build the project
    build:
      stage: build
      script:
        - echo "Building the project..."
        - mkdir build_output
        - echo "Build successful" > build_output/build.log
      artifacts:
        paths:
          - build_output/
        expire_in: 1 hour
    
    # Job to test the project
    test:
      stage: test
      needs: 
        - job: build
          artifact: true
      script:
        - echo "Running tests..."
        - cat build_output/build.log
        - echo "Test passed"
      artifacts:
        paths:
          - test_reports/
        expire_in: 2 hours
    
    # Job to deploy the project
    deploy:
      stage: deploy
      needs:
        - job: test
          artifact: true
      script:
        - echo "Deploying the project..."
        - cat test_reports/test_report.txt
        - echo "Deployment completed"
      artifacts:
        paths:
          - deploy_logs/
        expire_in: 1 day
    ```
    

### Step 4: Trigger the CI Pipeline

1. **Push Changes to GitLab**: Whenever you push the `.gitlab-ci.yml` file to your repository (or any other change), GitLab will automatically detect the presence of the `.gitlab-ci.yml` file and trigger the pipeline.
    
    * After you push the changes, go to the **CI / CD** section of the project, which can be found in the left sidebar under **CI / CD**.
        
2. **Pipeline Started**: You’ll see the pipeline is triggered and will move through the defined stages. If the pipeline doesn't trigger automatically, ensure that `.gitlab-ci.yml` is properly formatted and committed.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735103272257/463f6860-99c3-4585-bafe-0ad52f4ecd3d.png align="center")

### Step 5: Monitor CI Pipeline Jobs

1. **Navigate to the Pipelines Page**:
    
    * In your project, go to the **CI / CD** section from the left sidebar.
        
    * Click on **Pipelines**. This will show you a list of all pipelines for the project.
        
    * You’ll see the pipeline with a status indicating whether it's **running**, **succeeded**, or **failed**.
        
2. **View Job Details**:
    
    * Click on the pipeline entry to see detailed information about each job.
        
    * You will be able to see:
        
        * The status of each job (success, failed, canceled, etc.).
            
        * Logs for each job by clicking on the job name (e.g., `build`, `test`, `deploy`).
            
        * Artifacts that were created during each job.
            

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735103302525/6c41f0b9-f91c-4adc-af2a-dfeec9c9747b.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735103338065/47eccfa9-c771-45d0-8774-1fbddc29e50a.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1735105472028/c8030c95-7aad-41ed-a868-f79e959d928e.png align="center")
