使用docker安装jenkins,然后使用jenkins本地发版和远程发版
1、安装docker
1.安装必要的一些系统工具
sudo yum install docker-ce
2.添加软件源信息
sudo yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo
3.更新yum索引列表并安装Docker引擎
sudo yum makecache fast
4.yum索引列表没有更新可以直接使用yum makecache(若有报错请翻阅第四部分第二条)
docker-ce:社区版的,docker-ee:企业版的
sudo yum install docker-ce
5.安装过程中会提示确认,输入y以确认
6.开启Docker服务
sudo service docker start
或者 systemctl start docker
7.测试是否安装成功
docker version
2、使用安装jenkins
- 启动docker的jenkins
#拉取镜像docker pull jenkins/jenkins
# -p端口映射,-v挂载工作目录 --env 指定一些配置 如--prefix是我要使用nginx代理,所以给jenkins加上前缀
# 第二个-v是挂载的maven,需要先在官网下载一个maven,把配置文件改为阿里云镜像,服务器上的路径是/datadrive/software/docker-config/jenkins/maven,jenkins容器内对应的是/home/maven路径
# 挂载jenkins_home时可能在启动时会报Permission denied,可以进入文件目录,使用chmod 777 /datadrive/software/docker-config/jenkins/jenkins_home:/var/jenkins_home 给这个文件夹权限
# git和apache-maven-3.6.3可以到jenkins的全局工具中选择自动安装,当然jdk也可以,这样就可以不挂载了docker run --name jenkins \-p 8003:8080 -p 50000:50000 \-v /datadrive/software/docker-config/jenkins/jenkins_home:/var/jenkins_home \-v /datadrive/software/apache-maven-3.6.3:/datadrive/software/apache-maven-3.6.3 \-v /datadrive/software/jdk1.8.0_301:/datadrive/software/jdk1.8.0_301 \-v /datadrive/software/git:/datadrive/software/git \-v /datadrive/:/datadrive/ \-v /run/docker.sock:/var/run/docker.sock \-v /usr/bin/docker:/usr/bin/docker \-v "$HOME":/home \--env JENKINS_OPTS="--prefix=/jenkins -Xmx512m" \-d jenkins/jenkins
- 构建jenkins容器如果出现错误
touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied
Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?
表示容器中的/var/jenkins_home路径对应的宿主机地址/datadrive/software/docker-config/jenkins/jenkins_home:/var/jenkins_home
的权限不足,需要把宿主机的地址权限使用chmod 777 xxx更改下,再启动容器就可以了 - 进入jenkins容器内
#jenkins账号登录进入docker exec -it jenkins bash#root账号登录进入docker exec -u 0 -it jenkins bashwhoami docker容器可以查看当前用户是否为root# 获取密码cat /var/jenkins_home/secrets/initialAdminPassword
- 构建流程
1.插件管理中选择Available plugins菜单搜索:Publish Over SSH插件下载
2.全局工具配置
1.如果是自动安装它的安装路径是/datadrive/software/docker-config/jenkins/jenkins_home/tools,注意是docker的映射路径jenkins_home
2.jdk安装,可以使用宿主机的jdk,但是启动docker的时候需要将宿主机的jdk映射到docker中;
3.安装Git installations
4.Maven安装,自动安装路径
3.cd /datadrive/software/docker-config/jenkins/jenkins_home/workspace这个路径是jenkins的存储路径,构建项目存储在这个路径下
4.凭据管理,新增git账号和linux账号的凭据
5.系统管理菜单
6.构建任务
1.新建任务,填写任务名称,选择构建一个自由风格的软件项目
2.配置git
3.选择Build Steps,按照顺序添加下面两个构建步骤
1.调用顶层 Maven 目标
2.执行 shell
7.构建vue前端
1.构建流程
1.插件管理中选择Available plugins菜单搜索:nodejs
2.全局工具配置NodeJS
3.凭据管理,新增git账号
2.构建任务
1.新建任务,填写任务名称,选择构建一个自由风格的软件项目
2.配置git
3.选择源码管理,搜索:Provide Node & npm bin/ folder to PATH菜单勾选上
4.选择Build Steps,执行shell
- 构建错误
1.git rev-parse --resolve-git-dir /var/jenkins_home/workspace/ai-admin/.git # timeout=10
可以忽略
2.构建项目出现错误:Exception:Auth fail for methods ‘publickey,gssapi-keyex,gssapi-with-mic,password’
chmod 700 /root/.ssh
chmod 600 /root/.ssh/authorized_keys
3.com.jcraft.jsch.JSchException: Session.connect: java.net.SocketTimeoutException: Read timed
timeout延长到100
4.symbol class base64encoder location package sun.misc
到全局工具中把jdk设置为1.8,指定jdk的 JAVA_HOME=/datadrive/software/jdk1.8.0_301,宿主机的映射路径
5.jenkins cp: cannot create regular file xxx Permission denied
使用sudo
6./tmp/jenkins17356870084788890789.sh: line 71: sudo: command not found
找不到sudo命令,需要使用root账号登录容器docker exec -u 0 -it jenkins bash,进行yum下载命令
7.bash: docker: command not found
-v /run/docker.sock:/var/run/docker.sock
-v /usr/bin/docker:/usr/bin/docker
8.dial unix /var/run/docker.sock: connect: permission denied
重启失效:sudo chmod 666 /var/run/docker.sock
9.bash: /usr/bin/docker: Permission denied
sudo chmod 777 /usr/bin/docker
10.Step 1/7 : FROM java:8
manifest for java:8 not found: manifest unknown: manifest unknown
DockerFile中的FROM镜像有问题
11.E: List directory /var/lib/apt/lists/partial is missing. - Acquire (13: Permission denied)
这里是权限报错,在exec进来的时候加上 -u 0 即可:
docker exec -u 0 -it jenkins bash
12.bash: yum: command not found
apt-get update
apt-get install sshpass 安装sshpass命令
13.jenkins执行shell执行完成后无法停止
是因为使用sshpass登录需要执行exit退出指令
14.stderr: fatal: unable to access ‘https://gitee.com/hahaha9/ai-project.git/’:Failed to connect to gitee.com port 443 after 255971 ms: Couldn’t connect to server
git仓库地址Repository URL有误,如果是gitee仓库有时候可以不稳定,到本地拉取提交下项目再试试
15.bash: npm: command not found
使用root账号登录容器:docker exec -u 0 -it jenkins bash
apt-get install npm 安装npm命令
16.scp: local “dist” is not a regular file
scp: failed to upload file dist to /datadrive/mnt/ai/web-admin/dist
解决办法:需要添加一个-r参数表示复制该文件夹下的所有文件和目录,下面是一个示例
sshpass -p ‘pwd’ scp -r -q -o StrictHostKeyChecking=no dist root@x.x.x.x:/web-admin/dist
8.Dockerfile和ssh脚本
1.Dockerfile
# 指定是基于哪个基础镜像
FROM openjdk:8-jdk# 作者信息
MAINTAINER wwj# 挂载点声明
VOLUME /tmp# 将本地的一个文件或目录,拷贝到容器的文件或目录里,这里注意目录结构,前面的是对应宿主机的目录
ADD ai-toc-service-1.0-SNAPSHOT.jar ai-toc-service-1.0-SNAPSHOT.jar#shell脚本
RUN bash -c 'touch /ai-toc-service-1.0-SNAPSHOT.jar'# 将容器的8222端口暴露,给外部访问。
EXPOSE 8222ENV JVM_PARAMS = "-XX:MetaspaceSize=512m","-XX:MaxMetaspaceSize=512m","-Xms2048m","-Xmx2048m","-Xmn256m","-Xss256k","-XX:SurvivorRatio=8","-XX:+UseConcMarkSweepGC"# 当容器运行起来时执行使用运行jar的指令ENTRYPOINT ["java","-Dserver.port=8222","-Dfile.encoding=utf-8","-jar","ai-toc-service-1.0-SNAPSHOT.jar","--spring.cloud.bootstrap.location=/datadrive/mnt/ai/ai-toc-service/config/bootstrap.yml","&"]CMD ["${JVM_PARAMS}"]
2.java-jenkins远程打包shell脚本
#!/bin/bash -ilex
cd /datadrive/software/docker-config/jenkins/jenkins_home/workspace/ai-statistics-service/ai-service/ai-statistics-service
sshpass -p 'pwd' scp -q -o StrictHostKeyChecking=no target/ai-statistics-service-1.0-SNAPSHOT.jar root@x.x.x.x:/datadrive/mnt/ai/ai-statistics-service/ai-statistics-service-1.0-SNAPSHOT.jar
echo "后端服务上传成功!"
sshpass -p 'pwd' ssh -tt root@x.x.x.x << EOF
echo '登录成功!准备执行命令'cd /datadrive/mnt/ai/ai-statistics-service
docker rm -f ai-statistics-service
sleep 1
docker rmi -f ai-statistics-service:1.0
sleep 1
docker build -t ai-statistics-service:1.0 -f /datadrive/mnt/ai/ai-statistics-service/Dockerfile .
sleep 1
docker run -d \
-p 8224:8224 \
-v /datadrive/mnt/ai/ai-statistics-service/config/bootstrap.yml:/datadrive/mnt/ai/ai-statistics-service/config/bootstrap.yml \
-v /datadrive/mnt:/datadrive/mnt \
--name ai-statistics-service ai-statistics-service:1.0
exit
3.java-jenkins-本地打包shell脚本
#!/bin/bash -il
cd /datadrive/software/docker-config/jenkins/jenkins_home/workspace/ai-statistics-service/ai-service/ai-statistics-service
docker rm -f ai-statistics-service
sleep 1
docker rmi -f ai-statistics-service:1.0
sleep 1
docker build -t ai-statistics-service:1.0 -f /datadrive/software/docker-config/jenkins/jenkins_home/workspace/ai-statistics-service/ai-service/ai-statistics-service/src/main/docker/Dockerfile .
sleep 1
docker run -d \
-p 8224:8224 \
-v /datadrive/mnt/ai/ai-statistics-service/config/bootstrap.yml:/datadrive/mnt/ai/ai-statistics-service/config/bootstrap.yml \
-v /datadrive/mnt:/datadrive/mnt \
--name ai-statistics-service ai-statistics-service:1.0
4.vue-jenkins-远程打包shell脚本
#!/bin/bash -ilex
npm -v
node -v
npm i
rm -rf ./dist/*
#打包命令
npm run build:stage
#如果文件夹不存在,则创建文件夹,这个文件夹在宿主机
cd /datadrive/software/docker-config/jenkins/jenkins_home/workspace/web-admin
sshpass -p 'pwd' scp -r -q -o StrictHostKeyChecking=no dist root@x.x.x.x:/datadrive/mnt/ai/web-admin/dist
echo "后端服务上传成功!"
sshpass -p 'pwd' ssh -tt root@x.x.x.x << EOF
echo '登录成功!准备执行命令'
echo "*************打包成功****************"
exit
5.vue-jenkins本地打包shell脚本
#!/bin/bash -ilex
npm -v
node -v
npm i
rm -rf ./dist/*
#打包命令
npm run build:stage
#如果文件夹不存在,则创建文件夹,这个文件夹在宿主机
cd /datadrive/software/docker-config/jenkins/jenkins_home/workspace/web-admin
sshpass -p 'pwd' scp -r -q -o StrictHostKeyChecking=no dist root@x.x.x.x:/datadrive/mnt/ai/web-admin/dist
echo "后端服务上传成功!"
sshpass -p 'pwd' ssh -tt root@x.x.x.x << EOF
echo '登录成功!准备执行命令'
echo "*************打包成功****************"
exit