补充一下前文中提到的更多研究内容,此套食用方案满足小型项目的CI/CD

步骤一 安装jenkins

docker pull jenkins/jenkins:lts-jdk11
  • 创建Jenkins容器,这里推荐docker-compose创建, docker-compose.yml如下
version: '3'
services:
  jenkins:
    image: jenkins/jenkins:lts-jdk11 
    build: .
    container_name: myjenkins
    restart: always
    ports:
      - '127.0.0.1:9999:8080'
      - '127.0.0.1:50000:50000'
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/bin/docker:/usr/bin/docker
      - /usr/local/bin/docker-compose:/usr/local/bin/docker-compose
      - /home/jenkins-home:/var/jenkins_home

如果没有配nginx,ports中直接设置为

    ports:
      - '8080:8080'
      - '50000:50000'

将jenkins主目录挂在到宿主机的/home/jenkins-home(自己能记住就行)

由于我们即将部署的web项目是采用docker部署,所以将宿主机中的docker和docker-compose一并映射进jenkins容器中

此时浏览器访问服务器的8080端口,(配了域名和nginx的直接访问),即可进入jenkins的初始化界面,一顿设置后,进入dashboard

步骤二 安装Jenkins插件,配置webhook

仓库在码云的话,安装gitee 的插件,该插件可以实现push,PR等构建触发器

webhook配置原理同上文一致,在jenkins中生成sign

将webhook调用的地址配置在码云仓库里面,带上sign

步骤三 在项目中添加构建脚本

  • Dockerfile 镜像的构建脚本

以本项目为例是基于NextJs的ssr项目,采用官方dockerfile,添加了sitemap的生成脚本,其余没变,自行编写自己的dockerfile

# Install dependencies only when needed
FROM node:alpine AS deps
# Check https://github.com/nodejs/docker-node/tree/b4117f9333da4138b03a546ec926ef50a31506c3#nodealpine to understand why libc6-compat might be needed.
RUN apk add --no-cache libc6-compat
WORKDIR /app
COPY package.json ./
RUN yarn install --frozen-lockfile

# Rebuild the source code only when needed
FROM node:alpine AS builder
WORKDIR /app
COPY . .
COPY --from=deps /app/node_modules ./node_modules
RUN yarn build
RUN yarn postbuild

# Production image, copy all the files and run next
FROM node:alpine AS runner
WORKDIR /app

ENV NODE_ENV production

# You only need to copy next.config.js if you are NOT using the default configuration
# COPY --from=builder /app/next.config.js ./
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next ./.next
COPY --from=builder /app/node_modules ./node_modules

RUN addgroup -g 1001 -S nodejs
RUN adduser -S nextjs -u 1001
RUN chown -R nextjs:nodejs /app/.next
USER nextjs

EXPOSE 3000

# Next.js collects completely anonymous telemetry data about general usage.
# Learn more here: https://nextjs.org/telemetry
# Uncomment the following line in case you want to disable telemetry.
# RUN npx next telemetry disable

CMD ["node_modules/.bin/next", "start"]
  • docker-compose.yml 容器的运行脚本
version: '3'
services:
  fishing-funds-home:
    image: fishing-funds-home:latest
    build: .
    container_name: fishing-funds-home
    environment: #环境
      - TZ=Asia/Shanghai #设置时区
    restart: always
    ports:
      - 'xxx.xxx.xxx.xxx:xxxx'
  • Jenkinsfile jenkins pipline 构建脚本

pipeline {
  agent any
  environment {
    DOCKER_HOST = 'tcp://xxx.xxx.xxx.xxx:xx'
  }
  stages {
     stage('Build') {
      steps {
          sh "/usr/bin/docker build -t fishing-funds-home:latest . "
      }
     }
     stage('Deploy') {
      steps {
          sh "/usr/local/bin/docker-compose up -d"
      }
     }
  }
}

提交代码后,jenkins就开始自动构建了

console output中可以查看详细打包信息

至此,基于WebHook + Jenkins Pipline + Docker的CI/CD自动构建就完成了

发布结果:Fishing Funds官网

3 个评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注