gitlab-runner安装及使用教程

本文详细介绍了如何在CentOS、Ubuntu系统上安装GitLabRunner,以及如何在GitLab中注册和配置Runner,提供了一个运行GitLabJob的示例,包括构建、测试和部署阶段。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、安装gitlab-runner
1.1 如果系统是centos系列,直接使用以下命令安装gitlab-runner yum源,并安装gitlab-runner
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh" | sudo bash
yum install gitlab-runner
1.2 如果系统是centos变种,例如龙蜥8,可以使用以下命令安装gitlab-runner yum源,并安装gitlab-runner
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh" > script.rpm.sh
chmod +x script.rpm.sh
os=el dist=8 ./script.rpm.sh
yum install gitlab-runner
2.1 如果系统是ubuntu系列,直接使用以下命令安装gitlab-runner apt-get源,并安装gitlab-runner
curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
apt-get install gitlab-runner

二、注册gitlab-runner
1.1 进入gitlab admin area主界面,界面截图如下

1.2 点击CI/CD菜单下的Runners菜单,界面截图如下

1.3 点击New instance runner按钮,并填写必要的信息,界面截图如下

1.4 点击Create runner按钮,界面截图如下

1.5 在gitlab-runner安装好的机器中执行以下命令,输入注册名称,选择执行器类型,比如shell,即可注册成功
#--url为gitlab的external url地址
gitlab-runner register  --url https://newgitlab.com  --token glrt-GCTCASumzqyKyzrAA5qy
#如果gitlab的external url 地址是一个https 自签名域名,可以添加tls自签名证书信任,目录中需提前放好对应的域名证书
gitlab-runner register --url https://newgitlab.com --token glrt-GCTCASumzqyKyzrAA5qy --tls-ca-file="/etc/gitlab-runner/certs/newgitlab.com.crt"

三、运行gitlab job示例说明
1.1 打开测试仓库的Pipeline editor,截图如下

1.2 编辑.gitlab-ci.yml文件内容,.gitlab-ci.yml内容如下,点击Commit changes按钮即可运行gitlab job
# This file is a template, and might need editing before it works on your project.
# This is a sample GitLab CI/CD configuration file that should run without any modifications.
# It demonstrates a basic 3 stage CI/CD pipeline. Instead of real tests or scripts,
# it uses echo commands to simulate the pipeline execution.
#
# A pipeline is composed of independent jobs that run scripts, grouped into stages.
# Stages run in sequential order, but jobs within stages run in parallel.
#
# For more information, see: https://docs.gitlab.com/ee/ci/yaml/index.html#stages
#
# You can copy and paste this template into a new `.gitlab-ci.yml` file.
# You should not add this template to an existing `.gitlab-ci.yml` file by using the `include:` keyword.
#
# To contribute improvements to CI/CD templates, please follow the Development guide at:
# https://docs.gitlab.com/ee/development/cicd/templates.html
# This specific template is located at:
# https://gitlab.com/gitlab-org/gitlab/-/blob/master/lib/gitlab/ci/templates/Getting-Started.gitlab-ci.yml

stages:          # List of stages for jobs, and their order of execution
  - build
  # - test
  # - deploy
  - release

build-job:       # This job runs in the build stage, which runs first
  stage: build
  tags: # 可以运行此项任务的Runner tag 筛选
    - gitlab-release-ci # Runner的tag名
  script:
    - echo "Compiling the code..."
    - echo "Compile complete."

# unit-test-job:   # This job runs in the test stage.
#   stage: test    # It only starts when the job in the build stage completes successfully.
#   tags: # 可以运行此项任务的Runner tag 筛选
#     - gitlab-release-ci # Runner的tag名
#   script:
#     - echo "Running unit tests... This will take about 60 seconds."
#     #- sleep 60
#     - echo "Code coverage is 90%"

# lint-test-job:   # This job also runs in the test stage.
#   stage: test    # It can run at the same time as unit-test-job (in parallel).
#   tags: # 可以运行此项任务的Runner tag 筛选
#     - gitlab-release-ci # Runner的tag名
#   script:
#     - echo "Linting code... This will take about 10 seconds."
#     #- sleep 10
#     - echo "No lint issues found."

# deploy-job:      # This job runs in the deploy stage.
#   stage: deploy  # It only runs when *both* jobs in the test stage complete successfully.
#   tags: # 可以运行此项任务的Runner tag 筛选
#     - gitlab-release-ci # Runner的tag名
#   environment: production
#   script:
#     - echo "Deploying application..."
#     - echo "Application successfully deployed."

#将已有的二进制连接自动打包到release版本中
release-job:
  stage: release  # It only runs when *both* jobs in the test stage complete successfully.
  tags: # 可以运行此项任务的Runner tag 筛选
    - gitlab-release-ci # Runner的tag名
  rules:
    - if: $CI_COMMIT_TAG                 # Run this job when a tag is created  即创建新的tag
  environment: production
  script:
    - echo "running release_job"
  release:                               # See https://docs.gitlab.com/ee/ci/yaml/#release for available properties
    tag_name: '$CI_COMMIT_TAG'
    description: '$CI_COMMIT_TAG'
    assets:
      links:
        - name: 'rabbitmq_delayed_message_exchange-3.12.0.ez'
          url: 'https://newgitlab.com/gitlab-instance-85cd3587/repository/-/raw/main/rabbitmq/rabbitmq_delayed_message_exchange-3.12.0.ez?inline=false'
          filepath: '/rabbitmq/rabbitmq_delayed_message_exchange-3.12.0.ez'
          link_type: 'other'
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值