git init
git add .
git commit -m "first commit"
git remote add origin [email protected]:********/cicddemo.git
git push -u origin master
jenkins_service 集成服务器 端口:192.168.182.140
集成服务器需要安装 git ,maven,jdk,docker,docker-compose,jenkins
harbor_service 镜像服务器:192.168.182.139
镜像服务器需安装docker,docker-compose, harbor
com.spotify
docker-maven-plugin
1.0.0
${project.artifactId}
${project.basedir}/src/main/resources
${project.version}
192.168.25.139
true
192.168.182.139/library/${project.artifactId}:${project.version}
/
${project.build.directory}
${project.artifactId}-${project.version}.jar
复制pom.xml改个名字再加上dockerc插件
配置Dockerfile文件
FROM openjdk:8-jre
ENV HOME /home
ENV RE dev
run mkdir HOME
COPY /cicdtest-0.0.1-SNAPSHOT.jar HOME/cicdtest-0.0.1-SNAPSHOT.jar
WORKDIR HOME
ENTRYPOINT java -jar cicdtest-0.0.1-SNAPSHOT.jar --spring.profiles.active=$RE
git add.
git commit -m"commit docker plugins"
git push origin master
关闭防火墙和启动docker
systemctl stop firewalld
systemctl start docker
确认镜像服务器 harbor 启动
[root@base harbor]# cat /etc/docker/harbor_pw | docker login 192.168.182.139 -u admin --password-stdin
# 代表登录成功
Login Succeeded
清空集成服务器上没有用的 镜像 和 容器
[root@base ~]# docker rmi -f $(docker images -aq)
[root@base ~]# docker rm -f $(docker ps -aq)
集成服务器登录到镜像私服也就是 Harbor
# 注意daemon.json 要配置正确
[root@base ~]# vim /etc/docker/daemon.json
# 配置过后记得重启docker
[root@base ~]# systemctl restart docker
[root@base ~]# cat /etc/docker/harbor_pw | docker login 192.168.182.139 -u admin --password-stdin
# 代表登录成功
Login Succeeded
集成服务器拉去代码
[root@base project]# git clone [email protected]:******/cicddemo.git
打包 打镜像 上传镜像
cd cicddemo/
mvn clean package -f docker_pom.xml -Dmaven.test.skip=true docker:build
#-f docker_pom.xml指定打包时使用docker_pom.xml文件
#docker build 命令用于使用 Dockerfile 创建镜像
[root@base project]# cd cicddemo/
[root@base cicddemo]# ls
docker_pom.xml pom.xml src target
[root@base cicddemo]# mvn clean package -f docker_pom.xml -Dmaven.test.skip=true docker:build
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.jsxl:cicddemo >--------------------------
[INFO] Building cicddemo 0.0.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-clean-plugin:3.1.0:clean (default-clean) @ cicddemo ---
[INFO] Deleting /usr/project/cicddemo/target
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:resources (default-resources) @ cicddemo ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Using 'UTF-8' encoding to copy filtered properties files.
[INFO] Copying 4 resources
[INFO] Copying 1 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:compile (default-compile) @ cicddemo ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to /usr/project/cicddemo/target/classes
[INFO]
[INFO] --- maven-resources-plugin:3.2.0:testResources (default-testResources) @ cicddemo ---
[INFO] Not copying test resources
[INFO]
[INFO] --- maven-compiler-plugin:3.8.1:testCompile (default-testCompile) @ cicddemo ---
[INFO] Not compiling test sources
[INFO]
[INFO] --- maven-surefire-plugin:2.22.2:test (default-test) @ cicddemo ---
[INFO] Tests are skipped.
[INFO]
[INFO] --- maven-jar-plugin:3.2.0:jar (default-jar) @ cicddemo ---
[INFO] Building jar: /usr/project/cicddemo/target/cicddemo-0.0.1-SNAPSHOT.jar
[INFO]
[INFO] --- spring-boot-maven-plugin:2.6.2:repackage (repackage) @ cicddemo ---
[INFO] Replacing main artifact with repackaged archive
[INFO]
[INFO] --- docker-maven-plugin:1.0.0:build (default-cli) @ cicddemo ---
[INFO] Using authentication suppliers: [ConfigFileRegistryAuthSupplier]
[INFO] Copying /usr/project/cicddemo/target/cicddemo-0.0.1-SNAPSHOT.jar -> /usr/project/cicddemo/target/docker/cicddemo-0.0.1-SNAPSHOT.jar
[INFO] Copying /usr/project/cicddemo/src/main/resources/Dockerfile -> /usr/project/cicddemo/target/docker/Dockerfile
[INFO] Copying /usr/project/cicddemo/src/main/resources/application-dev.yml -> /usr/project/cicddemo/target/docker/application-dev.yml
[INFO] Copying /usr/project/cicddemo/src/main/resources/application-prod.yml -> /usr/project/cicddemo/target/docker/application-prod.yml
[INFO] Copying /usr/project/cicddemo/src/main/resources/application-test.yml -> /usr/project/cicddemo/target/docker/application-test.yml
[INFO] Copying /usr/project/cicddemo/src/main/resources/application.yml -> /usr/project/cicddemo/target/docker/application.yml
[INFO] Building image 192.168.182.139/library/cicddemo:0.0.1-SNAPSHOT
Step 1/7 : FROM openjdk:8-jre
---> 26ac3f63d29f
Step 2/7 : ENV HOME /home
---> Using cache
---> 8c279eb0393e
Step 3/7 : ENV RE dev
---> Using cache
---> d9963707fcfb
Step 4/7 : run mkdir HOME
---> Using cache
---> 9182057388b3
Step 5/7 : COPY /cicddemo-0.0.1-SNAPSHOT.jar HOME/cicddemo-0.0.1-SNAPSHOT.jar
---> fd5ed0d95b6c
Step 6/7 : WORKDIR HOME
---> Running in 5147cd965958
Removing intermediate container 5147cd965958
---> d68465aba479
Step 7/7 : ENTRYPOINT java -jar cicddemo-0.0.1-SNAPSHOT.jar --spring.profiles.active=$RE
---> Running in f9d025a07cb8
Removing intermediate container f9d025a07cb8
---> 6aa462c9a64e
ProgressMessage{id=null, status=null, stream=null, error=null, progress=null, progressDetail=null}
Successfully built 6aa462c9a64e
Successfully tagged 192.168.182.139/library/cicddemo:0.0.1-SNAPSHOT
[INFO] Built 192.168.182.139/library/cicddemo:0.0.1-SNAPSHOT
[INFO] Tagging 192.168.182.139/library/cicddemo:0.0.1-SNAPSHOT with 0.0.1-SNAPSHOT
[INFO] Pushing 192.168.182.139/library/cicddemo:0.0.1-SNAPSHOT
The push refers to repository [192.168.182.139/library/cicddemo]
d97d4bcde25a: Pushed
8a10288c1479: Pushed
b4cfcb8385a8: Pushed
2b730cf18c09: Pushed
edeaba958753: Pushed
8bf42db0de72: Pushed
31892cc314cb: Pushed
11936051f93b: Pushed
0.0.1-SNAPSHOT: digest: sha256:bdfff3d6cf5185724bf9aac2230160f57f7e7f48e5e0b252a5e3dead87076f35 size: 2000
null: null
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 15.055 s
[INFO] Finished at: 2021-12-25T18:58:39+08:00
[INFO] ------------------------------------------------------------------------
安装(高版本需要jdk11择优安装)
linux安装官网链接:https://www.jenkins.io/download/.
百度网盘链接:https://pan.baidu.com/s/12sO24LMdHeOYQqkYiZHMUQ.
提取码:xjaz
启动前
[root@base local]# ll /root -a
总用量 48
dr-xr-x---. 8 root root 4096 12月 25 19:41 .
dr-xr-xr-x. 18 root root 4096 12月 25 10:18 ..
-rw-------. 1 root root 968 12月 15 10:45 anaconda-ks.cfg
-rw-------. 1 root root 226 12月 23 17:57 .bash_history
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
drwxr-xr-x. 3 root root 17 12月 15 10:45 .cache
drwxr-xr-x. 3 root root 17 12月 15 10:45 .config
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
drwx------. 2 root root 24 12月 25 18:22 .docker
drwxr-----. 3 root root 18 12月 25 08:27 .pki
drwx------. 2 root root 24 12月 22 16:12 .ssh
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
-rw-------. 1 root root 5642 12月 25 18:30 .viminfo
启动
[root@base local]# java -jar jenkins.war --httpPort=9999
# 会生成一个初始化
9fd89521***********871e6e358
# 密码在这个文件下
This may also be found at: /root/.jenkins/secrets/initialAdminPassword
另开一台服务器窗口启动后(会在/root下生成.jenkins)
[root@base ~]# ll /root -a
总用量 48
drwxr-xr-x. 11 root root 4096 12月 25 19:42 .jenkins
访问jenkins(举例:http://192.168.182.140:9999)
建议通过压缩包导入插件,镜像在国外下载较慢,也可配置国内镜像
改密码
更换清华镜像源
https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json
[root@base .jenkins]# cd .jenkins/
[root@base .jenkins]# cp /usr/local/plugins.gz ./
[root@base .jenkins]# tar -zxvf plugins.gz
[root@base .jenkins]# ll plugins
装插件后需要重启jenkins
[root@base local]# java -jar jenkins.war --httpPort=9999
配置jenkins中的git maven和jdk
以git为例确定服务器上是否有安装
# 确认版本即可确认是否安装
[root@base local]# git --version
git version 1.8.3.1
# git的安装默认路径在这里
[root@base local]# cd /usr/libexec/git-core
# 如果没安装只需要执行如下指令
[root@base git-core]# yum -y install git
找到对应的mvn和jdk安装位置(配置后保存)
配置自动集成任务
配置代码仓库
配置 代码下载成功之后的 操作
额外补充:jenkins配置环境变量(这里可以不配因为服务中已经配置好了)
测试任务
镜像服务器查看镜像,已经更新了
测试服务器 test_environment_service 192.168.182.142
测试服务器需要安装git ,maven,jdk,docker,docker-compose,jenkins
关闭防火墙登陆docker
[root@base ~]# systemctl stop firewalld
[root@base ~]# systemctl start docker
# 注意daemon.json 要配置正确
[root@base ~]# vim /etc/docker/daemon.json
# 查看
[root@base ~]# cat /etc/docker/daemon.json
{
"registry-mirrors": ["https://registry.docker-cn.com"],
"insecure-registries":["192.168.182.139"]
}
# 配置过后记得重启docker
[root@base ~]# systemctl restart docker
[root@base ~]# cat /etc/docker/harbor_pw
Harbor12345
[root@base ~]# cat /etc/docker/harbor_pw | docker login 192.168.182.139 -u admin --password-stdin
# 代表登录成功
Login Succeeded
已知集成服务器(jenkins)中已有一对公钥私钥
[root@base cicddemo]# cd /root/.ssh
[root@base .ssh]# ls
id_rsa id_rsa.pub known_hosts
[root@base .ssh]# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC4vV3Y+qPHvFfAW0cjyeae73xdGAzoT0BvB9bEtXyKy1cRFtGQ/Hm5WimxQCR2dWPyuZJOd+k3jKxgvbGul5BwZs/LOgO1Y************************************************************************************************NtxahjhaApGbD16L/qRfD/06rNw/Z7yubpXQtN02X8WIIfckMRNcDUGVaXoOZy4sVP9pAIWkFRpaFgedYkneHKGLBpNM7L6qDc2Vv9ITrC/91n8iSzPPIrFaUss2GSk4d2tmv+P5LyIN4l/+Nn0t2qzPZkMLwXjGzv9eNBE7MXFqn5aQaafX0z [email protected]
测试服务器配置公钥(发送公钥报错)
[root@base .ssh]# ssh-copy-id -i /root/.ssh/id_rsa.pub [email protected]
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
/root/.ssh/id_rsa.pub [email protected]'s password:
Permission denied, please try again.
ssh连接root报错权限被拒绝,请重试。解决
修改连接服务器的sshd_config(如下图)
[root@base ~]# vim /etc/ssh/sshd_config
如果需要修改相关策略配置,在继续之前建议进行文件备份。
使用 vi 等编辑器,将参数值设置为 yes,或者整个删除或注释(在最开头添加 # 号)整行配置。比如:PermitRootLogin yes
使用如下指令重启 SSH 服务再重新发送公钥
[root@base ~]# systemctl restart sshd
ssh配置失败参考第二种方法
链接:https://blog.csdn.net/weixin_56219549/article/details/122164804.
最后查看测试服务器是否有镜像和容器再测试