通过docker部署gitlab和gitlab-runner

通过docker部署gitlab和gitlab-runner

文章目录

  • 通过docker部署gitlab和gitlab-runner
    • centos7上安装docker和docker-compose
    • 安装gitlab
    • 安装gitlab-runner
    • 配置gitlab-runner连上gitlab
    • 提交项目代码完成CI/CD

这里写的是通过docker-compose部署gitlab和gitlab-runner

centos7上安装docker和docker-compose

直接通过yum安装最新版本docker,为确保 yum 包更新到最新

$ sudo yum update

卸载旧版本(如果安装过旧版本的话)

$ sudo yum remove docker  docker-common docker-selinux docker-engine

安装需要的软件包, yum-util 提供yum-config-manager功能,另外两个是devicemapper驱动依赖的

$ sudo yum install -y yum-utils device-mapper-persistent-data lvm2

设置yum源

$ sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

img

可以查看所有仓库中所有docker版本,并选择特定版本安装

$ yum list docker-ce --showduplicates | sort -r

img

安装docker

$ sudo yum install docker-ce  #由于repo中默认只开启stable仓库,故这里安装的是最新稳定版17.12.0
$ sudo yum install <FQPN>  # 例如:sudo yum install docker-ce-17.12.0.ce
如果错误:
问题: package docker-ce-3:19.03.8-3.el7.x86_64 requires containerd.io >= 1.2.2-3, but none of the providers can be installed
解决:https://www.cnblogs.com/suanmiaoup/p/12772140.html
输入:yum install -y https://mirrors.aliyun.com/docker-ce/linux/centos/7/x86_64/edge/Packages/containerd.io-1.2.6-3.3.el7.x86_64.rpm
再输入:yum install docker-ce docker-ce-cli containerd.io

启动并加入开机启动

$ sudo systemctl start docker
$ sudo systemctl enable docker

安装gitlab

先查看gitlab所有镜像

docker search gitlab

通过docker部署gitlab和gitlab-runner_第1张图片

图中镜像gitlab/gitlab-ce是官网的版本,这里有使用twang2218/gitlab-ce-zh汉化版镜像

利用docker-compose.yml启动

version: '3'
services:
  gitlab:
    image: twang2218/gitlab-ce-zh
    container_name: gitlab-ce-zh
    restart: always
    hostname: '192.168.90.6'
    environment:
      GITLAB_OMNIBUS_CONFIG: |
        external_url "http://192.168.90.6:9090"  # gitlab访问的链接
        gitlab_rails['gitlab_shell_ssh_port'] = 2222 # 代码仓库拉取的端口
        gitlab_rails['gitlab_email_enabled'] = true
        gitlab_rails['gitlab_email_from'] = '[email protected]'
        gitlab_rails['gitlab_email_display_name'] = 'hfy'
        gitlab_rails['gitlab_email_reply_to'] = '[email protected]'
        gitlab_rails['smtp_enable'] = true
        gitlab_rails['smtp_address'] = "smtp.mxhichina.com"
        gitlab_rails['smtp_port'] = 465
        gitlab_rails['smtp_user_name'] = "[email protected]"
        gitlab_rails['smtp_password'] = "137801443aa"
        gitlab_rails['smtp_domain'] = "http://192.168.90.6"
        gitlab_rails['smtp_authentication'] = "login"
        gitlab_rails['smtp_enable_starttls_auto'] = true
        gitlab_rails['smtp_openssl_verify_mode'] = 'peer'
        gitlab_rails['smtp_tls'] = true
    ports:
      - '9090:9090'
      - '2222:22'
    volumes:
      - '/data/gitlab/config:/etc/gitlab'
      - '/data/gitlab/logs:/var/log/gitlab'
      - '/data/gitlab/data:/var/opt/gitlab'
    logging:
      driver: "json-file"
      options:
        max-size: "20m"
        max-file: "10"

networks:
  backend:
    driver: bridge

安装编译的时间会比较久,要耐心等待。安装后打开访问链接,第一次需要设置密码
通过docker部署gitlab和gitlab-runner_第2张图片

安装gitlab-runner

先查看gitlab所有镜像

docker search gitlab-runner

通过docker部署gitlab和gitlab-runner_第3张图片

利用docker-compose.yml启动

version: '3'
services:
    runner:
      image: 'gitlab/gitlab-runner:latest'
      container_name: gitlab-runner
      restart: always
      volumes:
        - ./conf:/etc/gitlab-runner
        - ./data/gitlab-running-docker.sock:/var/run/docker.sock
        - ${SOURCE_DIR}:/home/www/:rw  # 项目地址
        - ${DEPLOY_SH}:/home/deploy.sh:ro  # .gitlab-ci.yml在job时执行的sh文件
        - ${DEPLOY_PROJECTS}:/home/projects/:rw # 项目约定好一些配置文件

networks:
  backend:
    driver: bridge

注意:gitlab-runner的版本要和gitlab尽量对应不然会出现连不上gitlab

配置gitlab-runner连上gitlab

先在项目的 管理区域->runners中可以找到(这里注册的是share类型runner)
通过docker部署gitlab和gitlab-runner_第4张图片

注册gitlab-runner

docker exec -it gitlab-runner gitlab-runner register

(1)Please enter the gitlab-ci coordinator URL(输入您的GitLab实例URL)
​ http://192.168.90.6:9090 (安装的GitLab地址)
(2)Please enter the gitlab-ci token for this runner(输入 令牌)
上面图片中的token
(3)Please enter the gitlab-ci description for this runner(输入Runner的描述,您可以稍后在GitLab的UI中更改)
​ this is test
(4)Please enter the gitlab-ci tags for this runner (输入与Runner关联的标签,您可以稍后在GitLab的UI中更改)
(5)Please enter the executor: ssh, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
​ shell
(6)Please enter the Docker image (eg. ruby:2.1):
alpine:latest
通过docker部署gitlab和gitlab-runner_第5张图片

同时可以在gitlab-runner/config下可以看到生成一个config.toml的文件,文件内容如下:

concurrent = 1
check_interval = 0

[session_server]
  session_timeout = 1800

[[runners]]
  name = "this is test"
  url = "http://192.168.90.6:9090/"
  token = "d2c4999f1b08e288e21b24d5698c27"
  executor = "shell"
  [runners.custom_build_dir]
  [runners.cache]
    [runners.cache.s3]
    [runners.cache.gcs]

或者直接先配置好,gitlab-runner运行时会自动加载该配置文件

参考地址:https://docs.gitlab.com/ee/ci/docker/using_docker_build.html

提交项目代码完成CI/CD

在项目根目录下添加.gitlab-ci.yml,提交项目代码后会自动运行该文件打包项目

before_script:
  - echo "每个job之前都会执行"    

after_script:
  - echo "每个job之后都会执行"   

to-testing:
  stage: deploy
  only:
    - testing
    - /^testing_.*$/ #正则表达式,只有testing_开头的分支才会执行
  script:
    - /bin/bash /home/deploy.sh
  tags:
    - deploy_develop
  environment:
    name: testing

你可能感兴趣的:(gitlab,docker,shell)