Jenkins Automatically Deploy gitee Project to Aliyun ECS Server
Environmental preparation
Set up maven, java, git environment
Jenkins Installation
Jenkins (URL is https://jenkins.io/) is an open source project, which is a continuous integrated Java-based system.We can configure continuous integration tasks on Jenkins to allow machines to build automatically, focus on errors in the integration, provide detailed log files, alert functions, and chart trends and stability in project building.
Linux Installation Jenkins
wget -q -O - https://pkg.jenkins.io/debian/jenkins.io.key | sudo apt-key add - sudo sh -c 'echo deb http://pkg.jenkins.io/debian-stable binary/ > /etc/apt/sources.list.d/jenkins.list' //Here is Ubuntu for installation sudo apt-get update sudo apt-get install jenkins //CentOS Installation Default install with yum sudo yum update sudo yum install jenkins
When the installation is complete, it will automatically start the Jenkins service.Let's take a look at its status. As you can see from the screen output, it's currently working properly.
[root@iZ2vc1e3nnjpo1cayr4utfZ ~]# sudo systemctl status jenkins ● jenkins.service - LSB: Jenkins Automation Server Loaded: loaded (/etc/rc.d/init.d/jenkins; generated) Active: active (running) since Fri 2021-06-25 21:37:28 CST; 21h ago Docs: man:systemd-sysv-generator(8) Process: 1712 ExecStart=/etc/rc.d/init.d/jenkins start (code=exited, status=0/SUCCESS) Tasks: 86 (limit: 10995) Memory: 1.0G CGroup: /system.slice/jenkins.service ├─1669 java -jar /www/wwwroot/myblog/myblog.jar --server.port=443 └─2661 /etc/alternatives/java -Dcom.sun.akuma.Daemon=daemonized -Djava.awt.headless=tru> Jun 25 21:37:25 iZ2vc1e3nnjpo1cayr4utfZ systemd[1]: Starting LSB: Jenkins Automation Server... Jun 25 21:37:25 iZ2vc1e3nnjpo1cayr4utfZ runuser[1742]: pam_unix(runuser:session): session opened f> Jun 25 21:37:28 iZ2vc1e3nnjpo1cayr4utfZ runuser[1742]: pam_unix(runuser:session): session closed f> Jun 25 21:37:28 iZ2vc1e3nnjpo1cayr4utfZ jenkins[1712]: Starting Jenkins [ OK ] Jun 25 21:37:28 iZ2vc1e3nnjpo1cayr4utfZ systemd[1]: Started LSB: Jenkins Automation Server.
The default listening port for Jenkins is 8080, which is accessible by browser input of the ubuntuvm1 virtual server address and port 8080.First access needs to be unlocked, please follow the screen prompt.For example, the password found in the virtual machine is "2bcb508f031743d5ada3fefb93e3167d", fill it in the "Administrator password" input box, and press the "Continue" button to submit.
Choose to have Jenkins load commonly used plug-ins.
Jenkins Configuration
1. Modify the Jenkins default port
The default port is 8080, which is often used, so change to an unusual port to access Jenkins and modify the default access port of Jenkins through the vim editor
vim /etc/sysconfig/jenkins
JENKINS_USER set to root user
2. Plug-in Installation
Go to Manage Jenkins -> Manage Plugins -> Available and search for Gitee in Filter:
In the optional list below, check Gitee (if Gitee does not exist in the list, click Check now to update the list of plug-ins), and then click Download now and install after restart.
Check "Restart Jenkins when installation is complete and no jobs are running" on the installation page
Once installed, you can see the Gitee plug-in on the Install page
3. Gitee Plugin Configuration
1) Go to Jenkins -> Manage Jenkins -> Configure System -> Gitee Configuration -> Gitee Link
2) Enter Gitee or the name you want in the link name
3) Enter the full URL address of the code cloud in the Gitee domain name URL: https://gitee.com (CodeCloud Private Customer Enter Deployed Domain Name)
4) If the code cloud APIV5 private token is not configured in the certificate token, click Add - > Jenkins
Domain Select Global Credentials
Kind Selects Gitee API Token
Scope Choose the Scope You Need
Gitee API Token Enter your code cloud private token and get the address:https://gitee.com/profile/personal_access_tokens
ID, just enter the ID and description you want in Descripiton.
5) Credentials chooses the configured Gitee API Token
6) Click Advanced to configure whether to ignore SSL errors (which is supported in your Jenkins environment) and set the link detection timeout (which is appropriate for your network environment).
7) Click Test Connection to see if it is accessible. If it fails, check steps 3, 5, and 6 above.
4. Configure other plug-ins
1) Enter global tool configuration
2) Configure jdk, maven, Git plugins
5. Configure Global Environment
1) Click to enter system settings
2) Add global variables
Project Deployment
1. New Project
Project created successfully:
2. Configuration Items
1) Gitee Connection checks the gitee api token built above
2) Configure git remote warehouse
3) Add Credentials (because the warehouse is a code cloud, where the user name and password of the login code cloud are configured)
Remote warehouse is configured and refreshed automatically (no errors will occur after configuration, if any, refer to the problem encountered at the end of the article)
4. Script to set project timer build time (set trigger)
5. Write commands to build projects
Write project build commands:
#echo "Output PATH Variable" #echo $PATH # repackaging echo "Start repackaging" mvn clean install echo "Packed" whoami echo "Ready to restart" # Find the previous project jar package process ID and kill pid=`ps -ef | grep myblog.jar | grep -v grep | awk '{print $2}'` if [ -n "$pid" ] then kill -9 $pid fi echo "Stop running" #Move to tomcat webapps directory cp target/myblog.jar /www/wwwroot/myblog # Run the project jar package in the background nohup java -jar /www/wwwroot/myblog/myblog.jar --server.port=443 >myblog.log 2>&1 &
The project is configured.
3. Building Projects
Once the project is configured, start building the project
1)Build Now
2) Enter terminal to view output
4. Successful project construction
Build successfully, because it is a blog project, you can visit the following: https://caifl.top
If you encounter problems, refer to the encounter problems below!
Problem encountered:
1,cp: cannot create regular file '/usr/local/tomcat/tomcat-9.0.41/webapps/myblog.war': Permission denied
Description of the problem:
cp: cannot create regular file '/usr/local/tomcat/tomcat-9.0.41/webapps/myblog.war': Permission denied
# Attempt to authorize with chmod command chmod 777 /usr/local/tomcat
If not, modify the Jenkins user
vim /etc/sysconfig/jenkins
2,Non-resolvable parent POM for com.cfl:MyBlog:1.0.0: Could not transfer artifact org.springframework.
Description of the problem:
Non-resolvable parent POM for com.cfl:MyBlog:1.0.0: Could not transfer artifact org.springframework.
Reasons for the problem:
The pom file has two dependencies on the org.springframework.boot package
Solve:
Remove the duplicate dependency
3. The tomcat startup flashes by
Description of the problem:
Flash after tomcat starts, stop running directly
View logs in the logs directory:
Error: Could not find or load main class org.apache.catalina.startup.Bootstrap Error: Could not find or load main class org.apache.catalina.startup.Bootstrap Error: Could not find or load main class org.apache.catalina.startup.Bootstrap Error: Could not find or load main class org.apache.catalina.startup.Bootstrap Error: Could not find or load main class org.apache.catalina.startup.Bootstrap Error: Could not find or load main class org.apache.catalina.startup.Bootstrap Error: Could not find or load main class org.apache.catalina.startup.Bootstrap
Cause of problem: Unknown cause, tomcat package may be src version instead of bin version, or other environmental problem
Solution: Redownload the tomcat-9.0.48-bin.tar.gz installation package
4,Jenkins - mvn: command not found
Problem Description: Jenkins - mvn: command not found. When Jenkins deploys a build project, console output shows that the MVN command cannot be found
Reasons for the problem:
Linux environment variables for java or maven paths are placed in / etc/profile, and / etc/profile is loaded only when the user logs in. jenkins runs the command in a Non-login way, which is not loaded when the command is run, so jenkins can only look for executable files in the current path.
Solution:
In jenkins settings, you can set global variables. In the jenkins home page - >Manage jenkins->Configure System->Global Properties, check the Environment variables check box, and a List of variables will come out and fill in the following:
- name: JAVA_HOME value:XXX
- name: M2_HOME value:/usr/cyz/apache-maven-3.6.1
- name: PATH+EXTRA value: $M2_HOME/bin
Notice the last red PATH+EXTRA, which means PATH=EXTRA:$PATH, which extends the current PATH variable.
build succeeded after setup.