Jenkins官网实例----Build a Java app with Maven

Build a Java app with Maven 

来自 <https://www.jenkins.io/doc/tutorials/build-a-java-app-with-maven/>

   

先创建network

docker network create jenkins

  

运行容器jenkins-docker,网络别名为docker。

docker run \

--name jenkins-docker \

--rm \

--detach \

--privileged \

--network jenkins \

--network-alias docker \

--env DOCKER_TLS_CERTDIR=/certs \

--volume jenkins-docker-certs:/certs/client \

--volume jenkins-data:/var/jenkins_home \

--publish 2376:2376 \

--publish 3000:3000 --publish 5000:5000 \

docker:dind \

--storage-driver overlay2

    

   

vi Dockerfile

FROM jenkins/jenkins:2.387.1

USER root

RUN apt-get update && apt-get install -y lsb-release

RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \

https://download.docker.com/linux/debian/gpg

RUN echo "deb [arch=$(dpkg --print-architecture) \

signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \

https://download.docker.com/linux/debian \

$(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list

RUN apt-get update && apt-get install -y docker-ce-cli

USER jenkins

RUN jenkins-plugin-cli --plugins "blueocean docker-workflow"

   

构建

docker build -t myjenkins-blueocean:2.387.1-1 .

   

   

  

修改版:

FROM jenkins/jenkins:2.387.1

USER root

ENV http_proxy=http://deb.debian.org/debian/dists/bullseye/InRelease

ENV https_proxy=https://deb.debian.org/debian/dists/bullseye/InRelease

ENV http_proxy=http://deb.debian.org/debian-security/dists/bullseye-security/InRelease

ENV https_proxy=https://deb.debian.org/debian-security/dists/bullseye-security/InRelease

ENV http_proxy=http://deb.debian.org/debian/dists/bullseye-updates/InRelease

ENV https_proxy=https://deb.debian.org/debian/dists/bullseye-updates/InRelease

RUN apt-get update && apt-get install -y lsb-release

RUN curl -fsSLo /usr/share/keyrings/docker-archive-keyring.asc \

https://download.docker.com/linux/debian/gpg

RUN echo "deb [arch=$(dpkg --print-architecture) \

signed-by=/usr/share/keyrings/docker-archive-keyring.asc] \

https://download.docker.com/linux/debian \

$(lsb_release -cs) stable" > /etc/apt/sources.list.d/docker.list

RUN apt-get update && apt-get install -y docker-ce-cli

USER jenkins

RUN jenkins-plugin-cli --plugins "blueocean docker-workflow"

   

   

排错参考:

Docker Error: Could not connect to deb.debian.org (111: Connection refused) [closed]

来自 <https://stackoverflow.com/questions/65297987/docker-error-could-not-connect-to-deb-debian-org-111-connection-refused>

   

实测无效,后面重启服务器后正常。

8步有点慢

   

 

Run your own myjenkins-blueocean:2.387.1-1 image as a container in Docker

docker run \

--name jenkins-blueocean \

--detach \

--network jenkins \

--env DOCKER_HOST=tcp://docker:2376 \

--env DOCKER_CERT_PATH=/certs/client \

--env DOCKER_TLS_VERIFY=1 \

--publish 8080:8080 \

--publish 50000:50000 \

--volume jenkins-data:/var/jenkins_home \

--volume jenkins-docker-certs:/certs/client:ro \

--volume "$HOME":/home \

--restart=on-failure \

--env JAVA_OPTS="-Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true" \

myjenkins-blueocean:2.387.1-1

   

  

Display the Jenkins console log with the command:

   

docker logs jenkins-blueocean

   

  

git clone github后推到自己的gitlab

   

克隆并推送到gitlab

git clone https://github.com/jenkins-docs/simple-java-maven-app.git

   

git remote -v

origin https://github.com/jenkins-docs/simple-java-maven-app.git (fetch)

origin https://github.com/jenkins-docs/simple-java-maven-app.git (push)

   

git remote remove origin

   

git remote add origin http://192.168.1.143/root/simple-java-maven-app.git

   

git remote -v

origin http://192.168.1.143/root/simple-java-maven-app.git (fetch)

origin http://192.168.1.143/root/simple-java-maven-app.git (push)

   

git push -u origin --all

     

在根目录创建文件Jenkinsfile并重新推送

cat Jenkinsfile

   

pipeline {
agent {
docker {
image 'maven:3.9.0-eclipse-temurin-11'
args '-v /root/.m2:/root/.m2'
}
}
options {
skipStagesAfterUnstable()
}
stages {
stage('Build') {
steps {
sh 'mvn -B -DskipTests clean package'
}
}
stage('Test') {
steps {
sh 'mvn test'
}
post {
always {
junit 'target/surefire-reports/*.xml'
}
}
}
stage('Deliver') {
steps {
sh './jenkins/scripts/deliver.sh'
}
}
}
}

 

git add .

git commit -m "add Jenkinsfile"

git push

  


Jenkins创建流水线

simple-java-maven-app

   

   

   排错

构建失败

   

   

修改DNS不成功

[root@localhost jenkins]# docker exec -it 0e48a9cc38b1 /bin/bash

jenkins@0e48a9cc38b1:/$ cat /etc/resolv.conf

nameserver 127.0.0.11

options ndots:0

   

   

  

修改为以下,也无效。

docker run \

--name jenkins-blueocean \

--detach \

--network jenkins \

--env DOCKER_HOST=tcp://docker:2376 \

--env DOCKER_CERT_PATH=/certs/client \

--env DOCKER_TLS_VERIFY=1 \

--publish 8080:8080 \

--publish 50000:50000 \

--volume jenkins-data:/var/jenkins_home \

--volume jenkins-docker-certs:/certs/client:ro \

--volume "$HOME":/home \

--restart=on-failure \

--env JAVA_OPTS="-Dhudson.plugins.git.GitSCM.ALLOW_LOCAL_CHECKOUT=true" \

--dns=8.8.8.8 \

myjenkins-blueocean:2.387.1-1

   

   


经检查是容器jenkins-docker(网络别名为docker)停用了导致了!

上面报错显示是从此地址去获取镜像 https://docker:2376

--network-alias docker \
Makes the Docker in Docker container available as the hostname docker within the jenkins network.

  


   

posted @ 2023-04-17 22:07  ST运维  阅读(88)  评论(0编辑  收藏  举报