服务器问题

云服务器怎样搭建GitLab-CI持续集成环境

一、GitLab安装配置

1. 系统准备

  1. bash
    # 安装依赖
    apt update
    apt install -y curl openssh-server ca-certificates postfix

    # 添加GitLab源
    curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | bash

    # 安装GitLab
    apt install gitlab-ce

2. 初始配置

  1. ruby
    # /etc/gitlab/gitlab.rb
    external_url 'http://gitlab.example.com'
    gitlab_rails['gitlab_shell_ssh_port']=22
    gitlab_rails['time_zone']='Asia/Shanghai'

    # 应用配置
    gitlab-ctl reconfigure

二、Runner安装配置

1. Runner安装

  1. bash
    # 添加Runner源
    curl -"https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh"| bash

    # 安装Runner
    apt install gitlab-runner

    # 启动服务
    systemctl enable gitlab-runner
    systemctl start gitlab-runner

2. Runner注册

  1. bash
    # 注册Runner
    gitlab-runner register \
    --url "https://gitlab.example.com/" \
    --registration-token "PROJECT_REGISTRATION_TOKEN" \
    --description "docker-runner" \
    --executor "docker" \
    --docker-image "docker:latest"

三、CI/CD配置

1. GitLab CI配置

  1. yaml
    # .gitlab-ci.yml
    stages:
    - build
    - test
    - deploy

    build_job:
      stage: build
      script:
    - mvn clean package
      artifacts:
        paths:
    - target/*.jar

    test_job:
      stage: test
      script:
        - mvn test

    deploy_job:
      stage: deploy
      script:
        - bash deploy.sh
      only:
        - master

2. Runner执行器配置

  1. toml
    # config.toml
    [[runners]]
      name ="docker-runner"
      url ="https://gitlab.example.com/"
      token ="PROJECT_TOKEN"
      executor ="docker"
    [runners.docker]
        tls_verify =false
        image ="docker:latest"
        privileged =true
        disable_cache =false
        volumes =["/cache"]

四、环境变量与缓存

1. 变量配置

  1. yaml
    variables:
      MAVEN_OPTS:"-Dmaven.repo.local=.m2/repository"
      DOCKER_HOST:"tcp://docker:2375"

    cache:
      key: ${CI_COMMIT_REF_SLUG}
      paths:
    -.m2/repository/
    - node_modules/

2. Docker缓存

  1. yaml
    build:
      stage: build
      script:
    - docker build -t $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA .
    - docker push $CI_REGISTRY_IMAGE:$CI_COMMIT_SHA
      cache:
        paths:
    -.docker

五、Pipeline优化

1. 并行任务

  1. yaml
    test_unit:
      stage: test
      parallel:3
      script:
    - mvn test -Dtest=TestClass

    test_integration:
      stage: test
      script:
    - mvn verify

2. 条件执行

  1. yaml
    deploy_prod:
      stage: deploy
      script:
    - deploy_to_production
      rules:
    -if:'$CI_COMMIT_BRANCH == "master"'
    when: manual
    -when: never

六、监控与通知

1. 监控配置

  1. ruby
    # gitlab.rb
    prometheus['enable']=true
    grafana['enable']=true

    # 应用配置
    gitlab-ctl reconfigure

2. 通知配置

  1. yaml
    notification:
      stage:.post
      script:
    -|
          curl -X POST -'Content-Type: application/json' \
    --data '{"text":"Pipeline finished"}' \
          $WEBHOOK_URL
      rules:
    -when: always



免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:bkook@qq.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
上一篇:云服务器怎样部署Doris分析型数据库
下一篇:云服务器怎样部署与配置SonarQube代码质量平台
0

在线
客服

在线客服服务时间:9:00-18:00

客服
热线

19899115815
7*24小时客服服务热线

关注
微信

关注官方微信
顶部