Gitlab CI/CD Introduction

Jul 2, 2024

Gitlab CI/CD Introduction

Key Concepts

  • Gitlab CI/CD: Automation engine for software development (similar to Jenkins).

    • Practices: Continuous integration, Continuous Delivery/Deployment.
  • Gitlab Pipeline (CI YAML): Version-controlled .gitlab-ci.yml file at the root directory of a Gitlab project.

    • Components:
      • Job: Describes tasks (e.g., compile, test, deploy code).
      • Stage: Defines the order of jobs.
  • Gitlab Runner: Program executing the pipeline jobs; can run on local machines, VMs, or Docker containers.

Creating the First Pipeline

  1. Navigate to CI/CD section
  • Pipeline section or Pipeline Editor.
  • Gitlab provides CI/CD templates for various tech stacks.
  1. Using the Pipeline Editor
  • Browser-based; provides template and validation.
  • Features: Syntax highlighting and keyword suggestions (bug noted).
  • Additional Tabs: Visualize (graphical view) and Lint (syntax check).

Structure of a Basic Pipeline

  • Stages Section

    • Defines stages in order (e.g., build, test, deploy).
    • Example: stages: [build, test, deploy]
  • Job Definition

    • Jobs associated with stages using the stage: keyword.
    • Required keyword: script: for commands.
    • Example: Build job creating a file to be used in Test and Deploy jobs.

Running the Pipeline

  • Pipeline Execution
    • Commit changes to trigger the pipeline.
    • Issues related to workspace isolation and artifact sharing.

Artifacts & Sharing Data

  • Artifacts Keyword

    • Specifies files to share with downstream jobs.
    • Example: artifacts: { paths: ["build/executable_binary_file_v1"] }
  • Adjust Unit Test Job

    • Modify to consume artifact: cat $CI_PROJECT_DIR/build/executable_binary_file_v1
    • Commit changes and check pipeline output (successful reference).

Docker Images

  • Using Docker Images
    • Specified using image: keyword.
    • Example: Python Docker image (image: python:latest)
    • Docker container handles script commands.
    • Custom Docker images can be used.

Environment Variables

  • Defining Variables
    • Use variables: keyword; specify key-value pairs.
    • Example: variables: { USERNAME: "tech_with_moss" }
    • Sensitive variables: Define in CI/CD Settings -> Variables (e.g., password).

Gitlab Runner Settings

  • Shared vs. Specific Runners
    • Shared runners: Hosted by Gitlab, limited free minutes.
    • Specific runners: User-managed compute resources (local/VM/Docker).

Caching

  • Cache Keyword
    • Caches files downloaded in a job, reducing redundant downloads.
    • Example: Caching Python packages.

Conclusion

  • Quick overview of Gitlab CI/CD and pipeline creation.
  • References: Key documentation links included for further reading and video description.