CI/CD runners

2024-07-19 1071阅读

0 Preface/Foreword

This tutorial shows you how to configure and run your first runner in GitLab.

A runner is an agent in the GitLab Runner application that runs jobs in a GitLab CI/CD pipeline. Jobs are defined in configuration file and assigned to available runners.

GitLab has three types of runners:

  • Shared: Available to all groups and projects in a GitLab instance.
  • Group: Available to all projects and subgroups in a group
  • Project: Associated with specific projects. Typically, project runners are used by one project at a time.

    0.1 参考文献

    Tutorial: Create, register, and run your own project runner | GitLab

    Install GitLab Runner | GitLab

    1 Runners

    For this tutorial, you'll create a project runner to run jobs defined in a basic pipeline configuration:

    1. Create a blank project.
    2. Create a project pipeline.
    3. Create and register a project runner.
    4.  Trigger a pipeline to run your runner.

     1.1 Create a blank project

    First, create a blank project where you can create your CI/CD pipeline and runner.

    To create a blank project:

    1. On the left sidebar, at the top, select Create new (+) and new project/repository.
    2. Select create blank project.
    3. Enter project details: 1) in the project name field, enter the name of your project. The name must start with a lowercase or uppercase letter (a-zA-Z), digit (0-9), emoji, or underscore (_). It can also contain dots (.), pluses (+), dashes (-), or space. 2)I n the project slug field, enter the path to your project. The GitLab instance uses the slug as the URL path to the project. To change the slug, first enter the project name, then change the slug.
    4. Select Create project.

    1.2 Create a project pipeline

    Next, create a configuration file for your project. This a YAML file where you specify instructions for GtiLab 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.
      1. on the left sidebar, select search or go to find your project or group.
      2. select the plus icon (+), then select new file.
      3. in the filename field, enter .gitlab-ci.yml
      4. in the large text box, paste this sample configuration:

      stages:

       - build

       - test

      job_build:

        stage: build

        script:

           - echo "Building the project"

      job_test:

        stage: test

        script:

            - echo "Running tests"

      1.3 Create and register a project runner

       Next, create a project runner and register it. You must register the runner to link it ot GitLab so that it can pick up jobs from the project pipeline.

      To create a project runner:

      1. on the left sidebar, select search or go to and find your project,
      2. select settings > CI/CD
      3. expand the runners section
      4. select new project runner
      5. select your operating system
      6. in the tags section, select the Run untagged checkbox. Tags specify which jobs the runner can run an are optional.
      7. select create runner
      8. Follow the on-screen instructions to register the runner from the command line. When prompted: 1) For executor, because your runner will run directly on the host computer, enter shell. The executor is the environment where the runner executes the job. 2) For GitLab instance URL, use the URL for your GitLab instance. For example, if your project is hosted on gitlab.example.com/yourname/yourproject, then your GitLab instance URL is https://gitlab.example.com. If your project is hosted on GitLab.com, the URL is https://gitlab.com.
      9. start your runner: gitlab-runner run

      1.4 check the runner configuration file

      After you register the runner, the configuration and runner authentication token is saved to your config.toml. The runner uses the token to authenticate with GitLab when picking up jobs from the job queue. 

      You can use the config.toml to define more advanced runner configurations.

      Here's what your config.toml should look like after you register and start the runner:

      [[runners]]

      name = "my-project-runner1"

      url = "http://127.0.0.1:3000"

      id = 38 token = "glrt-TOKEN"

      token_obtained_at = 2023-07-05T08:56:33Z

      token_expires_at = 0001-01-01T00:00:00Z

      executor = "shell"

      1.5 Trigger a pipeline to run your runner

      Next, trigger a pipeline in your project so that you can view your runner exexute a job.

      1. on the left sidebar, select search or go to and find your project.
      2. seclect Build > pipelines
      3. select Run pipeline.
      4. select a job to view the job log. The output should look similar to this example, which shows your runner successfully executing the job:

      Running with gitlab-runner 16.2.0 (782e15da)

      on my-project-runner TOKEN, system ID: SYSTEM ID

      Preparing the "shell" executor

      00:00

      Using Shell (bash) executor...

      Preparing environment

      00:00

      /Users/username/.bash_profile: line 9: setopt: command not found

      Running on MACHINE-NAME...

      Getting source from Git repository

      00:01

      /Users/username/.bash_profile: line 9: setopt: command not found

      Fetching changes with git depth set to 20...

      Reinitialized existing Git repository in /Users/username/project-repository

      Checking out 7226fc70 as detached HEAD (ref is main)...

      Skipping object checkout, Git LFS is not installed for this repository.

      Consider installing it with 'git lfs install'.

      Skipping Git submodules setup

      Executing "step_script" stage of the job script

      00:00

      /Users/username/.bash_profile: line 9: setopt: command not found $ echo "

      Building the project"

      Building the project

      Job succeeded

      2 Install GitLab runner

      GitLab Runner runs the CI/CD jobs that are defined in GitLab.

      CI/CD runners

      You can install GitLab Runner on your infrastructure, inside a Docker container, or deployed to a Kubuernetes cluster.

      GitLab Runner is open-source and written in Go. It can run as a single binary and has no language-specific requirements.

      After you install GitLab Runner, you must create and register runners with you GitLab instance. The GitLab instance can be self-managed or you can use GitLab.com.

      GitLab Runner can be installed and used on GNU/Linux, macOS, FreeBSD, and Windows. You can install it:

      • In a container.
      • By downloading a binary manually.
      • By using a repository for rpm/deb packages

        For security and performance reasons, you should install GitLab Runner on a machine that is separate to the machine that hosts your GitLab instance

VPS购买请点击我

免责声明:我们致力于保护作者版权,注重分享,被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理! 图片声明:本站部分配图来自人工智能系统AI生成,觅知网授权图片,PxHere摄影无版权图库和百度,360,搜狗等多加搜索引擎自动关键词搜索配图,如有侵权的图片,请第一时间联系我们,邮箱:ciyunidc@ciyunshuju.com。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

目录[+]