Jenkins CI、CD入门操作

基于Jenkins拉取GitLab指定发行版本的SpringBoot代码进行构建发布到生产环境实现CD实现持续部署

image.png

准备测试项目

准备一个简单的Spring boot 项目


image.png

Jenkins新建任务

image.png
image.png

Jenkins关联Gitlab自动拉取最新代码

image.png

ssh key 配置

进入容器生成新的ssh key

[root@localhost mytest]# docker exec -it jenkins bash
jenkins@790140a70e6f:/$ cd /var/jenkins_home/
jenkins@790140a70e6f:~$ ssh-keygen -t rsa -C "[email protected]" 
Generating public/private rsa key pair.
Enter file in which to save the key (/var/jenkins_home/.ssh/id_rsa): 
Created directory '/var/jenkins_home/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /var/jenkins_home/.ssh/id_rsa
Your public key has been saved in /var/jenkins_home/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:LJt2FmYqJFN6fUrn64TtXPgOTigPKLVwLwFJjNK+5Wo [email protected]
The key's randomart image is:
+---[RSA 3072]----+
|oo               |
|+o.              |
|+.  .            |
| ..o.. .         |
|. B+o + S        |
| +.X.. / o       |
|. +.= O X .      |
| .E. * O =       |
| .    ..*.o      |
+----[SHA256]-----+ 
[root@localhost data]# cd .ssh/
[root@localhost .ssh]# ls
id_rsa  id_rsa.pub

进入容器现在执行下如下命令,否则直接使用ssh key 会报错:

[root@localhost mytest]# docker exec -it jenkins bash
jenkins@790140a70e6f:/$ git ls-remote -h -- [email protected]:xiaolyuh/test.git HEAD
The authenticity of host 'gitee.com (212.64.63.190)' can't be established.
ECDSA key fingerprint is SHA256:FQGC9Kn/eye1W8icdBgrQp+KkGYoFgbVr17bmjey0Wc.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added 'gitee.com,212.64.63.190' (ECDSA) to the list of known hosts.

将公钥配id_rsa.pub置到gitlab,私钥id_rsa配置到jenkins全局凭据.

image.png

image.png
image.png
image.png

执行构建&查看日志

image.png
image.png

通过日志我们可以发现jenkins将代码拉取到了/var/jenkins_home/workspace/mytest1目录,/var/jenkins_home/目录我们已经映射到了宿主机的/usr/local/docker/jenkins/data目录,到这个目录我们可以看到已经拉取下来后的文件。

image.png

配置Maven构建代码

将安装好的maven和jdk映射到jenkins容器的数据卷中

mv /usr/local/maven/ /usr/local/docker/jenkins/data/
mv jdk1.8.0_361/ /usr/local/docker/jenkins/data/jdk

映射好后,我们可以到jenkins容器中的home目录查看到maven和jdk。


image.png

Jenkins配置Maven&JDK

image.png
image.png

/var/jenkins_home/maven/apache-maven-3.8.8就是刚刚映射到容器的maven地址。

在任务上使用Maven构建项目

image.png
image.png

重新构建任务&日志

image.png
image.png

查看jar包

[root@localhost data]# docker exec -it jenkins bash
jenkins@790140a70e6f:/$ cd /var/jenkins_home/workspace/mytest1/spring-boot-student-devops/target
jenkins@790140a70e6f:~/workspace/mytest1/spring-boot-student-devops/target$ ls
classes            generated-test-sources  maven-status                                   spring-boot-student-devops-0.0.1-SNAPSHOT.jar.original
generated-sources  maven-archiver          spring-boot-student-devops-0.0.1-SNAPSHOT.jar  test-classes
jenkins@790140a70e6f:~/workspace/mytest1/spring-boot-student-devops/target$ 

将jar包推送到远程服务器&通过Docker打包部署

配置全局server服务

image.png
image.png

新增Docker打包和启动所需的配置

image.png

在项目下新增docker文件夹,然后新增Dockerfile和docker-compose.yml文件
增加Dockerfile文件

# 设置依赖镜像
FROM daocloud.io/library/java:8u40-jdk
# 拷贝jar到容器工作目录
WORKDIR /usr/local/docker
COPY ./app.jar /usr/local/docker
# 启动jar文件
CMD java -jar app.jar

增加docker-compose.yml文件

version: "3.1"
services:
  mytest:
    # 通过当前目录下的Dockerfile来构建镜像
    build:
      context: ./
      dockerfile: Dockerfile
    image: mytest:v1.0.0
    container_name: mytest
    ports:
      # 将容器80端口映射到宿主机80端口映上
      - 8081:80

配置单个任务的Publish

image.png
image.png

命令解释

# 切换到上传后的docker
cd /root/docker
# 删除原有jar包
rm -rf app.jar
# 将新的jar包拷贝到docker目录
mv ../spring-boot-student-devops/target/*.jar app.jar
# 删除无效目录
rm -rf ../spring-boot-student-devops
# 通过docker-compose进行打包和启动容器
docker-compose down
docker-compose up -d --build
# 删除所有未打标签的镜像
docker rmi $(docker images -q -f dangling=true)

执行任务构建&日志

image.png

这里传输了3个文件

目前服务器查看部署情况

[root@localhost docker]# docker ps
CONTAINER ID   IMAGE           COMMAND                   CREATED             STATUS             PORTS                                   NAMES
24d77685afcd   mytest:v1.0.0   "/bin/sh -c 'java -j…"   About an hour ago   Up About an hour   0.0.0.0:8081->80/tcp, :::8081->80/tcp   mytest
[root@localhost docker]# 
image.png

参数化构建项目

配置参数类型

image.png
image.png

设置构建前的操作

image.png
image.png
# 配置参数
REMOTE_NAME="origin"    # 远程仓库名
BRANCH_NAME=$branch     # 拉取的分支名

BRANCH_NAME=${BRANCH_NAME#*/}

# 检查输入参数
if [ -z "$BRANCH_NAME" ]; then
  echo "Please provide the branch name as the first argument."
  exit 1
fi

# 判断本地分支是否存在
if git show-ref --verify --quiet "refs/heads/$BRANCH_NAME"; then
  echo "Local branch $BRANCH_NAME exists."
  git checkout $BRANCH_NAME
  git config pull.rebase false
  git pull $REMOTE_NAME $BRANCH_NAME
else
  echo "Local branch $BRANCH_NAME does not exist."
  git fetch $REMOTE_NAME $BRANCH_NAME
  git checkout -b $BRANCH_NAME $REMOTE_NAME/$BRANCH_NAME
fi

# 执行完毕
echo "The latest code has been successfully pulled from the remote branch."
exit 0

准备多分支

image.png

重新构建任务

image.png

查看结果

image.png

你可能感兴趣的:(Jenkins CI、CD入门操作)