• Home
  • How To Set Up GitLab CI/CD Pipeline For Beginners In 2023?
set up GitLab CI/CD pipeline
webbuilder October 9, 2023 0 Comments

How To Set Up GitLab CI/CD Pipeline For Beginners In 2023?

GitLab CI/CD is a powerful solution for automating software development, from code creation and testing through deployment to production. You may enhance the quality of your software, minimize the time it takes to ship new features and increase your team’s productivity by learning how to set up GitLab CI/CD.

 

This article will lead you through the stages of configuring GitLab CI/CD for your first project. We will also cover utilizing GitLab CI/CD to create and deploy Docker images, run tests, produce reports, and automate additional activities.

 

This post will help you start with GitLab CI/CD, whether a newbie or an experienced developer. 

prerequisites to set up GitLab CI/CD pipeline

Creating Your GitLab Project

Sign in to GitLab:

  •     Open your web browser and go to the GitLab website (https://gitlab.com/).
  •     If you don’t have an account, you must sign up first. Otherwise, log in with your GitLab credentials.

Click on “New Project”:

  •     Once you’re logged in, you’ll be taken to your GitLab dashboard.
  •     Look for the “+ New Project” button or the “New Project” link, usually in the dashboard’s upper right corner. Click on it.

Fill in Project Details:

  •     You’ll now be on the “New Project” page, where you need to provide essential project details.
  •     Choose a project name: Give your project a unique and meaningful name.
  •     Choose a project slug: This is a URL-friendly version of your project’s name, usually auto-generated based on your project name. You can customize it if needed.
  •     Add a project description (optional): Write a brief description of your project to help others understand its purpose.
  •     You can also initialize your repository with a README file, which helps provide project documentation.

Choose Visibility Settings (Public or Private):

  •     GitLab allows you to control the visibility of your project. You have two main options:
  •     Public: Anyone on the internet can view your project, its code, and its contents.
  •     Private: Only people you invite can access your project. It’s ideal for sensitive or proprietary code.
  •     Select the appropriate visibility option based on your project’s requirements.

Click “Create Project”:

  •     Review the information you’ve provided after completing the project details and selecting the visibility settings.
  •     If everything looks correct, click the “Create project” button.

Congratulations! Your GitLab Project is Created:

GitLab will create your project and take you to the project’s main page.

Depending on your project’s needs, you can find various features like code repositories, issue tracking, CI/CD settings, and more.

The Steps For Setting Up GitLab CI/CD Project

Step 1: Check for Runner Availability

 

In GitLab, runners play a vital role as they are the agents responsible for executing your CI/CD jobs.

 

To verify the availability of runners, follow these steps:

 

  •     Navigate to your project’s settings by clicking “Settings” within your GitLab project.
  •     In the left sidebar, locate “CI/CD” and click to expand it.
  •     Look for the “Runners” section.
  •     Ensure you have at least one runner marked as active, typically indicated by a green circle next to it. This active runner is ready to process your CI/CD jobs.

 

If You Don’t Have an Active Runner:

 

If there are no active runners available, you’ll need to set up one as follows:

 

  •     Install GitLab Runner on your local machine or a dedicated server you intend to use as a runner.
  •     Register the runner for your specific project. During registration, choose the shell executor, which defines how your CI/CD jobs will run. The shell executor is suitable for running jobs on your local machine.
  •     After completing these steps, your CI/CD jobs will be able to execute on your local machine or the designated runner, ensuring the smooth functioning of your pipeline in GitLab.

 

Step 2: Create a .gitlab-ci.yml Configuration File

Let’s create a .gitlab-ci.yml file, a YAML-based configuration file where you define instructions for GitLab CI/CD. In this file, you specify the order of jobs for the runner and make decisions based on specific conditions.

 

To create your .gitlab-ci.yml file, follow these steps:

 

  •     In your GitLab project, navigate to the “Code” section and select “Repository” from the left sidebar.
  •     Choose the branch you want to commit to (typically “master” or “main”). Then click the plus icon (“+”) and select “New file.”
  •     Name the file as “.gitlab-ci.yml.”
  •     In the larger text window, provide your pipeline configuration. Below is an example of a basic configuration:

 

build-job:

  stage: build

  script:

- echo "Hello, $GITLAB_USER_LOGIN!"

 

test-job1:

  stage: test

  script:

- echo "This job tests something"

 

test-job2:

  stage: test

  script:

- echo "This job tests something, but takes more time than test-job1."

- echo "After the echo commands complete, it runs the sleep command for 20 seconds"

- echo "which simulates a test that runs 20 seconds longer than test-job1"

- sleep 20

 

deploy-prod:

  stage: deploy

  script:

- echo "This job deploys something from the $CI_COMMIT_BRANCH branch."

  environment: production

 

This example defines four jobs: build-job, test-job1, test-job2, and deploy-prod. The comments within the echo commands provide descriptions displayed in the GitLab UI when viewing the job details. Predefined variables like $GITLAB_USER_LOGIN and $CI_COMMIT_BRANCH are automatically populated when the jobs run.

 

  •     After creating the .gitlab-ci.yml file with your desired configuration, select “Commit changes.”
  •     GitLab will initiate a pipeline, running the jobs you’ve defined in the .gitlab-ci.yml file. The pipeline’s progress and job results can be monitored within the GitLab interface.
  •     By following these steps, you’ve successfully set up your .gitlab-ci.yml configuration file, defining the structure and behavior of your CI/CD pipeline in GitLab.

 

Step 3: Review Your Pipeline and Job Status

Now that you’ve set up your CI/CD pipeline, it’s time to check the status and details of your pipeline and its jobs.

 

Follow these steps:

 

  •     Navigate to the “Build” section and select “Pipelines” from the menu.
  •     In the “Pipelines” view, you’ll see a pipeline with multiple stages. Typically, you’ll have stages like “build,” “test,” and “deploy.”
  •     To get a visual representation of your pipeline, click on the pipeline ID. This provides an overview of the entire pipeline flow.
  •     Select the job name to delve deeper into a specific job’s details. For example, you can click “deploy-prod” to see the specifics of that particular job, including its logs and status.

 

By following these steps, you can review the status and details of your CI/CD pipeline. Congratulations on successfully creating your first CI/CD pipeline in GitLab!

 

You’re now ready to explore more advanced customization options for your .gitlab-ci.yml file and define more intricate jobs to enhance your development and deployment processes.

Conclusion

Setting up GitLab CI/CD for your inaugural project marks a pivotal stride towards modernizing your software development workflows. You’ve laid a strong foundation for expeditious, dependable, and error-resistant software releases by using the potential of automation and continuous integration.

 

Remember, GitLab CI/CD isn’t just a tool; it embodies a philosophy that champions collaboration, quality, and efficiency. As you progress in your CI/CD journey, constantly fine-tune and adapt your pipelines to meet your project’s distinct requirements.

 

Embrace a culture of perpetual enhancement, and your development process will consistently yield superior software with each iteration. If you ever need assistance or want to explore advanced CI/CD solutions, consider contacting experts at Triotech Systems to accelerate your DevOps journey. Your success story in the realm of CI/CD awaits!

FAQs

CI focuses on automatically building and testing code changes, ensuring code quality and stability. On the other hand, CD extends CI by automating code deployment to production or staging environments. GitLab CI/CD combines both processes for a seamless development pipeline.

Yes, GitLab CI/CD is language-agnostic and versatile. You can configure CI/CD pipelines for projects using various programming languages and technologies, making it suitable for a wide range of development stacks.

A GitLab Runner is an agent responsible for executing CI/CD jobs. GitLab provides shared runners for common use cases, but you can also set up your runners for custom environments or specific needs. Depending on your project, you may use shared runners or deploy your dedicated runners.

GitLab provides features like environment and secret variables to store sensitive information securely. You can encrypt and protect these variables, ensuring they are only accessible to authorized users and jobs within your pipeline.

GitLab CI/CD artifacts are files generated by jobs in your pipeline. They can include build outputs, test reports, or other relevant data. Artifacts help you preserve and share essential information between pipeline stages or even between different pipelines, making debugging and troubleshooting issues in your CI/CD process easier.

Recent Posts

Leave Comment