docker部署jenkins,持续构建vue工程

主要记录一下过程新的

docker环境

参考 菜鸟教程(https://www.runoob.com/docker/centos-docker-install.html)

安装所需的软件包。yum-utils 提供了 yum-config-manager ,并且 device mapper 存储驱动程序需要 device-mapper-persistent-data 和 lvm2。

sudo yum install -y yum-utils \
  device-mapper-persistent-data \
  lvm2

使用以下命令来设置稳定的仓库

# 官方镜像
sudo yum-config-manager \
    --add-repo \
    https://download.docker.com/linux/centos/docker-ce.repo

docker-compose环境

参考 菜鸟教程(https://www.runoob.com/docker/docker-compose.html)
安装包发布地址: https://github.com/docker/compose/releases

 sudo curl -L "https://github.com/docker/compose/releases/download/1.28.2/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
# 1.28.2 是版本号,可以去  https://github.com/docker/compose/releases 找最新的版本号

docker-compose.yml

version: '3.7'

networks:
  dep:

services:
  # docker 私服
  registry:
    image: registry:2
    container_name: registry
    restart: always
    networks:
      dep:
        aliases:
          - registry
    ports:
      - "5000:5000"
    volumes:
      - /var/lib/registry:/var/lib/registry
  # jenkins 部署
  jenkins:
    image: jenkins/jenkins:lts
    container_name: jenkins
    restart: always
    networks:
      dep:
        aliases:
          - jenkins
    ports:
      - "8080:8080"
      - "50000:50000"
    volumes:
      - /etc/localtime:/etc/localtime # 时区
      - /var/jenkins_home:/var/jenkins_home # 这个地方需要注意给jenkins_home权限,不然无法启动成功:执行:sudo chown -R 1000 jenkins_home
      - /usr/bin/docker:/usr/bin/docker
      - /var/run/docker.sock:/var/run/docker.sock
      - /usr/lib64/libltdl.so.7:/usr/lib/x86_64-linux-gnu/libltdl.so.7
    user: root # 解决容器中执行docker权限问题
  # 用来测试自动化构建的vue工程
  vue3play:
    image: 127.0.0.1:5000/vue3-play:latest
    container_name: vue3play
    restart: always
    networks:
      dep:
        aliases:
          - vue3play

jenkins docker命令问题,以及权限问题

参考:https://www.isolves.com/it/cxkf/rongqi/2020-11-06/32886.html

构建命令

选择自由风格:执行shell

pwd
docker build -t vue3-play .
docker tag vue3-play:latest 127.0.0.1:5000/vue3-play:latest
docker push 127.0.0.1:5000/vue3-play

ssh远程执行命令

cd /home/ly # 你的docker-compose.yml文件路径
sh restart-dc.sh

附上 restart-dc.sh 只是简单的启动docker-compose,然后清理none的image

#!/bin/bash
echo "--start docker-compose up -d"
cd /home/ly
docker-compose up -d
echo "--start rmi none image"
docker rmi $(docker images | grep "none" | awk '{print $3}')

vue3-play 参考来源于 vue-cli

Dockerfile

FROM node:14.15.4-alpine3.10
COPY ./ /app
WORKDIR /app
RUN npm config set sass_binary_site https://npm.taobao.org/mirrors/node-sass/
RUN npm install --registry=https://registry.npm.taobao.org && npm run build

FROM nginx
RUN mkdir /app
COPY --from=0 /app/dist /app
COPY nginx.conf /etc/nginx/nginx.conf

nginx.conf

user  nginx;
worker_processes  1;
error_log  /var/log/nginx/error.log warn;
pid        /var/run/nginx.pid;
events {
  worker_connections  1024;
}
http {
  include       /etc/nginx/mime.types;
  default_type  application/octet-stream;
  log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                    '$status $body_bytes_sent "$http_referer" '
                    '"$http_user_agent" "$http_x_forwarded_for"';
  access_log  /var/log/nginx/access.log  main;
  sendfile        on;
  keepalive_timeout  65;
  server {
    listen       80;
    server_name  localhost;
    location / {
      root   /app;
      index  index.html;
      try_files $uri $uri/ /index.html;
    }
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
      root   /usr/share/nginx/html;
    }
  }
}

github webhook

通过github给jenkins触发构建任务

参考:https://blog.csdn.net/qq_38588845/article/details/112404336

  1. 先配置github密钥
  2. jenkins添加github服务器,用密钥链接github
  3. github添加jenkins链接:http(s)://jenkins的地址/github-webhook/
  4. 构建项目中勾选 GitHub hook trigger for GITScm polling

你可能感兴趣的:(docker部署jenkins,持续构建vue工程)