1、环境准备:准备安装好docker的服务器一台
2、在服务器上创建一个目录用于安装Jenkins
mkdir jenkins
3、下载好要挂载的:maven、jkd;并将下载好的tar.gz包上传至服务器待安装目录中并解压
tar -xzvf tar -xzvf apache-maven-3.9.6-bin.tar.gz
tar -xzvf graalvm-jdk-21_linux-x64_bin.tar.gz
4、修改解压后maven中的setting配置文件
主要修改仓库存储路径和镜像仓库
<localRepository>/root/jenkins/repo</localRepository><!-- 配置阿里云镜像地址 --><mirror><id>alimaven</id><name>aliyun maven</name><url>http://maven.aliyun.com/nexus/content/groups/public/</url><mirrorOf>central</mirrorOf></mirror>
5、配置maven和jdk的PATH挂载文件,git用系统默认的就行,不需要额外配置
mkdir -p ~/jenkins/conf
vi profile
profile内容
export MAVEN_HOME=/usr/local/maven/apache-maven-3.9.6
export PATH=$MAVEN_HOME/bin:$PATH
export JAVA_HOME=/usr/local/java/graalvm-jdk-21.0.2+13.1
export PATH=$JAVA_HOME/bin:$PATH
6、配置jenkins的docker-compose.yml文件
version: '3.9'services:jenkins:image: jenkins/jenkins:2.440.2container_name: jenkinsuser: rootrestart: alwaysdeploy:resources:limits:cpus: '1.00'memory: 1Greservations:cpus: '0.10'memory: 256Mports:- "8080:8080"- "50000:50000"volumes:- /var/run/docker.sock:/var/run/docker.sock- /usr/bin/docker:/usr/bin/docker- ./data:/var/jenkins_home- ./apache-maven-3.9.6:/usr/local/maven/apache-maven-3.9.6- ./graalvm-jdk-21.0.2+13.1:/usr/local/java/graalvm-jdk-21.0.2+13.1- ./conf/profile:/root/.bashrcenvironment:- TZ=Asia/Shanghaihealthcheck:test: ["CMD", "curl", "-f", "http://localhost:8080/login"]interval: 30stimeout: 10sretries: 8start_period: 60s
7、启动好后访问,跟随引导安装推荐插件和创建账户
8、配置环境变量,只需配置:系统管理–全局属性–环境变量:PATH
path获取方法:进入Jenkins的docker中进行获取
docker exec -it jenkins bash
echo $PATH
补充:
以上配置在jenkins中执行mvn -v命令查看mvn对应的java版本时,发现还是旧的版本,跟挂载的版本不一致,这是因为jenkins去执行时使用的是Jenkins启动时的JAVA_HOME导致的,此时需要增加下环境变量进行覆盖就可
查看镜像设置的JAVA_HOME
docker inspect jenkins/jenkins:2.440.2
PATH变量
/usr/local/java/graalvm-jdk-21.0.2+13.1/bin:/usr/local/maven/apache-maven-3.9.6/bin:/opt/java/openjdk/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
配置完成后截图:
9、访问403异常错误排查:
MESSAGE: No valid crumb was included in the request
处理办法:
将【启用代理兼容】勾上
10、新建一个流水线任务并配置pipeline脚本:
pipeline {agent anystages {stage('开始') {// 开始steps {echo "开始"}}stage('环境检测') {steps {// 构建环境检测sh '''cat /proc/versionfree -mdf -hdocker -vgit -vmvn -vjava -version'''echo 'Hello jenkins'}}stage('你好 jenkins') {// 结束steps {echo "结束"}}}
}
配置截图:
11、保存后点击:立即构建