zoukankan      html  css  js  c++  java
  • docker安装gitlab并部署CICD

    摘要

    本文主要实现了在docker下安装gitlab,将gitlab绑定在宿主机的180端口,将gitlab的clone的URL添加指定端口号;部署了CI/CD,并公布了测试项目。

    安装docker[1]

    1. 删除旧版本的docker(如果未安装则忽略)
    sudo apt-get remove docker docker-engine docker.io containerd runc
    
    1. 安装依赖

      sudo apt-get update
      sudo apt-get install 
          apt-transport-https 
          ca-certificates 
          curl 
          gnupg-agent 
          software-properties-common
      
    2. Add Docker’s official GPG key

      curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
      
    3. 添加仓库

      # x86_64
      sudo add-apt-repository 
         "deb [arch=amd64] https://download.docker.com/linux/ubuntu 
         $(lsb_release -cs) 
         stable"
      
      # armf
      sudo add-apt-repository 
         "deb [arch=armhf] https://download.docker.com/linux/ubuntu 
         $(lsb_release -cs) 
         stable"
      
      # arm64
      sudo add-apt-repository 
         "deb [arch=arm64] https://download.docker.com/linux/ubuntu 
         $(lsb_release -cs) 
         stable"
      
    4. 安装

      sudo apt-get update
      sudo apt-get install docker-ce docker-ce-cli containerd.io
      

    docker换源

    1. 注册阿里云账号

    2. 选择容器镜像服务

    3. 1596620950317

    4. 执行命令

      sudo mkdir -p /etc/docker
      sudo tee /etc/docker/daemon.json <<-'EOF'
      {
        "registry-mirrors": ["https://uy81mm79.mirror.aliyuncs.com"]
      }
      EOF
      sudo systemctl daemon-reload
      sudo systemctl restart docker
      

    docker安装gitlab[2] [3]

    创建运行目录

    sudo mkdir /opt/gitlab_docker
    cd /opt/gitlab_docker
    

    安装docker-compose

    sudo apt install docker-compose
    

    编写docker-compose.yml

    version: '3.1'
    services:
     gitlab:
      image: 'gitlab/gitlab-ce'
      container_name: "gitlab"
      restart: always
      privileged: true
      hostname: 'gitlab'
      environment:
       TZ: 'Asia/Shanghai'
       GITLAB_OMNIBUS_CONFIG: |
        external_url 'http://192.168.196.1'
        gitlab_rails['time_zone'] = 'Asia/Shanghai'
        gitlab_rails['smtp_enable'] = true
        gitlab_rails['gitlab_shell_ssh_port'] = 22
      ports:
       - '181:80'
       - '180:180'
       - '1443:443'
       - '22:22'
      volumes:
       - /opt/gitlab_docker/config:/etc/gitlab
       - /opt/gitlab_docker/data:/var/opt/gitlab
       - /opt/gitlab_docker/logs:/var/log/gitlab
    

    安装gitlab镜像

    sudo docker-compose up -d
    

    修改文件配置

    sudo vim /opt/gitlab_docker/config/gitlab.rb
    
    1. 修改nginx['listen_port']

      nginx['listen_port'] = 180
      
    2. 修改external_url

      external_url 'http://192.168.196.1:180'
      
    3. 重新启动

      sudo docker exec -it $CONTINER_ID gitlab-ctl restart
      

    CI/CD依赖

    使用docker安装gitlab-runner也是可以的,但是感觉有bug,可能会不成功,这里介绍了我成功的方法。

    安装gitlab-runner

    找到对应包下载安装。下载地址https://gitlab-runner-downloads.s3.amazonaws.com/latest/index.html.

    sudo dpkg -i gitlab-runner_amd64.deb
    

    启动运行gitlab-runner

    # 允许开机自启动
    systemctl enable gitlab-runner
    # 启动服务
    systemctl start gitlab-runner
    

    项目测试

    创建项目

    新建项目,项目文件如下:

    1596622530681

    .gitlab-ci.yml

    # This file is a template, and might need editing before it works on your project.
    # use the official gcc image, based on debian
    # can use verions as well, like gcc:5.2
    # see https://hub.docker.com/_/gcc/
    image: ubuntu:bionic
    
    build:
      stage: build
      # instead of calling g++ directly you can also use some build toolkit like make
      # install the necessary build tools when needed
      # before_script:
      #   - apt update && apt -y install make autoconf
      script:
        - g++ hello.cpp -o mybinary
      artifacts:
        paths:
          - mybinary
          # depending on your build setup it's most likely a good idea to cache outputs to reduce the build time
          # cache:
          #   paths:
          #     - "*.o"
    
    # run tests using the binary built before
    test:
      stage: test
      script:
        - chmod +x runmytests.sh
        - ./runmytests.sh
    

    hello.cpp

    #include <iostream>
    using namespace std;
    int main()
    {
        cout << "hello world" << endl;
        return 0;
    }
    

    runmytests.sh

    ./mybinary
    

    项目文件说明

    .gitlab-ci.yml就是自动化测试的配置文件,相关内容可参见[4],建议大家仔细阅读该配置文件即可理解该项目。

    gitlab-runner注册

    查找api

    1596623036712

    1596623061206

    注册

    执行命令:

    sudo gitlab-runner register
    

    输入相关参数,下面内容引用自[2:1]

    # 输入 GitLab 地址
    Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
    http://192.168.199.109/
     
    # 输入 GitLab Token
    Please enter the gitlab-ci token for this runner:
    1Lxq_f1NRfCfeNbE5WRh
     
    # 输入 Runner 的说明
    Please enter the gitlab-ci description for this runner:
    可以为空
     
    # 设置 Tag,可以用于指定在构建规定的 tag 时触发 ci
    Please enter the gitlab-ci tags for this runner (comma separated):
    deploy
     
    # 这里选择 true ,可以用于代码上传后直接执行(根据版本,也会没有此选项)
    Whether to run untagged builds [true/false]:
    true
     
    # 这里选择 false,可以直接回车,默认为 false(根据版本,也会没有此选项)
    Whether to lock Runner to current project [true/false]:
    false
     
    # 选择 runner 执行器,这里我们选择的是 shell
    Please enter the executor: virtualbox, docker+machine, parallels, shell, ssh, docker-ssh+machine, kubernetes, docker, docker-ssh:
    shell
    

    重启gitlab-runner

    systemctl restart gitlab-runner
    

    自动测试

    此时,应该是已经自动进行了测试了:

    1596623433039

    参考文献

    注意:[2:2][3:1]的视频笔记,因此需要配合[3:2]使用。


    1. Install Docker Engine on Ubuntu ↩︎

    2. Docker-2020最新超详细版教程通俗易懂【显哥出品,必为精品】 ↩︎ ↩︎ ↩︎

    3. 2020 Docker最新超详细版教程通俗易懂 ↩︎ ↩︎ ↩︎

    4. GitLab CI/CD pipeline configuration reference ↩︎

  • 相关阅读:
    Scala的基本类型和操作
    spark-submit部署应用的相关参数详解
    Spark的相关概念说明和检查点机制
    解决网站需要cookies登录和内容需要动态加载问题
    python 多线程
    re
    echarts中的图表大小自适应
    git过滤提交文件
    响应式媒体
    爬过的坑
  • 原文地址:https://www.cnblogs.com/harrypotterjackson/p/13444533.html
Copyright © 2011-2022 走看看