GitLab(二) Runner CI/CD 持续集成浅尝

2024-10-14  本文已影响0人  申_9a33

前面我们使用docker 部署了一个私有的 gitlab, 我们需要配置一条流水线,在用户上传代码时,对代码进行校验,自动化编译和部署;GitLab Runner 已提供此功能


1. 安装 GitLab runner

1.2 Docker安装 编写 docker-compose.yml

version: '3.6'
services:
  gitlab:
    image: gitlab/gitlab-runner:latest
    container_name: gitlab-runner
    restart: always
    volumes:
      - './config:/etc/gitlab-runner'
      - './docker.sock:/var/run/docker.sock'

1.2 windows 安装

1.3 linux 安装

curl -L "https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh" | sudo bash
sudo apt-get install gitlab-runner

2. 新建项目 runner

企业微信截图_17289901852161.png

3. 注册 runners

3.1 docker 环境注册

docker exec -it gitlab-runner gitlab-runner register
企业微信截图_17289897511816.png

3.2 linux 环境注册

gitlab-runner register  --url http://gitlab.example.com:8929  --token glrt-GfpyZbFG43Y_v5B6yhyv

4.在项目中新建 .gitlab-ci.yml

build-job:
  stage: build
  tags:
    - build
  script:
    - echo "Hello, $GITLAB_USER_LOGIN!"

test-job1:
  stage: test
  tags:
    - build
  script:
    - echo "This job tests something"

test-job2:
  stage: test
  tags:
    - build
  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
  tags:
    - build
  script:
    - echo "This job deploys something from the $CI_COMMIT_BRANCH branch."
  environment: production

5. 提交代码,查看 runner 执行状态

企业微信截图_17289910684794.png
上一篇 下一篇

猜你喜欢

热点阅读