Teaching Warehouse: github.com/jinxuanzhen… “, you can clone it and try it out with the tutorial
CI Process building teaching trilogy:
- Implementing cloud builds
- How to Access CI (Jenkins Version)
- Build a version release management background + enterprise wechat robot crowd notification
- Only a fixed number of machines can produce version, change the machine always worry about some problems (heart cost)
- Multiple applets, switching warehouses, and even using developer tools to open different projects (physical cost)
- Frequent version issuance + trivial version issuance process + multiple small programs may exist, and half a day is consumed by mechanical labor (time cost)
This article is mainly to share my side of a set of solutions, to achieve one-click release, unified management, before reading the best to see the last article, or there may be some obstacles in the process of reading
About Jenkins: www.jenkins.io/zh/
case
- Pull the code
- Ask for the current version number that you want to change
- Build (gulp, Webpack, NPM)
- Stores the current version image to a static resource server
- The version information is inserted into the database
- The changed version files are committed to the Git repository
The red items are required, and the black items can be added according to the situation. If you want, you can also add any link you want. The following is the interface of our version release management background, and the version information stored in mysql looks like this:
Let ‘go
First you need a cloud server to set up the Jenkins environment
Install the Java
Jenkins relies on the Java environment, so install Java first
Yum install - y Java - 1.8.0 comes with - its. X86_64Copy the code
Jenkins installation package
The official tutorial: www.jenkins.io/zh/doc/book… Warehouse address: pkg.jenkins-ci.org/redhat-stab…
sudo wget -O /etc/yum.repos.d/jenkins.repo https://pkg.jenkins.io/redhat-stable/jenkins.repo
sudo rpm --import https://pkg.jenkins.io/redhat-stable/jenkins.io.key
yum install jenkins
# run
service jenkins start
Copy the code
Port number of the access server 8080
service jenkins restart
Create pipeline
Start by creating a task, select Pipeline (if you are interested in other items) and click OK
pipeline script
Official line grammar tutorial: www.jenkins.io/zh/doc/book… , which is more detailed, you can first run according to the following code, encountered problems and then check the tutorial
Pipeline structure
Before writing the pipeline, think about what we need to do throughout the release process. Taking the current example, it can be roughly divided into five links:
- Pull the code
- Ask for the version number you want to change
- Install the NPM package
- Perform publish
- Submit the modified version number information to the repository
Each link is a stage. First list the stages, and then we will add them one by one
pipeline {
agent any
stages {
// Pull git code
stage('git pull') {
steps {
}
}
// Query the current version information
stage('inquirer version') {
steps {
}
}
/ / build
stage('build') {
steps {
}
}
// Push version information to git repository
stage('push version2git') {
steps {
}
}
}
}
Copy the code
Pipelined syntax
The syntax of many plug-ins is still relatively complex and trivial, here provides a function to automatically generate pipieline syntax
Pull git code
According to the above, we need to generate pipeline script, but in order to establish SSH link between repository and Jenkins, we need to configure the public key of git repository and Jenkins private key credentials
Configure the public key of the Git repository
Jenkins pulled the Git repository code and needed the SSH secret key for convenience
# generate key (no press enter required)
ssh-keygen -t rsa -C "[email protected]"
# View the generated SSH file
cd ~/.ssh && ls Id_rsa id_rsa.pub: a private key and a public key
Copy the code
Click your profile picture, click Setting to find SSH, paste the content in ID_RSA. pub into the input box, and click Add to finish adding
Configure the Jenkins private key
In addition to configuring the public key on Git, you also need to configure the lower credentials on Jenkins, click Add
id_rsa
pipeline code
// Pull git code
stage('git pull') {
steps {
git branch: 'test'.credentialsId: '1'.url: '[email protected]:jinxuanzheng01/blog-xcx-ci-demo.git'}}Copy the code
After adding it to the specified location, click Save and try to build again to see if it is successful. Click #5 in the lower left corner to view the current build process
Query the current version information
Jenkins does not support interactive command, so it needs to be implemented in another way. After checking the document, Jenkins has API support, and the method is called input. However, before calling Jenkins, it is necessary to obtain the previous version information, that is, read the file version.config.json, the overall code is as follows:
// Query the current version information
stage('inquirer version') {
steps {
script {
// Read version information
def versionJson = readJSON file: './version.config.json'.text: ' '
// Set the problem description
def userInput = input(
id: 'versionInput'. message: 'Please set version information'. parameters:[[defaultValue: versionJson.version, description: 'Set version number'.name: 'VERSION'.$class: 'TextParameterDefinition'],
[defaultValue: 'jenkins CI is upload trial version as: ' + new Date().format('yyyy-MM-dd HH:mm:ss'), description: Set version description (please use English).name: 'VERSIONDESC'.$class: 'TextParameterDefinition']])// Set global variables
env.VERSION = userInput.VERSION;
env.VERSIONDESC = userInput.VERSIONDESC;
// Rewrite the local version file (in preparation for later version commits)
writeJSON file: './version.config.json'.json: [version: env.VERSION, versionDesc: env.VERSIONDESC], pretty: 4; }}}Copy the code
After the mouse clicks, the modal box pops up. You can see that there is information extracted from the last version in the local file. You can manually modify it
Execute a build
Git Clone: Git Clone: Git Clone: Git Clone: Git Clone: Git Clone: Git Clone: Git Clone: Git Clone
/ / build
stage('build') {
steps {
sh "npm install"
sh "npm run build"}}Copy the code
NPM not found NPM not found
Install the NodeJS environment
The runtime environment here is nothing to do with the native machine, like a sandbox, so to install the Node environment, you need to install the Config File Provider Plugin and NodeJS Plugin
Configuring global Tools
After the installation is complete, add the global tool, go to “System Administration -> Global Tool Configuration -> NodeJS”, click NodeJS to install the default items, click Save, restart the Jenkins service, return to the server, and enter service Jenkins restart
pipeline code
/ / build
stage('build') {
steps {
nodejs('NodeJS 14.3.0') {
sh "npm install"
sh "npm run build"}}}Copy the code
Uploaded successfully
At this time, if there is no problem, the code should have been released to the experience version. If the IP error is reported here, remember to add the IP whitelist uploaded to the wechat public background
Submit the new version number information to the repository
pipeline code
Basically, you write the commands you entered manually into the script
// Push version information to git repository
stage('push version2git') {
steps {
sh "git config --local user.name ${GIT_USER_NAME} && git config --local user.email ${GIT_USER_EMAIL}"
sh "git add version.config.json"
sh "Git commit -m 'docs: change VERSION to ${VERSION}'"
sh "git push origin ${BRANCH_NAME}"}}Copy the code
As for the “${}” in the code above, which is actually the default environment variable, we can put some fixed things out to avoid hard coding, such as:
pipeline {
agent any
// Environment variables
environment {
GIT_USER_NAME = 'jenkinsCI'
GIT_USER_EMAIL = '[email protected]'
GIT_ADDRESS = '[email protected]:jinxuanzheng01/blog-xcx-ci-demo.git'
BRANCH_NAME = 'master'}}Copy the code
Change Jenkins account permissions
When you do a push, Git will report that you have insufficient permissions. This is not because of a problem with the SSH key, but because Jenkins did the push using a Jenkins account instead of the root permission on the Linux machine
Open the configuration file
vim /etc/sysconfig/jenkins
# change jenkins_user to root, default to Jenkins
$JENKINS_USER="root"
# Change the related folder permission group
chown -R root:root /var/lib/jenkins
chown -R root:root /var/cache/jenkins
chown -R root:root /var/log/jenkins
# restart Jenkins
service jenkins restart
Copy the code
Run the pipeline
Rebuild, Jenkins reported no error, check git repository, execute successfully
Running success
At this point, the entire pipeline has worked, and you can easily try one-click publishing
Jenkins pipeline code has been put in the warehouse at github.com/jinxuanzhen…
For additional
Rights management
Jenkins has a lot of plugins, such as permission management, which is a relatively new function, you can Google it is not difficult
Sending an HTTP request
The HTTP Request Plugin can be used to Request the following services: plugins.jenkins. IO /http_reques… There are some demos in there
NPM switches private repositories
Method 1: NPM can directly use nPM_token to log in Method 2: project Settings. NPMRC, fill in the private warehouse + user information
Jenkins has insufficient access to docker
chmod 777 /var/run/docker.sock
Manually install Jenkins plugins
Using Jenkins plugins to manage the installation of plugins is slow and sometimes fails. Here is a manual installation method for Jenkins plugins library: updates.jenkins-ci.org/download/pl… Search for the required plugin download (usually in *.hpi format), open Jenkins “System Management -> Plugin Management -> Advanced”, select the downloaded file to upload