Gitlab Runner + Docker 自動部署前後端項目

語言: CN / TW / HK

highlight: atom-one-dark

描述

使用Gitlab Runner + Docker 自動部署前後端項目。每次項目提交時出發Gitlab Runner任務,然後將項目打包成Docker鏡像運行。

安裝軟件

  1. 安裝Docker

```shell 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

shell yum -y install git

  1. 安裝Gitlab Runner

```shell 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, 執行該命令後需要配置一些信息。 shell gitlab-runner register 配置步驟如下: image.png

註冊該項目所需的網址和token image.png

註冊成功後可以看到該runer image.png

編寫Dockerfile

```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

```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
```

出現的問題

shell 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, 執行該命令後需要配置一些信息。 shell gitlab-runner register 配置步驟如下: image.png

註冊該項目所需的網址和token image.png

註冊成功後可以看到該runer image.png

編寫Dockerfile

```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

```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倉部編譯打包項目

shell vim /etc/gitlab-runner/config.toml 找到該runnervolumes中添加“/data/maven/repository/:/root/.m2:rw”,配置本地maven倉部。之後運行gitlab-runner restart重啟Gitlab Runner

配置優先使用本地鏡像構建

shell vim /etc/gitlab-runner/config.toml 找到該runner添加pull_policy = ["if-not-present"]”配置優先使用本地鏡像構建。之後運行gitlab-runner restart重啟Gitlab Runner

配置後如下: image.png