Introduction
The hardware and software information used in this article and the project brief are as follows:
1. System: CentOs
2. OpenJDK version: 11
3. Jenkins version: 2.375.2
4. Project architecture: Egg + Vue2 + Typescript
One more word for students who are not familiar with Linux, when editing a file on the command line, press the "i" key to edit, after the modification, ESC exits editing, ":wq" saves the content and exits, ":q" exits without saving
Install
JDK installation
If you have installed other lower versions before, you need to uninstall them first
uninstall
Check if OpenJDK is installed
rpm -qa | grep java
rpm -qa | grep jdk
Bulk uninstall command
rpm -qa | grep jdk | xargs rpm -e --nodeps
rpm -qa | grep java | xargs rpm -e --nodeps
Install
There are two ways to install
Install via yum
yum install -y java-11-openjdk java-11-openjdk-devel
After the installation is complete, execute the following command to view the installation directory
ls -l $(which java)
/usr/lib/jvm/java-11-openjdk-11.0.18.0.10-1.el7_9.x86_64/bin/java
It is the installation path of my Java
Unzip and install after downloading
This method requires us to download OpenJDK first, the version used here isopenjdk-11+28_linux-x64_bin.tar.gz
After downloading, log in to the cloud server, create a directory /usr/java
, and upload the compressed package just downloaded to the directory
mkdir /usr/java
Enter the directory and unzip the file
cd /usr/java/
tar -zxvf openjdk-11+28_linux-x64_bin.tar.gz
At this time, the installation path becomes/usr/java/jdk-11
Environment variable configuration
Let's start configuring environment variables
vim /etc/profile
Configure JAVA_HOME as the Java installation path (the installation path in the previous step)
After saving, execute the following command to make the environment variable take effect
source /etc/profile
Jenkins installation
Similarly, if you have installed and run Jenkins before, you need to uninstall it first
uninstall
# 停止已启动的 Jenkins 服务
systemctl stop jenkins.service
# 卸载 Jenkins 包
rpm -e jenkins
# 查看是否还有jenkins依赖,有就删除
rpm -qa | grep jenkins
# 以下目录/文件是我本机之前安装的Jenkins(不同版本、系统可能有差异)
rm -rf /etc/sysconfig/jenkins.rpmsave
rm -rf /var/cache/jenkins/
rm -rf /var/lib/jenkins/
rm -rf /var/log/jenkins
rm -rf /usr/lib/jenkins
Install
There are many ways, here is the rpm installation method
1. First download the rpm package of Jenkins to the local, it is recommended to download the Jenkins package from Tsinghua ’s mirror site (here is the version I downloaded jenkins-2.375.2-1.1.noarch.rpm
)
After the installation is complete, the default account name of Jenkins is jenkins (not a login account), and the default port is: 8080
Modify the port and account name (not necessary)
If you need to modify, execute the following command to modify the configuration file
vim /etc/sysconfig/jenkins
As shown in the figure below, JENKINS_USER is the account name, and JENKINS_PORT is the port number
After the modification is completed, execute daemon-reload
the command to reload to make the configuration take effect
systemctl daemon-reload
Then, modify the directory permissions to prevent accidents
chmod -R 777 /var/lib/jenkins
chmod -R 777 /var/cache/jenkins
chmod -R 777 /var/log/jenkins
Remember to let go of the cloud server firewall's restrictions on the Jenkins port
If, after starting the service, it is found that the modified port configuration does not take effect, you need to /usr/lib/systemd/system/jenkins.service
modify
vim /usr/lib/systemd/system/jenkins.service
find Environment="JENKINS_PORT=8080"
modify
After the modification is completed, execute the same daemon-reload
command to reload to make the configuration take effect, and then restart Jenkins
service jenkins restart
Modify the Jenkins update configuration
1. Network configuration When installing and updating jenkins, the network connection will be checked by default. The default checkulr
is that http://www.google.com/
cannot be accessed in China. We can change the url to an address that can be accessed in China. For example, http://www.baidu.com
modify jenkins configuration file: .jenkins/updates/default.json````vim .jenkins/updates/default.json ```2.更新配置默认是国外的源,国内下载可能失败,建议更换```vim /var/jenkins_home/hudson.model.UpdateCenter.xml ``````bash-4.4# cat /var/jenkins_home/hudson.model.UpdateCenter.xml<?xml version='1.1' encoding='UTF-8'?><sites><site><id>default</id><url>https://updates.jenkins.io/update-center.json</url></site> ```将 url 更换为
https://mirrors.tuna. tsinghua.edu.cn/jenkins/updates/update-center.json`Jenkins initialization configuration
Visit the Jenkins address, you can see the following screen
According to the prompt, go to /var/lib/jenkins/secrets
the directory to view the password
cat /var/lib/jenkins/secrets/initialAdminPassword
Register your own administrator account (forgot to take a screenshot)
Here I directly choose to install the recommended plug-in (I forgot to take a screenshot again)
SSH Keys Configuration
In order to facilitate Jenkins to pull code from github and avoid using passwords, ssh is used here
generate certificate
1. Enter the following command, the double quotes are your email address to log in to github ssh-keygen -t rsa -C “[email protected]”
2. There will be an operation to enter the password twice, just press Enter to skip. The SSH certificate is generated successfully, and the generated secret key is stored in /root/.ssh
the path * Public key file: id_rsa.pub* Private key file: id_rsa
Configure the public key to Github
Copy the content id_rsa.pub
in and configure the content of the public key to GitHub
Settings > SSH and GPG keys
Click in the upper right corner New SSH key
, name whatever you like, and paste the content id_rsa.pub
in
Configure the private key to Jenkins
1. Configure credentials (Manage Jenkins > Manage credentials)
2. Click Global, create a new credential under the global domain
3. Create a new credential
Here we have configured the credentials
Bypass the security check of the new version of Jenkins
When configuring the build task, Jenkins will verify the github address, here is to prevent verification failure
The address and credentials are correct. The new version has strengthened the security check, so it is simply not checked.
1. On the configuration page, find the global security configuration
The configuration is basically complete, and the next step is to create tasks
Pipeline
Here I am using Pipeline, which can visually see the construction situation
Start the configuration task after creation. Let’s talk about the configuration of Pipeline here. It allows you to write pipeline statements directly in the configuration, or you can choose to read the statements in the file. What I choose here is to read the files in the Git project.
Fill in the SSH path of your Git project in Repositories, and select the global credentials we created in the previous step for the credentials; if the security check of Jenkins is not bypassed, an error will be reported after selecting the credentials here
You can modify the read branch (default master) and the path where the pipeline script file is located (the default is the Jenkinsfile placed in the root directory)
WebHook
This configuration is the key to realizing the automation from Github to Jenkins. The effect achieved is that after the Git project completes a Push operation, the corresponding task in Jenkins starts to build
plugin installation
In the configuration select plugin management
Search directly among available plugins Generic Webhook Trigger
and install plugins
task configuration
Go back to the task configuration, select the build trigger hereGeneric Webhook Trigger
http://JENKINS_URL/generic-webhook-trigger/invoke
This is the address that triggers the build, JENKINS_URL needs to be replaced with the address where your Jenkins service is running
This plug-in also has optional configurations such as tokens. If you are interested, you can explore by yourself, so I won’t go deep here.
Git configuration
Enter the GitHub project and find Webhooks in the settings
Fill in the trigger build address in the Payload URL, http://JENKINS_URL/generic-webhook-trigger/invoke
note that JENKINS_URL here needs to be replaced with the address where your Jenkins service is running
(If the Git verification fails after filling in, try to add the token configuration to the task configuration, and add the token parameter to the invoke url)
Node environment
You are not mistaken, it is to create a node environment under Jenkins, because my pipeline script will directly run npm, simply put it directly in Jenkins, save the installation on the cloud server
plugin installation
Done with plugins
Search directly among available plugins NodeJS Plugin
and install plugins
I have already installed it here, so I won’t take a screenshot
Node configuration
Found in System Administration全局工作配置
In the NodeJS column, click Add, select the node version you need and fill in the alias
If you need multiple versions, continue to click Add. Later, in the use of Pipeline, it is an alias to be used directly, so it is best to use a name to see the name of the version at a glance.
Pipeline script file
Create a Jenkinsfile file directly in the project root directory
Jenkinsfile syntax (Declarative)
First, let’s have a brief understanding of this grammar. If you are not interested, you can skip it, and it will not affect the use.
Jenkinsfile supports two syntax forms:
- Declarative pipeline – Introduced after pipeline v2.5, the structured way is relatively simple and easy to use. This is similar to the keyword-driven mode we came into contact with when doing automated testing, as long as we understand the defined keywords and fill in the data as required. Getting started is easy, but lacks flexibility.
- Scripted pipeline – Based on grjoovy syntax, compared with Declarative, it has higher scalability and better packaging, but it is somewhat difficult and requires certain programming tools
I am using Declarative here, so I mainly introduce Declarative syntax
- It must be included in the fixed-format Pipeline{} block, and each declaration statement must be on a separate line without a semicolon at the end of the line.
- Blocks {} can only contain Sections, Directives, Steps or assignment statements
agent: node
Must exist, agent must be defined at the top level inside the pipeline block
- any : the pipeline can be executed on any agent
- none : the pipeline will not allocate a global agent, and each stage will allocate its own agent
- label : Specify the label of the running node
- node: custom running node configuration, specify label, specify customWorkspace (workspace)
- docker: control the docker running related content on the target node
stages: set of stages
Must exist, including one or more stage commands that are executed sequentially. The name of the stage that needs to be defined can only be used once in the pipeline
steps: steps
must exist
steps are located inside the stage instruction block and contain one or more steps
stages {stage("stage name"){steps{echo "this is a step"}}
}
post: post-build operations
not necessary
- always: runs regardless of the completion status of the pipeline run
- success: run only if the current pipeline has a status of success
- failure: run only if the current pipeline has a failed status
Of course, there are other options, so I won’t make an extended introduction here;
parameters: parameters
not necessary
Parameter settings for parametric construction, parameter types include booleanParam, choice (choice), file, text, string, etc.
parameters{
string( name :'name',defaultValue:'beauty',description:'姓名是')
}
triggers: trigger
not necessary
Defines how the pipeline automation is triggered
- cron: accepts a cron-style string to define the regular interval for pipeline triggering
- pollSCM: Accepts a cron-style string to define the regular interval at which jenkins checks for changes to the SCM source; the pipeline will be retriggered if there are new changes
triggers{cron('_/1 _ \* \* \*')
}
Execute every minute
triggers{pollSCM('_/1 _ \* \* \*')
}
Monitor SCM feed every minute, trigger on change
write
I split the build deployment into 4 phases
pull code
Here you can directly use the git command
git branch: 'master', credentialsId: 'xxx', url: '[email protected]:LuciferHuang/xxx.git'
credentialsId is the id automatically generated by the global credentials we set earlier, as shown in the figure below
project build
The main thing to do is to build the egg project
Install dependencies, execute npm run build
, and delete dependencies after the build is complete
docker build
In my egg project, docker is directly used to take over the project to run
Therefore, this step is to execute the docker command to generate the image
docker run
There are two steps here
1. Stop the previously running docker container first (to prevent port occupation)
2. Use the run command to create and run the docker container
full script
pipeline{agent anytools {nodejs 'NodeJS_14.17.6'}stages{stage("pull"){steps {sh 'sudo rm -rf public'sh 'sudo rm -rf logs'git branch: 'master', credentialsId: 'xxx', url: '[email protected]:LuciferHuang/xxx.git'}}stage("project build"){steps {sh 'npm install'sh 'npm run build'sh 'sudo rm -rf node_modules'}}stage("docker build"){steps {sh 'docker build -t xxx:v${BUILD_NUMBER} . '}}stage("docker run"){steps {sh 'docker stop $(docker ps -a -q)'sh 'docker run -p 80:80 --name xxx_v${BUILD_NUMBER} -d xxx:v${BUILD_NUMBER}'}}}
}
The nodejs used in tools is the NodeJS alias configured in the previous steps
BUILD_NUMBER is the build version number of the current task, the script is for reference only, modify it as needed
epilogue
It has been successfully built here, modify the code and push it up, and you can see the effect
Occasionally, the automatic build cannot be triggered. You can go to the WebHooks of the Github project to view the latest triggers
at last
Recently, I also sorted out a JavaScript and ES note, a total of 25 important knowledge points, and explained and analyzed each knowledge point. It can help you quickly master the relevant knowledge of JavaScript and ES, and improve work efficiency.
cript and ES related knowledge to improve work efficiency.
Friends in need, you can click the card below to receive and share for free