1.启动命令
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
- 启动jenkins报错webroot: EnvVars.masterEnvVars.get("JENKINS_HOME")
- 答:--restart=always 位置写错了,不能写在jenkinsci/blueocean后面
2.jenkins的home文件夹
docker volume inspect jenkins-data
[
{
"CreatedAt": "2022-01-13T14:58:48+08:00",
"Driver": "local",
"Labels": null,
"Mountpoint": "/var/lib/docker/volumes/jenkins-data/_data",
"Name": "jenkins-data",
"Options": null,
"Scope": "local"
}
]
可以看到/var/lib/docker/volumes/jenkins-data/_data
就是jenkins的home目录
3.jenkins自动化构建项目流程
-
1.创建流水线项目,选择Pipeline script from SCM,填入你项目的git地址,我目前先在gitee上创建了一个git项目,填入
image.png -
2.配置push代码自动化构建
-
2.1.先在jenkins个人设置中获取自己的token
image.png -
2.2 jenkins配置构建触发器
-
2.3 配置gitee的webhook
image.png
-
填入的URL格式是 http://令牌名:token@JENKINS_URL/job/jenkins-test/build?token=TOKEN_NAME
之后点击测试,可发现请求响应201成功,之后你每次在gitee上push代码,jenkins就可以进行自动化远程构建。
- 2.4 安装docker插件
- 2.5 Jenkinsfile编写maven打包的步骤
stage('打包') {
agent {
docker {
image 'maven:3.8.4-eclipse-temurin-17-alpine'
}
}
steps {
echo "开始打包"
sh "mvn -v"
sh "mvn clean package -s ${JENKINS_HOME}/config/maven/settings.xml -Dmaven.test.skip=true"
}
}
-
- docker打包
- 3.1 配置正确的工作目录
因为我们第二步的编译工作是在临时目录,而第四步的生成镜像工作目录是代码的目录,所以第二步打出来的jar包在第四步获取不到,解决办法是定义一个环境变量保存代码的目录,让第二步编译的时候在代码目录编译。
environment {
WS = "${WORKSPACE}"
}
sh "cd ${WS} && mvn clean package -s ${JENKINS_HOME}/config/maven/settings.xml -Dmaven.test.skip=true"
- 3.2 生成镜像
stage('生成镜像') {
steps {
echo "开始生成镜像"
sh "docker build -t jenkins-test ."
}
}
- 3.3 部署
stage('部署') {
steps {
echo "开始部署"
sh "docker rm -f jenkins-test"
sh "docker run -d -p 8888:8080 --name=jenkins-test jenkins-test"
}
}
附录
完整的jenkinsfile
pipeline {
agent any
environment {
WS = "${WORKSPACE}"
}
stages {
stage('检查环境变量') {
steps {
echo "开始检查环境变量"
sh "pwd && ls -alh"
sh "printenv"
}
}
stage('编译') {
agent {
docker {
image 'maven:3.8.4-eclipse-temurin-17-alpine'
args '-v ${JENKINS_HOME}/config/maven/.m2:/root/maven/.m2'
}
}
steps {
echo "开始编译"
sh "mvn -v"
sh "cd ${WS} && mvn clean package -s ${JENKINS_HOME}/config/maven/settings.xml -Dmaven.test.skip=true"
}
}
stage('测试') {
steps {
echo "开始测试"
}
}
stage('生成镜像') {
steps {
echo "开始生成镜像"
sh "docker build -t jenkins-test ."
}
}
stage('部署') {
steps {
echo "开始部署"
sh "docker fm -f jenkins-test"
sh "docker run -d -p 8888:8080 --name=jenkins-test jenkins-test"
}
}
}
}
完整的dockerfile文件
FROM openjdk:18-jdk-alpine3.15
LABEL maintainer="295183917@qq.com"
ADD target/jenkins-test-0.0.1-SNAPSHOT.jar demo.jar
RUN ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime; \
echo 'Asia/Shanghai' >/etc/timezone; \
touch /app.jar;
ENV JAVA_OPTS = ""
ENV PARAMS = ""
EXPOSE 8080
ENTRYPOINT ["java", "-jar", "demo.jar"]
网友评论