Note content: Publishing PHP code via Jenkins
Note Date: 2018-02-26
26.4/26.5 Publishing PHP code via Jenkins
We all know that Jenkins is a tool for continuous integration, so this article briefly describes how to publish the PHP code through Jenkins to the remote machine, the experimental environment is as follows:
Two machines, one installed Jenkins as a Jenkins server, and one as a remote server:
- 192.168.77.130 Jenkins Server
- 192.168.77.128 Remote Server
- A remote public git repository (PHP code in this repository)
If you have not installed Jenkins, you can refer to my other article about installing Jenkins.
Before we release the PHP code through Jenkins, we first need to check that the "Git plugin" and "Publish over SSH" Two plugins are installed on the Jenkins Service's Web page:
System management → manage plugins → installed, and then search for git plugin to see if it's installed, typically this plugin is installed by default:
Then search publish over SSH, and you can see that if no installation is found:
Then install the Publish over SSH plugin in the optional plugins:
Installation Complete:
Restart the Jenkins service:
Systemctl Restart Jenkins.service
Make sure two plugins are installed, click "System Management" → "System settings", drop-down page, probably at the bottom of the page can find "Publish over SSH":
Then, on the server, Sir, a couple of keys:
[[email protected] ~]# ssh-keygen -f /root/.ssh/jenkinsGenerating public/private rsa key pair.Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/jenkins.Your public key has been saved in /root/.ssh/jenkins.pub.The key fingerprint is:75:f0:ac:f1:56:6d:f4:a8:9c:d5:57:9b:e8:e3:5f:83 [email protected]The key‘s randomart image is:+--[ RSA 2048]----+| . o|| + .==|| o +.+o*|| . *.= ..|| S . *o || .. o || E ..|| . o|| . |+-----------------+[[email protected] ~]# ls .ssh/id_rsa jenkins jenkins.pubauthorized_keys id_rsa.pub
After generating the key pair, first copy the public key to the remote machine, that is, the contents of the public key is pasted into the .ssh/authorized_keys
file, and we previously configured the remote login key verification is the same.
After copying the public key, let's manually test the ability to log on to the remote machine via SSH using the following command:
Ssh-i. Ssh/jenkins 192.168.77.128
The normal login is not a problem, if you have access to the user password situation is the key is not configured successfully, the configuration is successful in the case of login is not required password.
After completing the test to ensure that the remote machine is logged in via SSH, the contents of the private key are then pasted into the "Key" column:
Then complete the remote machine configuration, click "Apply" and if you do not need to continue to add the remote machine, click "Save":
Note: The Jenkins SSH Key column defaults to the Jenkins Admin admin account password, which can be set to a blank password without being filled in.
Now that we've configured the plug-in and the remote machine, we're ready to start creating new tasks:
Configuration Item Name and Description:
To configure the address of the remote warehouse:
The build triggers, and the build environment, do not need to be configured because we are releasing PHP code:
Finally, the "build", the core step, select "Send files or execute commands over SSH":
Brief description:
- SSH server,name Select the corresponding server, transfers, Source files filled out
**/**
, representing all files
- Remove prefix can specify the truncated prefix directory, where it is left blank, remote directory specifies the path to the code on the server, such as/data/wwwroot/www.aaa.com,exec command to execute after the file transfer is completed, such as can be a command to change the file permissions, set the completion of the click on "Add Transfer set", if there is another machine, you can click "Add Server" to repeat the above operation
Once you have done this, you can use the task you just created to work, and click "Build Now" on the left to publish the code on the GIT to the specified server:
In construction:
Build Complete:
Click View Output log:
The output logs are as follows:
To the remote machine to see if the Jenkins_test directory already exists:
[[email protected] ~]# ll /tmp/jenkins_test/总用量 108drwxr-xr-x 2 nobody nobody 64 2月 26 15:04 D11Zdrwxr-xr-x 2 nobody nobody 4096 2月 26 15:04 D12Zdrwxr-xr-x 2 nobody nobody 23 2月 26 15:04 D13Zdrwxr-xr-x 2 nobody nobody 4096 2月 26 15:04 D14Zdrwxr-xr-x 2 nobody nobody 4096 2月 26 15:04 D15Zdrwxr-xr-x 2 nobody nobody 24 2月 26 15:04 D17Zdrwxr-xr-x 2 nobody nobody 73 2月 26 15:04 D18Zdrwxr-xr-x 2 nobody nobody 148 2月 26 15:04 D19Zdrwxr-xr-x 2 nobody nobody 80 2月 26 15:04 D20Zdrwxr-xr-x 2 nobody nobody 4096 2月 26 15:04 D21Zdrwxr-xr-x 2 nobody nobody 122 2月 26 15:04 D22Z-rw-r--r-- 1 nobody nobody 18044 2月 26 15:04 LICENSE-rw-r--r-- 1 nobody nobody 419 2月
As above, we can see that the files are synchronized successfully, and the genus of the files is also the nobody of our set. After that, as long as the contents of the source files in the remote repository change and need to be synced to the line, you just need to click on "Build Now" for that task.
Publish PHP code via Jenkins