This article aims to implement semi-automatic project deployment based on Jenkins + Docker without further ado.
Required environment
- A virtual machine, the company VIRTUAL machine I use, in a virtual machine environment
Jenkins
Can always up, not every time to use the time up, if not, local can also be implemented, need to boot when the bootJenkins
. - The virtual machine is
Java
Environment (also required if localJava
Environment), here firstDemo implementation in virtual machine
. - The source repository is
Yards cloud
A, install,
Run the following command in Linux:
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'
sudo apt-get update
sudo apt-get install jenkins
Start after successful installation:
systemctl start jenkins
Modify theJenkins
Configuration file:
vi /etc/sysconfig/jenkins
To viewJenkins
Check whether the startup is successful:
systemctl status jenkins
Status displayed after successful startup:
Startup failure:
If the startup fails, the following error message is displayed: Job for jenkins.service failed because the control process exited with error code. See “systemctl status jenkins.service” and “journalctl -xe” for details.
Run the systemctl status jenkins.service command as prompted to check the Jenkins service status:
Cause analysis: Note that /usr/bin/java is the default Java path in the Jenkins configuration file, but the Java path installed on the host is not this path, so we need to replace the Jenkins configuration path:
which java
To viewJava
Location and copy:
which java
Copy the code
- Modify the
Jenkins
In the configuration fileJava
Path:
vi /etc/init.d/jenkins
Copy the code
findJenkins
In theJava
Set the path to that of the current host displayed in Step 1Java
Path:
- According to the
i
Key to edit, and then save to exit editing, and then restartJenkins
Can.
Ii. Jenkins configuration
After the above startup is successful, a random password will be generated. The browser will visit localhost:8080, visit Jenkins page, and copy the generated password to enter.
Home page select load plug-in and download.
Configuration items
1. InstallGitee
Plugins, select Jenkins’ root directory:
Manage Jenkins => Manage Plugins go to the plugin download pageCopy the code
2. The queryGitee
Plug-in and download:
Configuration of 3.Gitee
Information:
Manage Jenkins => Manage Plugins => Configure System => Gitee configurationCopy the code
- Link name: enter the name you want, this name does not matter, just for display
Gitee
The domain nameURL
: Enter the complete URL address of the code cloud,https://gitee.com (the domain name that the code cloud privatized customer enters to deploy), generally fill in https://gitee.com
- Certificate token: Click on the right option
add
, the choice ofjenkins
. Domain
: Selects global credentials(Global credentials)
- type
(Kind)
: chooseGitee API
The token(Gitee API Token)
- The scope of
(Scope)
: Depending on where you personally want this credential to be available, I chose global Gitee APIV5
Private token(Gitee API Token)
: Enter your code cloud private token to get the addresshttps://gitee.com/profile/personal_access_tokens
ID
And description: Fill in whatever you want- When you’re done, select the token you just added yourself
Advanced
: Can be configured whether to ignoreSSL
Wrong (for youJenkins
Environment support), and can set the link test timeout (depending on your network environment), I was ignored.- Test connection: click to confirm whether your configuration is correct, if not, reconfigure.
4. Create a new project and choose free style:
5. Configure General:
6. Source management optionsgit
:
If the following problem occurs: Because there is nojenkins
The public key of the server is configured on the code cloud
Solution:
Ssh-keygen -t rsa // Generate a public key cat /root/.ssh/id_rsa.pub Viewing a public key The following figure is a screenshot of the public key document obtained from the code cloud. For details, see: https://gitee.com/help/articles/4181#article-header0Copy the code
Configuration on the code cloud:
No error is reported after the public key is configured.
7. Build triggers:
Check the Gitee Webhook to trigger build push code, comment on submission information, and create Pull Requests [Ci-skip] directives to skip the build PR does not require filtering when testing build filters The Commit version that has been built allows all branches to trigger build generated Gitee WebHook Password configuration to the code cloud WebHookCopy the code
Jenkins
Configuration:
Yards cloudwebhook
Configuration:
8. Build the environment:
Select Provide Node & NPM bin/ Folder to PATHCopy the code
If you don’t have a Node environment, you need to configure the Node installation:
Manage Jenkins => Global Tool Configuration => NodeJS
Copy the code
9. Construction:
The last construction step: select Execute Shell to add the construction script. Here, take Docker packaging as an example to generate the docker image sourceCopy the code
Shell script, the following command is ok in the VIRTUAL machine environment, if the Windows local command, also need other, do not repeat here:
Docker build-t [project name]: 2020-01-21-10-15-11.2 [project name]: 2020-01-21-10-15-11-T tag [domain name] / [space] / [project name]:Copy the code