Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Time:2021-1-26

Automated Deployment (CI + CD) is a trend of software development and a good help to Devops. Today’s practice is to realize the automatic deployment process of PHP code based on docker + Jenkins + LNMP + code cloud.

Environment configuration

Operating system: CentOS Linux release 7.6.1810
Docker version: 19.03.5
Jenkins version: 2.204.1
Nginx version: 1.15.5
PHP version: 7.2.26
MySQL version: 8.0.18
Redis version: 5.0.5  
Code cloud: code warehouse

Docker installation

Please refer to this article: installing docker for CentOS

Docker installing LNMP environment

Please refer to this article: Customizing LNMP environment by dockerfile

Docker installing Jenkins

Please refer to this article: docker builds Jenkins to realize automatic deployment

Code cloud warehouse preparation

Create project warehouse

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Jenkins, get ready

Install SSH plug-in

Because of the need to execute some shell commands of the server to build a project, SSH login is used to communicate with the remote server.
1. System Management > plug in management
2. Search: SSH, install it

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

New project tasks

The task name cannot be in Chinese. Select the first one: freestyle project custom project

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Project task allocation

1. Source code management: Git
2. Repository URL: warehouse address (mine is codecloud warehouse)
3. Credentials select Add
4. Add credentials
5. Configure the warehouse private key (please refer to the warehouse key management help of code cloud for details)

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

After the credential configuration is successful, you can select the user name of the previous credential configuration. If you successfully connect to the warehouse address, no error will be reported.

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

GitHub hook trigger for gitscm polling

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Build environment: use secret text (s) or file (s) // transfer files in private
Credentials: select the specified credentials GIT

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Configure SSH

1. System Management > system configuration
2. Configure remote server SSH information

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Configure build shell scripts

Build: execute shell script on remote host Using SSH // execute remote server shell script during build
1. Fill in SSH site // remote server SSH login
2. Command // shell script
remarks:
Because the code is automatically deployed in the workspace directory of Jenkins, and the site directory of the project is under / data / www, the updated code file needs to be copied to the project site during the construction (there are many ways to achieve this, and the code copy is used here, because the size of the code will not take up a lot of space. Now many static resources are placed in some cloud services (on business)
Soft connection was used in the beginning, but because nginx does not support soft connection access.

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

After configuration, click save

Code cloud code push

The code cloud warehouse modifies the code, and then submits the PR to the master branch

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Deployment task manual build

Click build manually to realize automatic code deployment
remarks:
It can also be configured to build git push automatically (this is recommended for test environment)
Manually click build for better management (recommended for production environment)

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

If the task fails to build, there will be an error message and a red flag // which is easy to manage

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code

Visit the website

After the automatic deployment task is built successfully, visit our project to see the modification effect

Docker + Jenkins + code cloud warehouse for automatic deployment of PHP code