A Comprehensive GitLab Crash Course; Run your first CI/CD Pipeline

A Comprehensive GitLab Crash Course; Run your first CI/CD Pipeline

FOLLOW ME ON MEDIUM: https://medium.com/@jniicholaas

ARTICLE ON MEDIUM: https://medium.com/@jniicholaas/a-comprehensive-gitlab-crash-course-run-your-first-ci-cd-pipeline-84c680349bca

What is GitLab?

GitLab is a web-based platform for version control and collaboration. It provides a complete DevOps lifecycle management tool, including source code management, continuous integration/continuous deployment (CI/CD), issue tracking, and more.

STEP ONE: Make project

Make a free project at https://gitlab.com

STEP TWO: Ensure you have runners available

In GitLab, runners are agents that run your CI/CD jobs.

To view available runners:

  • Go to Settings > CI/CD and expand Runners.

As long as you have at least one runner that’s active, with a green circle next to it, you have a runner available to process your jobs.

STEP THREE: If no runners exist..

If you don’t have a runner:

  1. Install GitLab Runner on your local machine.
  2. Register the runner for your project. Choose the shell executor.

When your CI/CD jobs run, in a later step, they will run on your local machine.

STEP FOUR: Create yml file

Now create a .gitlab-ci.yml file. It is a YAML file where you specify instructions for GitLab CI/CD.

In this file, you define:

  • The structure and order of jobs that the runner should execute.
  • The decisions the runner should make when specific conditions are encountered.

To create a .gitlab-ci.yml file:

  1. On the left sidebar, select Code > Repository.
  2. Above the file list, select the branch you want to commit to. If you’re not sure, leave master or main. Then select the plus icon ( ) and New file:

3. For the Filename, type .gitlab-ci.yml and in the larger window, paste this sample code:

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 shows four jobs: build-job, test-job1, test-job2, and deploy-prod. The comments listed in the echo commands are displayed in the UI when you view the jobs. The values for the predefined variables $GITLAB_USER_LOGIN and $CI_COMMIT_BRANCH are populated when the jobs run.

4. Select Commit changes.

The pipeline starts and runs the jobs you defined in the .gitlab-ci.yml file.

STEP5: View the status of your pipeline and jobs

Now take a look at your pipeline and the jobs within.

  1. Go to Build > Pipelines. A pipeline with three stages should be displayed:

2. View a visual representation of your pipeline by selecting the pipeline ID:

3. View details of a job by selecting the job name. For example, deploy-prod:

You have successfully created your first CI/CD pipeline in GitLab. Congratulations!

Now you can get started customizing your .gitlab-ci.yml and defining more advanced jobs.


要查看或添加评论,请登录

Jack M.的更多文章