Install and deploy the Docker Jenkins environment

Create a folder for Jenkins configuration

mkdir /Users/mengfanxiao/docker/jenkins
Copy the code

Download the Docker Jenkins image

docker pull jenkins/jenkins
Current version: Jenkins 2.243Copy the code

Boot image

docker run -d -p 8080:8080 -p 50000:50000 --name jenkins --privileged=true -v /Users/mengfanxiao/docker/jenkins:/var/jenkins_home jenkins/jenkins
Copy the code

Visit Jenkins

http://localhost:8080/
Copy the code

PS: If Jenkins has been Please wait while he is getting ready to work… Page, resource access is too slow to load out

The solution

Find/Users/mengfanxiao/docker/Jenkins directoryHudson. Model. UpdateCenter. XML file,https://updates.jenkins.io/update-center.jsonReplaced by http://mirror.xmission.com/jenkins/updates/update-center.jsonThen reboot docker JenkinsCopy the code

Log in to your account

Enter the password
The start command has a file mapping that maps the configuration files in docker to the local computerNamely - v/Users/mengfanxiao/docker/Jenkins: / var/jenkins_home Jenkins/JenkinsNow that Docker Jenkins has been started, the configuration file is mapped from Docker to a local copyCopy the code

By "input password" screenshot above we can see to the/var/jenkins_home/secrets/initialAdminPassword find initial password file(Jenkins is missing from the directory on the screenshot)So just from the local file/Users/mengfanxiao docker/Jenkins/secrets/initialAdminPassword canCopy the code

Installing a plug-in


Select the recommended plug-in to install hereCopy the code

PS: How to solve the long waiting time here?

Solutions:

Modify Updated the configuration source
  • Find/Users/mengfanxiao docker/Jenkins/updates/default. The json

  • Change “www.google.com “to “http://www.baidu.com/”

  • “Replace” the “updates.jenkins-ci.org/download mirrors.tuna.tsinghua.edu.cn/jenkins”

Quick way to update

Use vim’s command

  • Replace urls for all plug-in downloads
: 1,$s/http:\/\/updates.jenkins-ci.org\/download/https:\/\/mirrors.tuna.tsinghua.edu.cn\/jenkins/g
Copy the code
  • Replace the connection test URL
: 1,$s/http:\/\/www.google.com/https:\/\/www.baidu.com/g
Copy the code

Note: enter vim first type: and then paste the above: after the command, do not write two colons! Save the changes and exit :wq

Kaka is so fast (tears of happiness)

Create a user

After creating a user, you can enter the home pageCopy the code


Review and repair plug-ins

Viewing failed plug-ins

  • If any plug-in fails to be installed on the system management page, it will be displayed (name and cause of the failed plug-in).

  • View the installed plug-ins
http://localhost:8080/pluginManager/installed 
Red is the plug-in that failed to installCopy the code

  • The function is not complete
Install the Maven plugin if there is no Build a Maven project option when creating a taskCopy the code

How to install plug-ins

For example, the above features are incomplete – there is no [Build Maven project]

  • Click on the optional plug-in and enter Maven
http://localhost:8080/pluginManager/available
Copy the code

  • The effect after installation


Restart to make the newly installed plug-in take effect

There is a restart prompt at the bottom of the installed plug-in page if the plug-in changes,The newly installed plug-in will take effect after you restart itCopy the code

Deploy the project through Jenkins

Creating a Task


The General configuration


Git configuration

Git Information Configuration
Access git account configuration
To specify a branch means to obtain the specified branch below the specified Git address for code deploymentCopy the code

build

Maven needs to be configured here


clean package -U -Dmaven.test.skip=true
-U Indicates forcible updateCopy the code

Post Steps

The operation prior to this step is for Jenkins to compile the specified project via Maven into a JAR or war package or other formThis step is to access the specified server and execute the specified script in the specified path to start the JAR or WAR or whateverTherefore, you need to configure the following Server: System Management - System Configuration -SSH ServerAnd test to see if it can connectCopy the code




  • The source file indicates whether the current project is compiled and packaged as a JAR or a WAR package

  • Executable file

root@iZuf6heduaqlfxz2wj9zc3Z:~# cat /home/jenkins/vote-api/vote-api-test.sh
#! /bin/bash
Configure the JDK environment
export JAVA_HOME=/usr/local/ software/jdk1.8.0 _141export JAR_HOME=$JAVA_HOME/bin
export CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH Configure the project name APP_NAME=vote-api.jar If the process exists, kill it is_exist() {pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' ` # return 1 if none exists, return 0 if none exists if [ -z "${pid}" ]; then return 1 else return 0 fi } is_exist if [ $? -eq "0" ]; then kill9 -$pid else echo "${APP_NAME} is not running" fi Delete the last compiled and packaged file rm -rf /home/test-vote-api/*.jar # Copy the Jenkins package compiled file to the specified location cp -rf /home/jenkins/vote-api/target/*.jar /home/test-vote-api/${APP_NAME} # Start a project nohup java -jar -Dspring.profiles.active=dev /home/test-vote-api/${APP_NAME} -Xms256m -Xmx512m -XX:MetaspaceSize=128m -XX:MaxMetaspaceSize=512m --server.port=8082 >/home/test-vote-api/logs/vote-api.log 2>&1 & echo "Program started..." # Whether the process exists is_exist if [ $? -eq "0" ]; then echo "${APP_NAME} is running. pid is ${pid} " else echo "${APP_NAME} is not running." fi exit  Copy the code

Pay attention to the point

  • The folders in the above script are created first
mkdir /home/test-vote-api/logs
Copy the code
  • PS: The file access permission is incorrect
If Jenkins does not access the server as user root when configuring the serverLet's say Jenkins usersCopy the code

The access permissions for viewing files are read/write and executable for the current user, read/write and executable for the user group to which the current user belongs, and read/write and executable for other users
That's the only way Jenkins users can access it
If this is not the case, chmod 777 /home/Jenkins -r is required to set the folder access permissionCopy the code

Email notification of project deployment


build


Afterword.

Now that I've covered the Jenkins environment setup process and how to deploy a project through Jenkins,I will not demonstrate the build process because I used the company's server, worried about running out of memory, the company's running projects to stop 😂Copy the code

This article is formatted using MDNICE