preface
Use Docker to build Jenkins service on Centos7 Ali cloud server.
Conditions to be prepared in advance
- Maven environment
- The docker environment
- JDK environment (Centos7 comes with JDK environment)
Docker pull image
Jenkins has both official and private mirror images. Here we select the Jenkins image in Chinese.
docker pull jenkinszh/jenkins-zh
Copy the code
Create Jenkins mount directory and grant permissions
If you are familiar with Docker, you should know that its implementation actually creates an independent container environment in which Jenkins runs, so if we want to configure Jenkins, we need to go into the container and select the file to configure. Although we can use docker exec it [container id] bash to enter the container directory for configuration, even simple vi command cannot be used.
If we specify the mount directory when we start the image, a mapping can be created between the server native and the container.
Therefore, we will create a Jenkins working directory on the server, /var/jenkins_mount, and grant corresponding permissions. Later, we will mount the Jenkins container directory to this directory, so that we can easily modify the configuration file in the container.
mkdir -p /var/jenkins_mount
chmod 777 /var/jenkins_mount
Copy the code
Create and start the Jenkins container
The command is as follows, don’t rush to execute the command, remember to change the path.
docker run -d -p 10240:8080 -p 10241:50000 -v /var/jenkins_mount:/var/jenkins_home -v / root/apache maven - 3.6.3: / usr/local/etc / / maven - v localtime: / etc/localtime - name myjenkins jenkinszh/Jenkins - usefulCopy the code
Notice the meaning of the parameters we specify below.
-d Run the image in the background
-p 10240:8080 Meaning: Port 8080 of the mirror is mapped to port 10240 of the server.
-p 10241:50000 Description: Map port 50000 of the mirror to port 10241 of the server
-v /var/jenkins_mount:/var/jenkins_mount Description: The /var/jenkins_home directory is the Jenkins working directory of the container. We mounted a directory on the hard disk to this directory, which is convenient for using the original working directory after updating the image. Here we set up the /var/jenkins_mount directory we created above
– v/etc/localtime: / etc/localtime meaning: let containers and servers the same time Settings. -v /root/apache-maven-3.6.3: /usr/local/maven-3.6.3: /usr/local/maven-3.6.3 :/usr/local/maven
Then use Docker to start the Jenkins image in the background. Note that Maven must be mounted, otherwise you will not find Maven in the container later. I installed /root/apache-maven-3.6.3. If you install it somewhere else, remember to change the path.
I did not mount the JDK here, because centos comes with the JDK, so I can use the JDK in the container if you are using Windows or other versions of the operating system, it is recommended that you also mount the JDK.
Docker ps can be used to check whether Jenkins is started. If not, use docker ps -a to query all containers, find the container ID corresponding to Jenkins, use Docker rm [container ID] to delete it, and then re-check whether the command is correct, port and other problems. And start the image to create the container.
Can be achieved bydocker logs myjenkins
Command to view the log, you can see that Jenkins is a Spring project.
Enter the container and check whether Maven is successfully mounted. The command to enter the container isDocker exec -it [container ID] bash
Go to the Maven directory we just specified, i.e/usr/local/maven
You can see that there is maven.
4. Configure Jenkins service
Jenkins is a Web service, so we can directly use the Web port to access it.
As the docker above is configured with port 10240, you can access port 10240. If it is a cloud server, remember to open port 10240 of the security group
It’s important to note that what’s shown here isjenkins_home
Path, because I set the directory to bejenkins_mounts
, so my password placement directory is/var/jenkins_mount
So it’s recommended to usecd
andls
Command to access the password directory layer by layer
Enter the password you find.
After entering the password, select the recommended plug-in.Finally, I made it to Jenkins.Install two plug-ins, one ispublish over ssh, one isMaven Integration
After installing the plugin, we need to configure the Maven environment and JDK environment.
After clicking Global configuration, configure Maven to the maven path you just mounted, namely /usr/local/maven
Create a Maven project
After creating the project, select the configuration project
Binding git
Write the commands that Maven will execute,clean install
Specifies how often git changes are checked and compiled if they occur.And specify the policy after the compile, we want to compile directly after the run, using SSH to specify.
After configuring the Maven project we just created, we can build it immediatelyWe looked at the console output and found that the POM file was not found because I had not created a Maven project on Git.So create a Maven project and upload it to Git.After that, every time we submit the project to Git, after the build is compiled, it will be automatically deployed online.
reference
www.likecs.com/show-103109… www.cnblogs.com/wenwen20/p/…
Automatically build and deploy the project using Jenkins after compiling and packaging