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.
Here is the easiest and fastest way to use Jenkins. You do not need to configure the Node environment, everything is done with Docker, only need to write a script, no other configuration
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 scripts for VUE and other similar front-end projects, details need to be modified myself. This script requires the use of Dockfile, as shown in the next section
Import the Java. Text. SimpleDateFormat node {try {/ / here is so named to release to tencent docker warehouse, can modify the def dockerId ='tengxun'
def dockerUrl='ccr.ccs.tencentyun.com'
def dockerNamespace='emp'
def dockerName='emp-web'Def env= def env= def env= def env= 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('Npm run build') {
docker.image('node:11-alpine').inside {
sh 'node --version'
sh 'npm --version'
//sh 'npm install'
sh 'npm run build'
}
}
stage('Docker build') {
sh 'pwd'
sh 'ls'
def imageUrl = "${dockerUrl}/${dockerNamespace}/${dockerName}:${dockerTag}"
def customImage = docker.build(imageUrl)
sh "docker rm -f ${dockerName} | true"
customImage.run("-it -p 7100:80 --name ${dockerName} -e env=${env}")
//only retain last 3 images
sh """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 {throw e} finally {throw e} finally {throw e} finally {'[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/emp/emp-web
Vue project structure and Dockerfile
To facilitate the modification of the server URL, add config.js as the external configuration, which can be modified by using the docker run command
Dockerfile writing, using nginx deployment, only for reference, specific according to their own project structure
FROM nginx:1.17.3-alpine
MAINTAINER [email protected]
ENV TZ=Asia/Shanghai
RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && mkdir -p /emp-web
WORKDIR /emp-web
ADD /dist/ /usr/share/nginx/html/
ENV env "test"
CMD sed -i "s/empEnv.active/\"${env}\"/g" /usr/share/nginx/html/config.js && nginx -g 'daemon off; '
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.