Gitlab pipeline creation along with its components and stages



Gitlab offers various features and one of them is pipeline. Gitlab pipeline is used to perform continuous integration, continuous deployment ,code quality checks, security checks, artifact storage etc. The .gitlab-ci.yml file plays a vital role in the gitlab pipeline. All the pipeline configuration is written in the .gitlab-ci.yml file.CI/CD pipeline can run automatically when events are triggered like pushing to a branch , merging to a branch etc.

Pipeline Components

Jobs and Stages together form the pipeline. YAML keywords are used to define the stages and jobs.

  • Jobs: Jobs run specific commands to achieve a single goal. For example a job could be creating a jar, checking code coverage, checking code quality, finding the vulnerable scenarios etc. Runners are responsible for executing the jobs. Jobs can run parallel as they are independent to each other.
  • Stage: Stages are nothing but the group of jobs. Stages run in sequence. If any job fails in any stage, the next stage is not executed and pipeline aborts. Next stage will run once the previous stage runs successfully.

    Pipeline Steps

    1. Create a project in the gitlab
    2. Ensure you have runners available for your project (runners are available by default)
    3. Select a branch in which you want to create a pipeline
    4. Create gitlab-ci.yml file in the root directory of the project where all the configuration related to CI/CD is written
    5. Write your CI/CD code in the gitlab-ci.yml file.

    Example

    build:
      stage: build
      script:
        - echo "Hello, Gitlab Pipeline!"
    
    test:
      stage: test
      script:
        - echo "This job tests Pipeline demo project."
    
    deploy:
      stage: deploy
      script:
        - echo "This job deploys jar from the main branch into dev env"
      environment: dev
    
    
    6. Select Commit changes
    7. If your configuration is valid, you will see banner saying Your Gitlab CI configuration is valid


    Otherwise you will see Invalid configuration banner like below



    8. Go to Build ? Pipelines on the left hand side menu and you will see all the failed and successful pipelines.



    We have configured 3 stages for this pipeline Build, Test and Deploy.

    9. You can see each job by clicking on it, for example "test" stage

    Run a job manually:
    In real time scenarios, we may come across some scenarios where manual interaction is needed to run the job. The best example for manual job is "deployment into production" . Gitlab offers manual jobs run by adding "When : manual" in the .gitlab-ci.yml file.
    Example

    build:
      stage: build
      script:
        - echo "Hello, Gitlab Pipelne!"
    
    test:
      stage: test
      script:
        - echo "This job tests Pipeline demo project."
    
    deploy:
      stage: deploy
      script:
        - echo "This job deploys artifact from the main branch into prod environment."
      environment: production
      when : manual

    Manual jobs are displayed as skipped when the pipeline runs like below. As we defined "deploy" job as a manual job, so it is shown as skipped.


    You can run manual run by clicking on run button.


    You can add a confirmation alert before running a sensitive job.

    Example
    build:
      stage: build
      script:
        - echo "Hello, Gitlab Pipeline!"
    
    test:
      stage: test
      script:
        - echo "This job tests Pipeline demo project."
    
    deploy:
      stage: deploy
      script:
        - echo "This job deploys artifacts from the main branch into the prod environment."
      environment: production
      when : manual
      manual_confirmation : "Are you sure you want to deploy into Production?"
    



    To use "manual_confirmation" ,"when : manual" is a must. WIthout "when : manual" , "manual_confirmation" has no effect on the job.

    Run a pipeline at scheduled intervals

    Gitlab offers scheduled runs for the pipeline. You can create a pipeline and run it everyday, every week, every month etc. You can customize the schedule by giving regular expressions also.To create a scheduled pipeline go to Build ? Pipeline Schedules




  • Updated on: 2025-03-10T11:52:16+05:30

    159 Views

    Kickstart Your Career

    Get certified by completing the course

    Get Started
    Advertisements