当前位置:网站首页>【云原生 | Devops篇】Jenkins安装与实战(二)
【云原生 | Devops篇】Jenkins安装与实战(二)
2022-06-21 11:31:00 【Lanson】
Jenkins安装与实战
一、Jenkins安装
1、背景
Jenkins,之前叫做Hudson,由SUN公司启动,2010年oracle收购SUN导致hudson商标归oracle保留,hudson的主要贡献者基于hudson更名为jenkins并持续更新。很长一段时间内lenkins和Hudson继续作为两个独立的项目,每个都认为对方是自己的分支。目前Hudson已经停止开发,最新的版本发布于2016年,而Jenkins的社区和开发却异常活跃。Jenkins是目前市场上使用最多的CICD工具。
Jenkins是基于Java开发的一种持续集成工具。 Jenkins作为持续集成工具,持续集成是其核心功能,在核心功能基础之上可扩展实现强大的CD功能。
2、特点
开源免费 易于安装(基本上算是所有CI工具里安装配置最简单的) 多平台支持(windows/linux/macos) 主从分布式架构 提供web可视化配置管理页面 安装配置简单 插件资源丰富
3、官网安装文档地址
https://www.jenkins.io/zh/doc/book/installing/
大家最好看着官网文档进行安装,好的开源软件一般官网文档都是写得很好,不然很难火起来。
这边主要演示用docker方式来安装Jenkins,没有安装docker或者不知道docker怎么安装的同学可以先看一下我这篇文章《https://lansonli.blog.csdn.net/article/details/124202180【云原生 | Docker篇】轻松学会原理|架构|安装|加速(一)》,安装完docker再接着往下看吧

4、开始安装
注意:
jenkinsci/jenkins 是没有 blueocean插件的,得自己装 jenkinsci/blueocean:带了的
#自己构建镜像 RUN的时候就把时区设置好 #如果是别人的镜像,docker hub,UTC; 容器运行时 , -v /etc/localtime:/etc/localtime:ro
docker run \
-u root \
-d \
-p 8080:8080 \
-p 50000:50000 \
-v jenkins-data:/var/jenkins_home \
-v /etc/localtime:/etc/localtime:ro \
-v /var/run/docker.sock:/var/run/docker.sock \
--restart=always \
jenkinsci/blueocean
#/var/run/docker.sock 表示Docker守护程序通过其监听的基于Unix的套接字。 该映射允许jenkinsci/blueocean 容器与Docker守护进程通信, 如果 jenkinsci/blueocean 容器需要实例化其他Docker容器,则该守护进程是必需的。 如果运行声明式管道,其语法包含agent部分用 docker;例如, agent { docker { ... } } 此选项是必需的。
#如果你的jenkins 安装插件装不上。使用这个镜像【 registry.cn-qingdao.aliyuncs.com/lfy/jenkins:plugins-blueocean 】默认访问账号/密码是【admin/admin】
5、安装中

6、安装完成

7、页面访问

8、通过以下命令查找密码
方式一:



方式二:


9、安装默认推荐插件,并且配置用户
选择安装推荐的插件选项即可

出现这个等待自动安装插件完成就好

配置用户,点击右下角保存即可



安装完成

二、Jenkins实战
1、准备一个git项目进行测试
我们以gitee为例,github可能太慢了。需要idea安装gitee插件。或者自己熟悉手动命令也行。
简要说明一下,其实这里用gitee、github或者用gitcode都能实现的,具体用哪个,同学们可以自行选择
步骤:
idea创建Spring Boot项目

VCS - 创建git 仓库

gitee创建一个仓库,本地上传到仓库上

idea提交内容到gitee

开发项目基本功能,并在项目中创建一个jenkinsfile文件

创建一个名为 devops-java-demo的流水线项目,使用项目自己的流水线



Jenkins的工作流程
先定义一个流水线项目,指定项目的git位置
流水线启动
1、先去git位置自动拉取代码
2、解析拉取代码里面的Jenkinsfile文件
3、按照Jenkinsfile指定的流水线开始加工项目
不太会写流水线配置文件可以参考官方文档,非常详细的
官网流水线的说明文档:流水线
Jenkins重要的点 1、jenkins的家目录 /var/jenkins_home 已经被我们docker外部挂载了 /var/lib/docker/volumes/jenkins-data/_data
2、WORKSPACE(工作空间)=/var/jenkins_home/workspace/java-devops-demo
每一个流水线项目,占用一个文件夹位置 BUILD_NUMBER=5;当前第几次构建 WORKSPACE_TMP(临时目录)=/var/jenkins_home/workspace/[email protected]
JOB_URL=http://192.168.88.163:8080/job/java-devops-demo/
3、常用的环境如果没有,jenkins配置环境一大堆操作
4、jenkins_url : http://192.168.88.163:8080/
小案例测试:
// 写流水线的脚本(声明式、脚本式)
pipeline{
// 全部的CICD流程都需要在这里定义
// 任何一个代理可用就可以执行
agent any
// 定义一些环境信息
// 定义流水线的加工流程
stages {
// 流水线的所有阶段
// 1、编译
stage('编译'){
steps{
// 要做的所有事情
echo "编译..."
}
}
// 2、测试
stage('测试'){
steps{
// 要做的所有事情
echo "测试..."
}
}
// 3、打包
stage('打包'){
steps{
// 要做的所有事情
echo "打包..."
}
}
// 4、部署
stage('部署'){
steps{
// 要做的所有事情
echo "部署..."
}
}
}

2、远程构建触发
期望效果: 远程的github代码提交了,jenkins流水线自动触发构建。

实现流程:
1、保证jenkins所在主机能被远程访问
可以在云平台配置一个公网IP,让gitee能远程访问
2、jenkins中远程触发需要权限,我们应该使用用户进行授权

3、配置gitee/github,webhook进行触发

测试成功

前面出现两次错误是因为我的地址没有加上用的API Token,下图是生成Token的设置


#远程构建即使配置了github 的webhook,默认会403.我们应该使用用户进行授权 1、创建一个用户 2、一定随便登陆激活一次 3、生成一个apitoken http://cheshi:[email protected]:8080/job/simple-java-maven-app/build?token=lansonli

远程触发:
JENKINS_URL /job/simple-java-maven-app/build?token= TOKEN_NAME 请求即可
以后直接提交push代码就会触发自动构建,不用手动执行触发了

测试官方推荐的gitcode是否也支持这个功能
我用阿里云的服务器,又重新搭了一遍Jenkins,以下就是我测试的结果,其实都能用的
1、复制上面的测试代码提交到gitcode,如果不知道如何使用gitcode可以看一下官方教程

2、配置webhook进行触发
有人说那些apitoken生成过程呢,我这边就直接跳过,因为上面都写的很详细,就不占用篇幅再写一遍了

配置原理还是一样的,这里只要写一下URL,下面勾选一下触发事件,点击提交就可以生效了
3、见证测试结果

经过多次失败后一度怀疑 gitcode是否能用,结果是我多虑了,肯定是能用的,失败是因为我复制的URL有问题导致,所以大家测试一定要注意细节。


边栏推荐
- Flink tuning (I) resource tuning and back pressure analysis
- Is the Huatai Securities account given by qiniu school true? Is it safe to open an account
- 2022 special operation certificate examination question bank and online simulation examination for safety management personnel of hazardous chemical business units
- 數據可視化入門
- Machine learning 2-linear regression
- Kotlin - Sequence 序列
- harmonyOS第二次培训
- There are obvious signs of oversupply of chips, ASML is no longer a pastry, and investment institutions are shorting on a large scale
- One's deceased father grind politics English average cent furnace! What is your current level?
- You must get started with boost
猜你喜欢

One article quick learning - playing with MySQL time operation function and detailed explanation of time matching operation + instance code

机器学习2-线性回归

harmonyos培训一

fix libpng warning: iCCP: Not recognizing known sRGB profile ......

Deep water area involvement

基于QtQuick的QCustomPlot实现

无界版图荣获2022年度数字文化产业创新品牌称号

New experience of cultural tourism! 3dcat helps Guangzhou intangible cultural heritage "yuancosmos" block make a cool appearance

Clear the switch configuration, configure the image port and Wireshark packet capturing (take Huawei s5720 as an example)
![[yolov5s target detection] opencv loads onnx model for reasoning on GPU](/img/87/49a54dfaf325fe104287235fe533c5.png)
[yolov5s target detection] opencv loads onnx model for reasoning on GPU
随机推荐
C# Cannot access child value on Newtonsoft. Json. Linq. JProperty
C语言初阶(八)联合体
Est le logiciel d'oscilloscope allemand, le logiciel d'ordinateur hôte d'oscilloscope keysight NS scope
基于QtQuick的QCustomPlot实现
OpenGL学习笔记之坐标变换学习
The first question of leetcode -- sum of two numbers
使用赞美提高绩效
贺志理:红树林湿地沉积物中微生物驱动的碳氮硫磷循环及其耦合机制
C语言初阶(十)类型重命名typedef
Nature sub Journal | Zhou concentrated the team to reveal that long-term climate warming leads to the decrease of soil microbial diversity in grassland
Introduction to common source oscilloscope software and RIGOL oscilloscope upper computer software ns-scope
What if the server is invaded
SSD【目标检测篇】
完美安全代码审计的5个最佳实践
fix libpng warning: iCCP: Not recognizing known sRGB profile ......
五步成功完成威胁建模
QT operation on SQLite database multithreading
qt对sqlite数据库多线程的操作
Operation and maintenance security, not so simple
Démarrer avec la visualisation des données