软件开发最初是由两个团队共同组成:(没有采用DevOps之前)
没有采用DevOps的缺点:
采用DevOps的优点?
整体的软件开发流程:
为了保证整体流程可以高效的完成,各个阶段都有比较常见的工具,如下图:
持续集成、持续部署的工具很多,其中Jenkins是一个开源的持续集成平台。
Jenkins涉及到将编写完毕的代码发布到测试环境和生产环境的任务,并且还涉及到了构建项目等任务。
Jenkins需要大量的插件保证工作,安装成本较高,下面会基于Docker搭建Jenkins。
Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具
Jenkins应用广泛,大多数互联网公司都采用Jenkins配合GitLab、Docker、K8s作为实现DevOps的核心工具。
Jenkins最强大的就在于插件,Jenkins官方提供了大量的插件库,来自动化CI/CD过程中的各种琐碎功能。
Jenkins最主要的工作就是将GitLab上可以构建的工程代码拉取并且进行构建,再根据流程可以选择发布到测试环境或是生产环境。经过大量的测试后,确定发行版本,再发布到生产环境
CI过程即是通过Jenkins将代码拉取、构建、制作镜像交给测试人员测试。
CD过程即是通过Jenkins将打好标签的发行版本代码拉取、构建、制作镜像交给运维人员部署。
mkdir -p /root/jenkins && cd /root/jenkins
docker pull jenkins/jenkins:2.359
vi docker-compose.yml
内容如下:
version: "3.1"
services:
jenkins:
image: jenkins/jenkins:2.359
container_name: jenkins
ports:
- 9200:8080
- 50000:50000
volumes:
- /usr/local/docker/jenkins_docker/data/:/var/jenkins_home/
mkdir -p /usr/local/docker/jenkins_docker/data/
chmod -R a+w /usr/local/docker/jenkins_docker/data/
docker-compose up -d
cd /usr/local/docker/jenkins_docker/data/
rm -f hudson.model.UpdateCenter.xml
vi hudson.model.UpdateCenter.xml
内容如下:(复制进去即可)
<sites>
<site>
<id>defaultid>
<url>http://mirror.esuni.jp/jenkins/updates/update-center.jsonurl>
site>
sites>
cd /root/jenkins
docker-compose restart
[root@centos7-jenkins jenkins]# docker exec -it jenkins cat /var/jenkins_home/secrets/initialAdminPassword
af057174db8b41808bb3ca224eb67f10
解决方法二里面的找最新版本的方法图解如下:⭐
cd /usr/local/docker/jenkins_docker/data/
mv /usr/local/jdk/ /usr/local/docker/jenkins_docker/data
mv /usr/local/maven/ /usr/local/docker/jenkins_docker/data
mkdir -p /usr/local/test
package com.boot.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class JenkinsController {
@ResponseBody
@GetMapping(path = "/test")
public String test(){
return "hello Jenkins";
}
}
spring.application.name=jenkins-demo
server.port=8082
[root@centos7-jenkins jenkins] docker exec -it jenkins bash
[root@centos7-jenkins Jenkins容器] cd var/jenkins_home/workspace/mytest/
[root@centos7-jenkins Jenkins容器] ls
README.md mvnw mvnw.cmd pom.xml src
<build>
<finalName>mytestfinalName>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
<executions>
<execution>
<phase>packagephase>
<goals>
<goal>repackagegoal>
goals>
execution>
executions>
<version>2.5.9version>
<configuration>
<includeSystemScope>trueincludeSystemScope>
<mainClass>com.boot.JenkinsDemoApplicationmainClass>
configuration>
plugin>
plugins>
build>
clean package -DskipTests
[root@centos7-jenkins jenkins]# docker exec -it jenkins bash
[root@centos7-jenkins 容器]# cd var/jenkins_home/workspace/mytest/target/
[root@centos7-jenkins 容器]# ls
classes generated-test-sources maven-archiver test-classes
generated-sources mytest.jar maven-status
提示:
target/*.jar
开始配置远程推送jar包给目标服务器:
[root@centos7-jenkins jenkins]# cd /usr/local/test/target/
[root@centos7-jenkins target]# ls
mytest.jar
1:在项目中编写Dockerfile和docker-compose.yml文件,然后上传到gitlab上去:
FROM java:8
COPY mytest.jar /usr/local/
WORKDIR /usr/local
CMD java -jar mytest.jar
version: "3.1"
services:
mytest:
build:
context: ./
dockerfile: Dockerfile
image: mytest:v1.0.0
container_name: mytest
ports:
- 8082:8082 #因为我们application.yml配置这个项目是8082端口
[root@centos7-jenkins test]# rm -rf /usr/local/test/docker/ /usr/local/test/target/
3:在Jenkins进行配置:
target/*.jar docker/*
cd /usr/local/test/docker
mv ../target/*jar ./
docker-compose down
docker-compose up -d --build
docker image prune -f
4:Jenkins构建完成之后查看镜像和容器:(可以看到mytest:v1.0.0 已经启动。)
[root@centos7-jenkins local]# docker images
REPOSITORY TAG IMAGE ID CREATED SIZE
mytest v1.0.0 db233798a206 2 minutes ago 661MB
jenkins/jenkins 2.359 616d2581d755 7 days ago 463MB
[root@centos7-jenkins local]# docker ps
CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
04146936e4ec mytest:v1.0.0 "/bin/sh -c 'java -j…" 3 minutes ago Up 3 minutes 0.0.0.0:8082->8082/tcp, :::8082->8082/tcp mytest
5b69eb0eb387 jenkins/jenkins:2.359 "/usr/bin/tini -- /u…" 24 hours ago Up About an hour 0.0.0.0:50000->50000/tcp, :::50000->50000/tcp, 0.0.0.0:9200->8080/tcp, :::9200->8080/tcp jenkins
案例过程:在Jenkins实战1的基础上,通过修改部分内容让CI变成CD。