Jenkins学习笔记
1、基本概念
Jenkins是一个开源软件项目,是基于Java开发的一种持续集成工具,用于监控持续重复的工作,旨在提供一个开放易用的软件平台,使软件项目可以进行持续集成。
2、安装
文件包:https://pan.baidu.com/s/1uhSpic2bZVBDE-jynycdpg?pwd=lggi
本来打算使用jenkinsci/blueocean,后来在使用过程中发现这个镜像坑比较多,还是决定自己用Dockerfile做一个
# Build: docker build --no-cache --force-rm -t jenkinsci ./
# Run: docker run -d -u root -p 80:8080 --name jenkinsci -v jenkins_home:/root/.jenkins -v /var/run/docker.sock:/var/run/docker.sock --restart=always jenkinsci
# Into: docker exec -it jenkinsci /bin/bash
FROM centos:centos7
COPY ./ /dest
WORKDIR /dest
RUN rpm -ivh jdk-11.0.16_linux-x64_bin.rpm && \
rpm -ivh jenkins-2.362-1.1.noarch.rpm && \
\cp jenkins /etc/sysconfig/jenkins && \
chmod 777 -R /var/lib/jenkins && \
yum -y install fontconfig && \
yum -y install git && \
yum-config-manager --add-repo http://mirrors.aliyun.com/docker-ce/linux/centos/docker-ce.repo && \
yum -y install docker-ce-cli-20.10.5-3.el7
VOLUME ["/root/.jenkins"]
EXPOSE 8080
WORKDIR /
ENTRYPOINT ["jenkins"]
构建镜像
docker build --no-cache -t jenkinsci .
启动镜像
docker run -d -u root -p 80:8080 --name jenkinsci -v jenkins_home:/root/.jenkins -v /var/run/docker.sock:/var/run/docker.sock --restart=always jenkinsci
进入容器
docker exec -it jenkinsci /bin/bash
3、配置
http://150.158.24.200/
获取解锁密码
[root@cicd ~]# docker logs jenkinsci
*************************************************************
*************************************************************
*************************************************************
Jenkins initial setup is required. An admin user has been created and a password generated.
Please use the following password to proceed to installation:
7aa5995e44fb4c47b20a1da291ed3d00
This may also be found at: /root/.jenkins/secrets/initialAdminPassword
*************************************************************
*************************************************************
*************************************************************
自定义插件
创建用户
实例配置
配置完成
4、插件
Jenkins->Manage Jenkins->Manage Plugins->Available
Credentials Binding Plugin
Gitee
Pipeline
Email Extension Template
5、凭证
Jenkins->Manage Jenkins->Manage Credentials->global
5.1、username-password
5.2、ssh-key
6、Git hook
jenkins配置gitee webhook
gitee配置推送hook
7、全局工具配置
Jenkins->Manage Jenkins->Global Tool Configuration
7.1、JDK
JDK8:https://pan.baidu.com/s/1JcpZtQYNDrFLUl00CdhNNw?pwd=bweh
7.2、Maven
Maven3.6:https://pan.baidu.com/s/1rEQOUN2BdAP0cpoifRnLqQ?pwd=k9g3
7.3、全局工具配置测试
pipeline {
agent any
tools {
jdk "JDK8u221"
maven "Maven3.6.3"
}
stages {
stage('Environment Check') {
steps {
sh 'java -version'
sh 'mvn -v'
}
}
}
}
8、Pipeline
8.0、生成器
8.1、存放位置
jenkins中维护
项目中维护
pipeline {
agent any
stages {
stage('Pull') {
steps {
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '6a6543d1-3262-4f96-a4b5-4259a9a5c038', url: 'https://gitee.com/lianxing2233/springcloud-demo.git']])
}
}
stage('Build') {
steps {
sh 'docker build --no-cache --force-rm -t springcloud-demo:latest ./'
sh 'docker images'
}
}
}
post {
always {
emailext(
to: "2750955630@qq.com",
subject: 'CICD:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
body: '${FILE,path="email.html"}'
)
}
}
}
8.2、下载代码
pipeline {
agent any
stages {
stage('Pull') {
steps {
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '6a6543d1-3262-4f96-a4b5-4259a9a5c038', url: 'https://gitee.com/lianxing2233/vue-demo.git']])
}
}
}
}
8.3、构建镜像
pipeline {
agent any
stages {
stage('Pull') {
steps {
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '6a6543d1-3262-4f96-a4b5-4259a9a5c038', url: 'https://gitee.com/lianxing2233/vue-demo.git']])
}
}
stage('Build') {
steps {
sh 'docker build --no-cache -t vue-demo ./'
sh 'docker images'
}
}
}
}
9、推送镜像
阿里云->容器镜像服务->个人实例
9.1、创建命名空间
9.2、创建镜像仓库
9.3、登录到阿里云镜像仓库
docker login -u USERNAME -p PASSWORD registry.cn-hangzhou.aliyuncs.com
9.4、推送镜像
docker tag [ImageId] registry.cn-hangzhou.aliyuncs.com/ialso/jenkinsci:[镜像版本号]
docker push registry.cn-hangzhou.aliyuncs.com/ialso/jenkinsci:[镜像版本号]
9.5、整合到Pipeline
生成密钥引用片段
pipeline {
agent any
stages {
stage('Pull') {
steps {
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '6a6543d1-3262-4f96-a4b5-4259a9a5c038', url: 'https://gitee.com/lianxing2233/vue-demo.git']])
}
}
stage('Build') {
steps {
sh 'docker build --no-cache --force-rm -t vue-demo:latest ./'
sh 'docker images'
}
}
stage('Push') {
steps {
withCredentials([usernamePassword(credentialsId: 'ed4d4a17-85bb-4395-a6a6-abb439c2301a', passwordVariable: 'AliyuPassword', usernameVariable: 'AliyuUsername')]) {
sh 'docker login -u ${AliyuUsername} -p ${AliyuPassword} registry.cn-hangzhou.aliyuncs.com'
}
sh 'docker tag vue-demo:latest registry.cn-hangzhou.aliyuncs.com/ialso/vue-demo:latest'
sh 'docker push registry.cn-hangzhou.aliyuncs.com/ialso/vue-demo:latest'
}
}
}
post {
always {
emailext(
to: "2750955630@qq.com",
subject: 'CICD:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
body: '${FILE,path="email.html"}'
)
}
}
}
10、邮件配置
10.1、获取邮箱授权码
QQ邮箱->设置->账户->POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服务->管理服务->生成授权码
xkdqwhuiwjoxdcia
10.2、配置jenkins管理员邮箱
Jenkins->Manage Jenkins->Configure System-> Jenkins Location
10.3、配置
Jenkins->Manage Jenkins->Configure System->Extended E-mail Notification
10.4、测试邮件功能
Jenkins->Manage Jenkins->Configure System->E-mail Notification
10.5、整合至Freestyle
10.6、整合至Pipeline
email.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>${ENV,var="JOB_NAME"}-${BUILD_NUMBER}</title>
</head>
<body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
<table width="95%" cellpadding="0" cellspacing="0"
style="font-size: 11pt; font-family: Tahoma,Arial,Helvetica,sans-serif">
<tr>
<td>THIS EMAIL IS SENT BY JENKINS AUTOMATICALLY, PLEASE DON'T REPLY!</td>
</tr>
<tr>
<td>
<h2>
<font color="#0000FF">BUILD RESULT - ${BUILD_STATUS}</font>
</h2>
</td>
</tr>
<tr>
<td><br />
<b>
<font color="#0B610B">DETAILS:</font>
</b>
<ht size="2" width="100%" byte="center" />
</td>
</tr>
<tr>
<td>
<ul>
<li>PROJECT NAME : ${PROJECT_NAME}</li>
<li>PROJECT URL : <a href="${PROJECT_URL}">${PROJECT_URL}</a></li>
<li>BUILD NUMBER : ${BUILD_NUMBER}</li>
<li>STARTED BY : ${CAUSE}</li>
<li>BUILD URL : <a href="${BUILD_URL}">${BUILD_URL}</a></li>
<li>BUILD LOG : <a href="${BUILD_URL}console">${BUILD_URL}console</a></li>
<li>BUILD WORKFLOW : <a
href="${PROJECT_URL}workflow-stage">${PROJECT_URL}workflow-stage</a></li>
</ul>
</td>
</tr>
<tr>
<td>
<font color="#0B610B">BUILD LOG (LAST 200 ROWS):</font></b>
<hr size="2" width="100%" byte="center" />
</td>
</tr>
<tr>
<td><textarea cols="80" rows="30" readonly="readonly"
style="font-family: Courier New">${BUILD_LOG,maxLines=200,escapeHtml=true}</textarea>
</td>
</tr>
</table>
</body>
</html>
pipeline
pipeline {
agent any
stages {
stage('Pull') {
steps {
checkout scmGit(branches: [[name: '*/master']], extensions: [], userRemoteConfigs: [[credentialsId: '6a6543d1-3262-4f96-a4b5-4259a9a5c038', url: 'https://gitee.com/lianxing2233/vue-demo.git']])
}
}
stage('Build') {
steps {
sh 'docker build --no-cache --force-rm -t vue-demo:latest ./'
sh 'docker images'
}
}
}
post {
always {
emailext(
to: "2750955630@qq.com",
subject: 'CICD:${PROJECT_NAME} - Build # ${BUILD_NUMBER} - ${BUILD_STATUS}!',
body: '${FILE,path="email.html"}'
)
}
}
}
11、常见问题
安装后有时候还会出现jenkins与linux时间不一致,可以在Manage Jenkins->Script Console,执行下面命令
System.setProperty('org.apache.commons.jelly.tags.fmt.timeZone', 'Asia/Shanghai')