gitlabrunner做持续集成

gitlabrunner做持续集成

使用 GitLab Runner Docker

环境准备

  • 创建工作目录 /usr/local/docker/runner
  • 创建构建目录 /usr/local/docker/runner/environment
  • 下载 jdk-8u152-linux-x64.tar.gz 并复制到 /usr/local/docker/runner/environment

Dockerfile

在 /usr/local/docker/runner/environment 目录下创建 Dockerfile
FROM gitlab/gitlab-runner:v11.0.2
MAINTAINER Lusifer <topsale@vip.qq.com>

# 修改软件源
RUN echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial main restricted universe multiverse' > /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-security main restricted universe multiverse' >> /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-updates main restricted universe multiverse' >> /etc/apt/sources.list && \
    echo 'deb http://mirrors.aliyun.com/ubuntu/ xenial-backports main restricted universe multiverse' >> /etc/apt/sources.list && \
    apt-get update -y && \
    apt-get clean

# 安装 Docker
RUN apt-get -y install apt-transport-https ca-certificates curl software-properties-common && \
    curl -fsSL http://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | apt-key add - && \
    add-apt-repository "deb [arch=amd64] http://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" && \
    apt-get update -y && \
    apt-get install -y docker-ce
COPY daemon.json /etc/docker/daemon.json

# 安装 Docker Compose
WORKDIR /usr/local/bin
RUN wget https://raw.githubusercontent.com/topsale/resources/master/docker/docker-compose
RUN chmod +x docker-compose

# 安装 Java
RUN mkdir -p /usr/local/java
WORKDIR /usr/local/java
COPY jdk-8u152-linux-x64.tar.gz /usr/local/java
RUN tar -zxvf jdk-8u152-linux-x64.tar.gz && \
    rm -fr jdk-8u152-linux-x64.tar.gz

# 安装 Maven
RUN mkdir -p /usr/local/maven
WORKDIR /usr/local/maven
RUN wget https://raw.githubusercontent.com/topsale/resources/master/maven/apache-maven-3.5.3-bin.tar.gz
# COPY apache-maven-3.5.3-bin.tar.gz /usr/local/maven
RUN tar -zxvf apache-maven-3.5.3-bin.tar.gz && \
    rm -fr apache-maven-3.5.3-bin.tar.gz
# COPY settings.xml /usr/local/maven/apache-maven-3.5.3/conf/settings.xml

# 配置环境变量
ENV JAVA_HOME /usr/local/java/jdk1.8.0_152
ENV MAVEN_HOME /usr/local/maven/apache-maven-3.5.3
ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin

WORKDIR /

daemon.json

在 /usr/local/docker/runner/environment 目录下创建 daemon.json,用于配置加速器和仓库地址

{
  "registry-mirrors": [
    "https://registry.docker-cn.com"
  ],
  "insecure-registries": [
    "192.168.75.131:5000"
  ]
}

docker-compose.yml

在 /usr/local/docker/runner 目录下创建 docker-compose.yml

version: '3.1'
services:
  gitlab-runner:
    build: environment
    restart: always
    container_name: gitlab-runner
    privileged: true
    volumes:
      - /usr/local/docker/runner/config:/etc/gitlab-runner
      - /var/run/docker.sock:/var/run/docker.sock

注册 Runner

docker exec -it gitlab-runner gitlab-runner register

# 输入 GitLab 地址
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
http://192.168.75.146:8080/

# 输入 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

附:项目配置 Dockerfile 案例

FROM openjdk:8-jre

MAINTAINER Lusifer <topsale@vip.qq.com>

ENV APP_VERSION 1.0.0-SNAPSHOT
ENV DOCKERIZE_VERSION v0.6.1
RUN wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz \
    && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz

RUN mkdir /app

COPY itoken-eureka-$APP_VERSION.jar /app/app.jar
ENTRYPOINT ["dockerize", "-timeout", "5m", "-wait", "tcp://192.168.75.128:8888", "java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/app.jar", "--spring.profiles.active=prod"]

EXPOSE 8761

项目配置 docker-compose 案例

version: '3.1'
services:
  itoken-eureka:
    restart: always
    image: 192.168.62.133:5000/itoken-eureka
    container_name: itoken-eureka
    ports:
      - 8761:8761
    networks:
      - eureka_network

networks:
  eureka_network:

项目配置 .gitlab-ci.yml 案例


    stages:
      - build
      - push
      - run
      - clean
    
    build:
      stage: build
      script:
        - /usr/local/maven/apache-maven-3.5.3/bin/mvn clean package
        - cp target/itoken-eureka-1.0.0-SNAPSHOT.jar docker
        - cd docker
        - docker build -t 192.168.62.133:5000/itoken-eureka .
    
    
    push:
      stage: push
      script:
        - docker push 192.168.62.133:5000/itoken-eureka
    
    run:
      stage: run
      script:
      - cd docker
      - docker-compose down
      - docker-compose up -d
    
    
    clean:
      stage: clean
      script:
      - docker rmi $(docker images -q -f dangling=true)
    ```
    
    
    ## 非docker版本
    #### Ubuntu 安装脚本:
    curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
    sudo apt-get update
    sudo apt-get install gitlab-ci-multi-runner
    
    
    注册 Runner
    [root@iZbp1fmnx8oyubksjdk7leZ gitbook]# gitlab-ci-multi-runner register
    Running in system-mode.                            
    
    Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
    http://192.168.75.146:8080/
    Please enter the gitlab-ci token for this runner:
    1Lxq_f1NRfCfeNbE5WRh
    Please enter the gitlab-ci description for this runner:
    [iZbp1fmnx8oyubksjdk7leZ]: deploy-gaming
    Please enter the gitlab-ci tags for this runner (comma separated):
    deploy
    Whether to run untagged builds [true/false]:
    [false]: true
    Whether to lock Runner to current project [true/false]:
    [false]: 
    Registering runner... succeeded                     runner=P_zfkhTb
    Please enter the executor: virtualbox, docker+machine, parallels, shell, ssh, docker-ssh+machine, kubernetes, docker, docker-ssh:
    shell
    Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 
    
    说明:
    gitlab-ci-multi-runner register:执行注册命令
    Please enter the gitlab-ci coordinator URL:输入 ci 地址
    Please enter the gitlab-ci token for this runner:输入 ci token
    Please enter the gitlab-ci description for this runner:输入 runner 名称
    Please enter the gitlab-ci tags for this runner:设置 tag
    Whether to run untagged builds:这里选择 true ,代码上传后会能够直接执行
    Whether to lock Runner to current project:直接回车,不用输入任何口令
    Please enter the executor:选择 runner 类型,这里我们选择的是 shell
    CI 的地址和令牌,在 项目 –> 设置 –> CI/CD –> Runner 设置:
    
    #### .gitlab-ci.yml
    
    
    
    stages:
      - install_deps
      - test
      - build
      - deploy_test
      - deploy_production
    
    cache:
      key: ${CI_BUILD_REF_NAME}
      paths:
        - node_modules/
        - dist/
    
    # 安装依赖
    install_deps:
      stage: install_deps
      only:
        - develop
        - master
      script:
        - npm install
    
    # 运行测试用例
    test:
      stage: test
      only:
        - develop
        - master
      script:
        - npm run test
    
    # 编译
    build:
      stage: build
      only:
        - develop
        - master
      script:
        - npm run clean
        - npm run build:client
        - npm run build:server
    
    # 部署测试服务器
    deploy_test:
      stage: deploy_test
      only:
        - develop
      script:
        - pm2 delete app || true
        - pm2 start app.js --name app
    
    # 部署生产服务器
    deploy_production:
      stage: deploy_production
      only:
        - master
      script:
        - bash scripts/deploy/deploy.sh

## 其它配置
### 安装完 GitLab Runner 后系统会增加一个 gitlab-runner 账户,我们将它加进 root 组:
#### gpasswd -a gitlab-runner root
配置需要操作目录的权限,比如你的 runner 要在 gaming 目录下操作
#### chmod 775 gaming	
由于我们的 shell 脚本中有执行 git pull 的命令,我们直接设置以 ssh 方式拉取代码:
su gitlab-runner
ssh-keygen -t rsa -C "你在 GitLab 上的邮箱地址"
cd 
cd .ssh
cat id_rsa.pub
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值