This article mainly introduces the use of PHP + laravel How to implement the deployment automation of the relevant information, the text through the sample code introduced very detailed, to everyone's study or work has a certain reference learning value, the need for friends can refer to, let's look at the following together.
Objective
This article mainly introduces the use of PHP + laravel How to achieve deployment automation related content, share out for everyone to reference the study, the following words do not say, come together to see the detailed introduction bar.
The so-called automated deployment, my understanding is that the user to ensure the quality of the code, the code can be quickly automatically deployed to the target server a means.
Implementation principle
Local push code, code base, Webhook notification service-side auto-replace code library
Build and deploy the public key
Specific steps refer to configuring the SSH public key
1) Generate public key
# Generate public/private RSA key using a given email # If you need to configure with a nondefault address. ssh/config$ ssh-keygen-t rsa-b 4096-c your_email@example.com
2) Add the public key in the coding
Output Deployment Public Yue
$ cat Coding.pub
Deploying the public key on the GIT management side
3) Configure config file
Edit ~/.ssh/config File
Host git.coding.netUser xxxx@email.compreferredauthentications publickeyidentityfile ~/.ssh/coding_rsa// The public key storage point of the generated non-default address
4) test if you can link to the git@git.coding.net server
# Note that Git.coding.net is connected to a CDN so it resolves multiple different host IPs $ ssh-t git@git.coding.netthe authenticity of host ' git.coding.net (123.5 9.85.184) ' can ' t be established. RSA key fingerprint is 98:ab:2b:30:60:00:82:86:bb:85:db:87:22:c4:4f:b1. Is you sure want to continue connecting (yes/no)? # Here we are prompted to enter yeswarning:permanently added ' git.coding.net,123.59.85.184 ' (RSA) to the list of known hosts. Coding tip: Hello Duoli, you ' ve connected to Coding.net via SSH. This is a deploy Key.duoli, hello, you have passed the SSH Protocol authentication Coding.net Service, which is a deployment public key
Set Webhook
Notifies the server to receive code updates when the code library receives a notification.
This webhook way to receive a request that can be deployed, where the request is using the Post method
PHP Receive Deployment
Because PHP scripting code may have a service interruption (such as execution time) when it executes, it is not necessarily realistic, so it is intended to be called using a script.
Script listening processing queue-----queued to receive requests
Due to the use of the Laravel framework, after receiving notifications, the queue is queued because the queue uses command line snooping, so the command line executes without interruption.
Before this, you need to configure the user who runs the code to have permission to access the server to Git. That is, if your code runs in Www-data, you need to use Www-data's role to access the Git@git.coding.net server. Otherwise, deployment is not possible because the key is not compliant and does not have permission to get content.
1) Queue Code Setup App/jobs
<?php namespace App\jobs;use illuminate\contracts\bus\selfhandling;use illuminate\contracts\queue\shouldqueue; Use Symfony\component\process\process;class WebDeploy extends Job implements Selfhandling, shouldqueue{private $ Shellpath; /** * Create a new job instance. */Public Function __construct () { $this->shellpath = dirname (dirname (__dir__));}/** * Execute the job. * @return void */public function handle () { if (!env (' Lm_deploy_branch ')) { echo ' ERR > '. ' No branch Set '. \ n "; } $shell = "/bin/bash". Base_path (' resources/shell/deploy.sh '). ' ' . Base_path (). ' ' . Env (' Lm_deploy_branch ', ' master '); $process = new Process ($shell); $process->start (); $process->wait (function ($type, $buffer) { if (process::err = = = $type) { echo ' ERR > '. $buffer; } else { echo ' out > '. $buffer;}} );}}
2) Trigger queue
Dispatch (New WebDeploy ());
3) Deploying Shell scripts
#!/bin/bashaim_path=$1branch=$2cd ${aim_path}echo $PWD/usr/bin/git pull Origin ${branch} >/dev/null 2>&1if [ $? -eq 0];thenecho "OK" else/usr/bin/git fetch-f/usr/bin/git reset--hard/usr/bin/git Pull Origin ${branch}fi
4) Use Supervisor to listen for queue execution, listen for queue tasks
File Location/etc/supervisord.d/project.ini
[program:project_name]process_name=% (Program_name) s_% (process_num) 02dcommand=php/path/to/project/artisan queue: Work--sleep=3--tries=3--daemonautostart=trueautorestart=trueuser=apachenumprocs=1redirect_stderr=truestdout_ Logfile=/webdata/logs/project.logenvironment=queue_driver=database
Attention points
It took a long time before and with colleagues to study automated deployments, and there was some confusion about whether PHP could do this, but it was possible to deploy the code when the LAN was deployed, but the rest of the time was a failure. This change in this way found a way to run the script. In theory there is no time for execution to be unsuccessful until you see the following error:
Out >/webdata/www/sour-lemon.comerr > Could not create directory '/usr/share/httpd/.ssh '. ERR > Host Key verification failed. ERR > Fatal:could not read from remote repository. Please make sure the correct access rights and the repository exists.
Because the current shell of the user is Apache, so at the time of the call will be the identity of Apache to call this request, and therefore appeared Could not create directory '/usr/share/httpd/.ssh ', so consider using Apache permissions to set up an automated deployment of SSH.
Since Apache users are not allowed to log in, they need to be allowed to sign in first and then set the appropriate SSH key.
Change file/etc/passwd allow user to log in
# before it was/sbin/nologinapache:x:48:48:apache:/usr/share/httpd:/bin/bash
Then switch to Apache user to do SSH key setting, so tested and passed.
Summarize