描绘

运用Gitlab Runner + Docker 主动布置前后端项目。每次项目提交时动身Gitlab Runner使命,然后将项目打包成Docker镜像运转。

装置软件

  1. 装置Docker
yum update
yum remove docker  docker-common docker-selinux docker-engine
yum install -y yum-utils device-mapper-persistent-data lvm2
yum install -y docker
yum list installed | grep docker
systemctl start docker
systemctl status docker
  1. 装置git
yum -y install git
  1. 装置Gitlab Runner
wget -O /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64
chmod +x /usr/local/bin/gitlab-runner
useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
gitlab-runner start

如果呈现 过错,履行以下指令 sudo fuser /usr/local/bin/gitlab-runner kill -9 xxx

主动布置前端Vue项目

  1. 注册一个runner 运用gitlab-runner register注册一个runner, 履行该指令后需要装备一些信息。
gitlab-runner register

装备过程如下:

Gitlab Runner + Docker 自动部署前后端项目

注册该项目所需的网址和token

Gitlab Runner + Docker 自动部署前后端项目

注册成功后可以看到该runer

Gitlab Runner + Docker 自动部署前后端项目

编写Dockerfile

# n-base 是自己基于node构建的项目,里边包含了一些装备文件
FROM n-base as builder
WORKDIR /web
COPY . .
RUN cp /config/config.ts ./src/utils/http && cp /config/default.conf ./default.conf
RUN npm install --registry=https://registry.npm.taobao.org --no-package-lock --no-save
RUN npm run build
FROM nginx:alpine
RUN cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime \
    && echo "Asia/Shanghai" > /etc/timezone
WORKDIR /web
COPY  --from=builder /web/default.conf /etc/nginx/conf.d/default.conf
COPY  --from=builder /web/dist /web
EXPOSE 80

编写gitlab-ci.yml

image: node:14.19.1-alpine
stages: # 分段
  - deploy
job_deploy:
    image: docker
    stage: deploy
    script:
      - docker build -t image-name .
      - if [ $(docker ps -aq --filter name=image-name) ]; then docker rm -f image-name;fi
      - docker run -d -p 8081:80 --name project-name image-name
    only: 
      - main  

呈现的问题

vim /etc/gitlab-runner/config.toml

找到该runnervolumes中增加"/usr/bin/docker:/usr/bin/docker", "/var/run/docker.sock:/var/run/docker.sock"。之后运转gitlab-runner restart重启Gitlab Runner

主动布置后端Spring Boot项目

  1. 注册一个runner 运用gitlab-runner register注册一个runner, 履行该指令后需要装备一些信息。
gitlab-runner register

装备过程如下:

Gitlab Runner + Docker 自动部署前后端项目

注册该项目所需的网址和token

Gitlab Runner + Docker 自动部署前后端项目

注册成功后可以看到该runer

Gitlab Runner + Docker 自动部署前后端项目

编写Dockerfile

# 指定基础镜像,这是多阶段构建的前期阶段
FROM openjdk:17 as builder
# 指定作业目录,目录不存在会主动创立
WORKDIR /app
# 将生成的 jar 复制到容器镜像中
COPY target/*.jar application.jar
# 经过东西spring-boot-jarmode-layertools从application.jar中提取拆分后的构建成果
RUN java -Djarmode=layertools -jar application.jar extract
# 正式构建镜像
FROM openjdk:17
# 指定作业目录,目录不存在会主动创立
WORKDIR /app
# 前一阶段从jar中提取除了多个文件,这里分别履行COPY指令复制到镜像空间中,每次COPY都是一个layer
COPY --from=builder app/dependencies ./
COPY --from=builder app/spring-boot-loader ./
COPY --from=builder app/snapshot-dependencies ./
COPY --from=builder app/application ./
# 指定时区
ENV TZ="Asia/Shanghai"
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone
# 界说一些环境变量,方便环境变量传参
ENV JVM_OPTS=""
ENV JAVA_OPTS=""
# 指定露出的端口,起到说明的作用,不指定也会露出对应端口
EXPOSE 8080
# 启动 jar 的指令
ENTRYPOINT ["sh","-c","java $JVM_OPTS $JAVA_OPTS org.springframework.boot.loader.JarLauncher"]

编写gitlab-ci.yml

before_script:
  - export IMAGE_FULL_NAME=image-name
stages:
  - compile
  - build
  - run
variables:
  PROJECT: "project-name"
  MAVEN_REPO: "/.m2"
job_compile:
  image: mvn-base:latest
  stage: compile
  allow_failure: false
  script:
    - cp /config/application-local.yml ./src/main/resources/application-local.yml
    - mvn -Dmaven.repo.local=$MAVEN_REPO clean package -P local -Dmaven.test.skip=true
  artifacts:
    name: $PROJECT
    expire_in: 7 days
    paths:
      - target/*.jar
  only:
    - main
job_build:
  image: docker:stable
  stage: build
  script:
    - docker build -t $IMAGE_FULL_NAME .
    - rm -rf target
  only:
    - main
job_run:
  image: docker:stable
  stage: run
  script:
    - CONTAINER_NAME=$(docker ps -aq --filter name=$PROJECT)
    - echo $CONTAINER_NAME
    - if [[ -n "$CONTAINER_NAME" ]]; then
      docker rm -f $CONTAINER_NAME;
      fi
    - docker run -d --name $PROJECT -p 8099:8099 $IMAGE_FULL_NAME
  only:
    - main

装备运用本地maven仓部编译打包项目

vim /etc/gitlab-runner/config.toml

找到该runnervolumes中增加“/data/maven/repository/:/root/.m2:rw”,装备本地maven仓部。之后运转gitlab-runner restart重启Gitlab Runner

装备优先运用本地镜像构建

vim /etc/gitlab-runner/config.toml

找到该runner增加pull_policy = ["if-not-present"]”装备优先运用本地镜像构建。之后运转gitlab-runner restart重启Gitlab Runner

装备后如下:

Gitlab Runner + Docker 自动部署前后端项目