docker安装和Harbor仓库安装参考此链接:
https://blog.csdn.net/weixin_43810415/article/details/131142744
jenkins构建镜像并上传harbor
ip | 部署内容 |
---|---|
192.168.88.20 | Docker(24.0.2)、Harbor(v2.8.2)、Docker Compose(v2.18.1)、Docker Portainer、Jenkins(2.410) |
docker pull portainer/portainer
docker run -p 9000:9000 -p 8000:8000 --name portainer \
--restart=always \
-v /var/run/docker.sock:/var/run/docker.sock \
-v /mydata/portainer/data:/data \
-d portainer/portainer
访问地址:http://192.168.88.20:9000/#/home
# 下载对应压缩包并上传到服务器/usr/local/java/目录下,解压
tar -zxvf jdk-8u291-linux-x64.tar.gz
tar -zxvf apache-maven-3.9.2-bin.tar.gz
# 编辑环境变量文件
vim /etc/profile
# 输入内容 :wq保存退出
JAVA_HOME=/usr/local/java/jdk1.8.0_291
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME CLASSPATH PATH
export MAVEN_HOME=/usr/local/java/apache-maven-3.9.2
export PATH=${MAVEN_HOME}/bin:${PATH}
# 使配置生效
source /etc/profile
# 检查是否安装成功
java -version
# 配置maven镜像加速
cd apache-maven-3.9.2/conf/
# 编辑配置文件
vim settings.xml
# mirrors标签内添加以下内容
<mirror>
<id>alimaven</id>
<name>aliyun maven</name>
<url>http://maven.aliyun.com/nexus/content/groups/public/</url>
<mirrorOf>central</mirrorOf>
</mirror>
cd /home
# 创建挂载目录
mkdir jenkins_home
# 赋权限
chmod -R 777 jenkins_home
# 尽量指定新一点且支持jdk1.8的版本下载,尽量不要使用lts,因为有些插件会要求达到某版本才能使用
docker pull jenkins/jenkins:2.410
# 执行命令
#-v /home/jenkins_home:/var/jenkins_home:是对容器内存储Jenkins的一些配置信息的文件夹
#-v /usr/local/java/jdk1.8.0_291:/usr/local/jdk1.8.0_291:是把linux下的jdk和容器内的关联(配置Jenkins时使用)
#-v /usr/local/java/apache-maven-3.9.2:/usr/local/apache-maven-3.9.2:是把linux下的maven和容器内的关联(配置Jenkins时使用)
#-v $(which docker):/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock:是可以在Jenkins容器里使用我们Linux下的docker。
docker run -di --name=myJenkins -p 9003:8080 -v /home/jenkins_home:/var/jenkins_home \
-v /usr/local/java/jdk1.8.0_291:/usr/local/jdk1.8.0_291 \
-v /usr/local/java/apache-maven-3.9.2:/usr/local/apache-maven-3.9.2 \
-v $(which docker):/usr/bin/docker \
-v /var/run/docker.sock:/var/run/docker.sock jenkins/jenkins:2.410
1、Publish Over SSH。作用是允许通过SSH的方式连接Linux,然后进行部署项目等
2、Deploy to container。作用是允许在项目构建完成之后,部署到容器中
3、Git
4、Gitee
5、Git Parameter。参数化构建需要
参考:https://help.gitee.com/devops/connect/Jenkins-Plugin
下面的流水线脚本也可以在项目根目录创建Jenkinsfile文件,定义一项选择”Pipeline script from SCM“
# 模块pom.xml添加插件
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>dockerfile-maven-plugin</artifactId>
<version>1.4.13</version>
<configuration>
<repository>${project.artifactId}</repository>
<buildArgs>
<JAR_FILE>target/${project.build.finalName}.jar</JAR_FILE>
</buildArgs>
</configuration>
</plugin>
</plugins>
</build>
# 工程根目录添加Dockerfile文件
#FROM java:8
FROM openjdk:8-jdk-alpine
ARG JAR_FILE
COPY ${JAR_FILE} app.jar
EXPOSE 9000
ENTRYPOINT ["java","-jar","/app.jar"]
//harbor地址
def harbor_url="192.168.88.20:9002"
//镜像库的名称
def harbor_project="infra"
//Harbor的凭证
def harbor_auth = "9ed86288-cf38-4533-86ca-792b934452f9"
// 镜像
def authImage = "${harbor_url}/${harbor_project}/infra-auth:${env.TAG}"
def businessImage = "${harbor_url}/${harbor_project}/infra-business:${env.TAG}"
def gatewayImage = "${harbor_url}/${harbor_project}/infra-gateway:${env.TAG}"
pipeline {
agent any
tools {
// jenkins全局工具配置配置的maven和jdk
maven 'apache-maven-3.9.2'
jdk 'jdk1.8.0_291'
}
stages {
stage('拉取代码') {
steps {
echo '相关环境变量'
sh "printenv"
echo '-------------------------------------'
git 'https://gitee.com/dalao708/infra.git'
echo '拉取成功'
}
}
stage('清理maven库') {
steps {
dir("${env.WORKSPACE}"){
sh "rm -rf /var/jenkins_home/.m2/repository/com/infra/server/*"
echo '清理maven库完成'
}
}
}
stage('构建基础工程') {
steps {
dir("${env.WORKSPACE}/infra-server"){
sh "mvn -Dmaven.test.skip=true clean install"
//编译,安装公共工程
sh "mvn -f infra-support -Dmaven.test.skip=true clean install"
echo '构建基础工程'
}
}
}
stage('编译&构建镜像') {
steps {
dir("${env.WORKSPACE}/infra-server"){
sh "mvn -f infra-auth -Dmaven.test.skip=true clean package dockerfile:build"
sh "mvn -f infra-business -Dmaven.test.skip=true clean package dockerfile:build"
sh "mvn -f infra-gateway -Dmaven.test.skip=true clean package dockerfile:build"
echo '编译&构建镜像完成'
sh "docker tag infra-auth ${authImage}"
sh "docker tag infra-business ${businessImage}"
sh "docker tag infra-gateway ${gatewayImage}"
echo '镜像打标签完成'
}
}
}
stage('上传Harbor') {
steps {
withCredentials([usernamePassword(credentialsId: "${harbor_auth}", passwordVariable: 'password', usernameVariable: 'username')]) {
//登录到Harbor
sh "docker login -u ${username} -p ${password} ${harbor_url}"
//镜像上传
sh "docker push ${authImage}"
sh "docker push ${businessImage}"
sh "docker push ${gatewayImage}"
sh "echo 镜像上传成功"
//删除本地镜像
sh "docker rmi -f ${authImage}"
sh "docker rmi -f ${businessImage}"
sh "docker rmi -f ${gatewayImage}"
sh "docker rmi -f infra-auth"
sh "docker rmi -f infra-business"
sh "docker rmi -f infra-gateway"
sh "echo 删除本地镜像成功"
}
}
}
}
}
解决办法:
# 修改/var/run/docker.sock文件的权限
chmod 777 /var/run/docker.sock
解决办法:
vim /etc/docker daemon.json
# 这是一个json格式,如何还有其他配置注意格式要求
{
"insecure-registries": ["192.168.88.20:9002"]
}
systemctl daemon-reload
systemctl restart docker
解决办法:
cd /usr/local/harbor/
docker compose down -v
docker compose up -d