We have been publishing projects with Jenkins for over 4 years and have made several improvements to minimize Jenkins configuration and release script writing. From the initial construction of a free style software project to the pipeline-line project to the current pipeline-Docker, pipeline-line can make the project release process clearer, and Docker can greatly reduce Jenkins configuration.
The following is the easiest and fastest way to use Jenkins. You don’t need to configure git, Maven, JDK, Node and other environments. Everything is done with Docker
This tutorial is only an advanced tutorial, the specific Jenkins installation, Git credentials configuration, email and other need to find the tutorial for configuration.
The environment used in this tutorial
- centos7
- Docker-ce 18.09.1(You must use Docker-CE, the old version of Docker will cause docker daemon to be unavailable in Jenkins)
The preparatory work
Install the docker
yum install docker-ce
Configure docker accelerator (optional)
Many manufacturers provide Docker acceleration services, such as Ali Cloud, Tencent cloud details please find your own Baidu. Without a domestic accelerator, image downloading may be slow.
Install Jenkins using Docker
Docker Pull Jenkins/Jenkins, do not docker Pull Jenkins has been abandoned
Jenkins Docker Hub address
Start the Jenkins
docker run -u root -itd --name jenkins -p 6001:8080 -v $(which docker):/usr/bin/docker -v /var/run/docker.sock:/var/run/docker.sock -e TZ="Asia/Shanghai" -v /etc/localtime:/etc/localtime:ro -v /volume1/docker/jenkins:/var/jenkins_home jenkins/jenkins
Copy the code
Description of Start Commands
-p 6001:8080Jenkins The default webpage access port is 8080, and the port is mapped to an external host
– v $(which docker) : / usr/bin/docker – v/var/run/docker. The sock: / var/run/docker. Inside the sock that Jenkins docker command may be used
– e TZ = “Asia/Shanghai” – v/etc/localtime: / etc/localtime: ro configuration Jenkins container time zone
– v/volume1 / docker/Jenkins: / var/Jenkins configuration is mapped to the external host jenkins_home rolling, container delete can still retain the configuration
Enter the Jenkins container and judge whether the Docker command is executed normally
docker exec -it jenkins bash
docker info
You may need to install libltdl7 if you need to run the following command
apt-get update
apt-get install -y libltdl7
Configuration Jenkins
Http://host IP: 6001 You can visit Jenkins’ webpage and ignore other processes. All defaults are ok.
No need to configure git, Maven, JDK, Node and other environments, we use docker solution
Create a new assembly line project
There are two ways to write pipelining scripts, official documents
Using Docker with Pipeline, official documentation
Write a script
Share my own project release script, suitable for Spring Boot project, details need to be modified. This script requires the use of Dockfile, as shown in the next section
Import the Java. Text. SimpleDateFormat node {try {/ / name to write like this is can be published to the tencent docker warehouse, can change the def dockerId ='tengxun'
def dockerUrl='ccr.ccs.tencentyun.com'
def dockerNamespace='esms'
def dockerName='esms-api'
def env='test'
def dateFormat = new SimpleDateFormat("yyyyMMddHHmm")
def dockerTag = dateFormat.format(new Date())
stage('git pull'){
sh 'pwd'
git credentialsId: 'Fill git credentials here, need to be configured in Jenkins credentials', url: 'Git address here'
}
stage('mvn install') {
sh 'pwd'
docker.image('the maven: 3.6.0 - JDK - 8 - alpine').inside('-v /volume1/docker/.m2:/root/.m2') {
sh 'mvn --version'
sh 'mvn clean install'
}
}
stage('docker run') {
dir("esms-main") {
sh 'pwd'
def imageUrl = "${dockerUrl}/${dockerNamespace}/${dockerName}:${dockerTag}"
def customImage = docker.build(imageUrl)
sh "docker rm -f ${dockerName} | true"Docker network create esms-net, which is used to interact with multiple services. Optional: customImage.run("-it -d --name ${dockerName} -p 8090:8090 --network esms-net -e SPRING_PROFILES_ACTIVE=${env}") // Only retain last 3 images"""docker rmi \$(docker images | grep ${dockerName} | sed -n '4,\$p' | awk '{print \$3}') || true"""
}
}
currentBuild.result="SUCCESS"
} catch (e) {
currentBuild.result="FAILURE"Throw e} finally {// If you want to publish mail, you need to configure the mail server mail to in system Administration-system Settings:'[email protected]',subject: "Jenkins: ${currentBuild.fullDisplayName}.${currentBuild.result}",body:"${currentBuild.result}"}}Copy the code
After the final release of docker images, the name format is similar to the following, such as using the above code after the release of the name is
ccr.ccs.tencentyun.com/esms/esms-api
Sprint Boot project structure and Dockerfile
Dockerfile writing, only for reference, specific according to their own project structure
FROM openjdk:8-jdk-alpine
MAINTAINER [email protected]
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && mkdir -p /esms
WORKDIR /esms
EXPOSE 8090
ADD ./target/esms.jar ./
CMD java -Djava.security.egd=file:/dev/./urandom -jar esms.jar
Copy the code
Publish the project
Directly build, not do git webhook, can not link git push, etc., if you need to add their own
conclusion
The overall process is very simple, mainly is to install Jenkins-> write pipeline script -> write Dockerfile script.