This is the fifth day of my participation in the August More text Challenge. For details, see: August More Text Challenge

Daily sentence

No one can become wise by learning. Learning may be obtained by diligence, but wit and wisdom is lazy talent.

If the profile

  • Under Jenkins, the Java project is automatically deployed with DockerFile, and the deployment of the project is confidently pushed to the container era mechanism.
  • This section requires a little knowledge of Jenkinsfile, a little knowledge of Dockerfile, a little knowledge of shell, and a little knowledge of Docker

Environment to prepare

New-build a Maven project

It should be noted that this option is not available for new installations, so you need to install Maven Integration in the plugin

Java configuration, other configuration see previous articles

Published to the server, DockerFile deployment is built automatically

The Publish SSH configuration

Execute the process

  1. Jenkins pulls the code from the repository
  2. Jenkins execute jenkinsfile file (name can be specified)
  3. Build the pulled project into a Docker image on Jenkins’ server
  4. Publish the image to the image repository
  5. Pull the image down from the node of the application server (private repository requires user name/password)
  6. Execute the image on the application server

Jenkins configuration

Create a Pipeline SCM project for Jenkins named Sample-Tezst (the same as the released project name)

  • 【Pipeline Script from SCM】

  • Branch Specifier(blank for ‘any’) : Select branches

  • Additional Behaviours: Add email notifications to senders

  • Script Path: Fill in the name of the Jenkinsfile file in the use project

Application engineering configuration

Create the Dockerfile file and Jenkins_docker file in the project root directory

The jenkinsfile_docker file contains the following contents:

Docker integrated Docker deployment
pipeline { agent {label 'master'} tools{ maven 'maven' } environment { GIT_PROJECT_ADDR = "[email protected]: uncleqiao/springboot - test. Git" / / project git address JENKINS_WORKSPACE = "/ root /. Jenkins/workspace" The address of the store files / / Jenkins PROJECT_NAME = "${JOB_NAME}" / / project name JAR_NAME = "sample - tezst - 0.0.1 - the SNAPSHOT. Jar" / / project to generate the name of the jar IMAGE_NAME="sample-tezst" IMAGE_ADDR="repository/qiao_namespace/${IMAGE_NAME} VERSION_ID="${BUILD_ID}" } stages { stage('pullCode'){ steps{ echo 'This is a pullCode step' //git branch: "${BRANCH}", credentialsId: '1001', url: "${GIT_PROJECT_ADDR}" checkout scm } } stage('Build') { steps{ echo 'This is a Build step' // Sh 'MVN clean package -dmaven.test. skip=true'}} // Create directory (if it does not exist), And put the jar files uploaded to the directory stage (' SSH ') {steps {echo 'push jar to the target server' sh "' ole_image_id = ` docker images | grep ${IMAGE_NAME}|grep ${VERSION_ID}|awk '{print $3}'` if [[ -n "${ole_image_id}" ]]; then docker rmi -f ${ole_image_id} fi docker build -f Dockerfile --build-arg jar_name=${JAR_NAME} -t ${IMAGE_NAME}:${VERSION_ID} . new_image_id=`docker images|grep ${IMAGE_NAME}|grep ${VERSION_ID}|awk '{print $3}'` sudo docker tag ${new_image_id} ${IMAGE_ADDR}:${VERSION_ID} sudo docker push ${IMAGE_ADDR}:${VERSION_ID} ''' } } stage('run') {//customWorkspace "${SERVER_TARGET_PATH}"}} options {//customWorkspace "${SERVER_TARGET_PATH}" SkipDefaultCheckout ()} steps {echo 'pull image and docker run' withEnv(['JENKINS_NODE_COOKIE=dontKillMe']) { sh ''' sudo docker login --username=yourusername --password=yourpassword ccr.ccs.tencentyun.com sudo docker pull ${IMAGE_ADDR}:${VERSION_ID} container_id=`docker ps|grep ${IMAGE_ADDR}:${VERSION_ID}|awk '{print $1}'` if [ -n "${container_id}" ]; then docker rm -f "${container_id}" fi old_pid=`ps -ef|grep ${JAR_NAME}|grep -v grep|awk '{print $2}'` if [[ -n $old_pid ]]. then kill -9 $old_pid fi old_image=`docker images|grep ${IMAGE_ADDR}|grep ${VERSION_ID}` if [[ -n $old_image ]]; then old_image_id=`echo ${old_image}|awk '{print $3}'` docker rmi -f ${old_image_id} fi sudo docker run --name "${PROJECT_NAME}_${VERSION_ID}" -p 9001:8081 -d ${IMAGE_ADDR}:${VERSION_ID} ''' } } } } }Copy the code

Note:

If [-n STR] if [-n STR −a”{STR} -a “STR −a”{STR}x”!= “x”] if [[-n STR]] {STR}]] for shell syntax, STR]] for shell syntax, () and ‘ ‘(two single quotes) can be executed in subshells, but $() is not supported

Idea has no syntax hint for Jenkinsfile, which is very error prone, this is uncomfortable

Step 1: Pull the item in the pullCode step

Checkout SCM, which was selected when creating the Jenkins pipeline, is a special variable that represents your warehouse information and automatically retries projects from the warehouse

Step 2: 【Build】

Compile and package the pulled item

Step 3: 【build_image】

Build the packaged project into a Docker image and push it to the image repository (this can be divided into two steps)

Example Obtain an existing mirror
ole_image_id=`docker images| grep ${IMAGE_NAME} | grep ${VERSION_ID}|awk '{print $3}'`
Copy the code
Delete the existing mirror
if [[ -n "${ole_image_id}" ]]; then
	docker rmi -f ${ole_image_id}
fi
Copy the code
  • Build the image from the Dockerfile in the project root directory
    • -f: [filename] –build-arg

      Passes arguments to the Dockerfile
      =value>
    • -t: Name of the mirror: version here I use Jenkins’ build count as the version of the mirror
docker build -f Dockerfile --build-arg jar_name=${JAR_NAME} -t ${IMAGE_NAME}:${VERSION_ID} .
Copy the code
Gets the ID of the built image
new_image_id=`docker images | grep ${IMAGE_NAME} | grep ${VERSION_ID} | awk '{print $3}'`
Copy the code
Based on the generated image, the tag produces an image with different namespaces
sudo docker tag ${new_image_id} repoistory/qiao_namespace/${IMAGE_NAME}:${VERSION_ID}
Copy the code
Push the images recognized by the image repository to the repository
sudo docker push repoistory/qiao_namespace/${IMAGE_NAME}:${VERSION_ID}
Copy the code

Step 4: 【run】

The application server pulls the project and runs it. This step selects the node (if the application server is a cluster, do not log in the node using SCP).

Log in to the mirror store
sudo docker login --username=yourusername --password=yourpassword repository
Copy the code
Pull the generated image from the repository
sudo docker pull repository/qiao_namespace/${IMAGE_NAME}:${VERSION_ID}
Copy the code
If you look at the existing container, if it exists, delete it if it exists, delete it if it exists it should be an array, there’s a little bit of a problem, I need to fix it
container_id=`docker ps | grep ${IMAGE_ADDR}:${VERSION_ID} | awk '{print $1}'`
if [ -n "${container_id}" ]; then
	docker rm -f "${container_id}"
fi
Copy the code
View existing projects of the same version and delete them if they exist
pid=`ps -ef | grep ${JAR_NAME}| grep -v grep | awk '{print $2}'`
if [[ -n $pid ]]; then
	kill -9 $pid
fi
Copy the code
View an existing mirror
old_image=`docker images|grep ${IMAGE_ADDR}|grep ${VERSION_ID}`
if [[ -n $old_image ]]; then
	old_image_id=`echo ${old_image}|awk '{print $3}'`
    docker rmi -f ${old_image_id}
fi
Copy the code

Run the container

sudo docker run --name "${PROJECT_NAME}_${VERSION_ID}" -p 9001:8081 -d ${IMAGE_ADDR}:${VERSION_ID}
Copy the code

Dockerfile

The file content is as follows

FROM repository/qiao_namespace/myjdk8:v1
Copy the code
The default jar package name notice that the separator is :- this is passed by jenkinsfile when build Dockerfile
ARG jar_name = {jar_name: - sample - teszt - 0.0.1 - the SNAPSHOT. Jar}Copy the code
RUN is used to RUN commands inside a container
RUN mkdir -p /usr/local/project
WORKDIR /usr/local/project
Copy the code
Place the project under /usr/local/project
COPY./target/ sample-teszt-0.0.1-snapshot.jar./ EXPOSE 8081 CMD java-jar-dserver. port=8081 Springboot - test - 0.0.1 - the SNAPSHOT. The jarCopy the code

After that we log on to the application server and see that the container has started