gitlab CI/CD实践

212

gitlab CI/CD实践

image.png

1、centOS 安装docker

安装准备

 sudo yum install -y yum-utils
 sudo yum install psmisc
 sudo yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo

安装docker

sudo yum install docker-ce docker-ce-cli containerd.io

启动docker

sudo systemctl start docker

2、配置docker镜像

创建或修改 /etc/docker/daemon.json 文件,并写入以下内容

{ "registry-mirrors": [ "https://mirror.ccs.tencentyun.com" ] }

依次执行以下命令,重新启动docker服务。

systemctl daemon-reload
service docker restart

检查是否生效

docker info

3、以非 root 用户身份管理 Docker

创建docker组。

sudo groupadd docker

将您的用户添加到docker组中。

sudo usermod -aG docker $USER

在 Linux 上,您还可以运行以下命令来激活对组的更改:

newgrp docker 

4、安装gitlab-runner

拉取镜像

docker pull gitlab/gitlab-runner:alpine-v14.0.1

启动镜像脚本

su $USER
mkdir ~/docker_mnt
mkdir ~/docker_sh
mkdir docker_mnt/gitlab-runner
vim docker_sh/gitlab-runner.sh
vim docker_sh/gitlab-runner-register.sh
#/bin/bash

name="gitlab-runner"
docker rm -f $name
docker run -d \
       --name $name \
       --restart always \
       -v /home/jhit/docker_mnt/gitlab-runner/config:/etc/gitlab-runner \
       -v /var/run/docker.sock:/var/run/docker.sock \
       gitlab/gitlab-runner:alpine-v14.0.1

启动成功后注册到gitlab仓库

docker exec gitlab-runner gitlab-runner register -n \
       --url http://HIDE_MY_GIT_HOST/  \
       --registration-token HIDE_MY_TOKEN \
       --tag-list jhit_102 \
       --executor docker \
       --docker-image docker \
       --docker-volumes /root/.m2:/root/.m2 \
       --docker-volumes /root/.npm:/root/.npm \
       --docker-volumes /var/run/docker.sock:/var/run/docker.sock \
       --description "gitlab runner 172.20.203.102"

拉取常用镜像

docker pull openjdk:11
docker pull maven:3.8.5-openjdk-11

5、SpringBoot后端项目配置

Dockerfile配置

# 该镜像需要依赖的基础镜像
FROM openjdk:11
# 命令在下面的文件夹下运行
WORKDIR /home
# 复制文件
COPY /target/databoard-0.0.4-SNAPSHOT.jar databoard-0.0.4-SNAPSHOT.jar
# 声明服务运行在8093端口
EXPOSE 8011
# 指定docker容器启动时运行jar包
ENTRYPOINT ["java", "-jar", "databoard-0.0.4-SNAPSHOT.jar", "--spring.profiles.active=dev"]
# 指定维护者的名字
MAINTAINER kjj

.gitlab-ci.yml

stages:
  - package
  - deploy
start_package:
  image: maven:3.8.5-openjdk-11
  stage: package
  tags:
    - juhua
  cache:
    key: ${CI_PIPELINE_ID}
    paths:
      - target/
  script:
    - echo "Packaging application..."
    - mvn clean && mvn -Dfile.encoding=UTF-8 -DskipTests=true install && mvn -Dfile.encoding=UTF-8 -DskipTests=true package
    - echo "Application successfully packaged."
  only:
    refs:
      - master
deploy:
  stage: deploy
  tags:
    - juhua
  cache:
    key: ${CI_PIPELINE_ID}
    paths:
      - target/
  script:
    - echo "Deploying application..."
    - pwd
    - docker build . -t ${CI_PROJECT_NAME}:${CI_PIPELINE_ID}
    - docker stop zjrd-databoard && docker rm zjrd-databoard
    - docker run --name zjrd-databoard -p 7013:8011 -d ${CI_PROJECT_NAME}:${CI_PIPELINE_ID}
    - ls
    - echo "Application successfully deployed."
  only:
    refs:
      - master

6、Vue前端项目配置

//TODO