تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Continuous Deployment Overview0:00

At the moment, we have a production-ready application hosted on our server. We handle deployment by SSHing into the server, switching to the laracast user, and running git pull. And this is fine for apps that aren't under active long-term development. However, in most cases, we deploy our applications continuously to make changes and fix bugs. And having to SSH into the server and run commands every time we want to deploy is not ideal. So in this lesson, I'm going to show you how to continuously deploy your application using GitHub Actions. And to do that, we need to allow GitHub Actions to SSH into our server and run the deployment.

Creating SSH Keypair0:40

GitHub Actions. And to do that, we need to allow GitHub Actions to SSH into our server and run the deployment commands. But, and that's a very important thing, only the ubuntu user on our system is open for SSH connections, and that user has sudo privileges to do anything on the server. It's not a good idea to allow GitHub Actions to SSH as ubuntu. We have to make the laracasts system user accept SSH connections and use it for our deployments. So let's start with that. The first thing we're going to do is generate a key pair on our local machine. So we run ssh-keygen -t rsa -C "github" -f ./storage/key.

The first thing we're going to do is generate a key pair on our local machine. So we run ssh-keygen -t rsa -C "github" in the current working directory storage. We then enter an empty passphrase, confirm the empty passphrase. And here we go. Let's go to the storage directory where we generated the keys and print the public key. So cat key.pub. Now let's copy the key. And then we ssh into the server using the ubuntu user. And once we are in, we switch to the laracast user. Then go to the home/laracast/.ssh directory.

Authorizing Server SSH Key2:04

And once we are in, we switch to the Laracast user. Then go to the home laracast directory. Then we create a new file, nano authorized_keys. Inside this file, we paste the public key we just generated, save the file, and then adjust the permissions on it to 600. So chmod 600 authorized_keys. Now let's exit the Laracast session and the Ubuntu session and go back to our local machine. We are still inside the storage directory, which has our keys. Let's then ssh into our server using this key. So ssh -i key laracasts@the IP address.

Let's then ssh into our server using this key. So ssh -i key laracasts at the IP address. And here we go. We are now connected as laracasts. Let's check the current working directory and its home laracasts. All look good. The next step for us would be creating a GitHub workflow that deploys our application. In this workflow, we are going to use an action from the GitHub marketplace called SSH action. This will help us ssh into the server and run our deployment script. So let's do that.

Writing GitHub Actions Workflow3:28

This will help us SSH into the server and run our deployment script. So let's do that. Let's first edit our .gitignore file and ignore the storage.key file and the storage.key.pub file. That way they won't be uploaded to git. And then we create a new directory in our application root/.github/workflows. And inside this directory, we create a file named deploy.yaml. Inside this file, we will give our workflow a name, deploy, and configure it to run on workflow_dispatch. That way we can trigger this action manually for the sake of this tutorial.

And configure it to run on workflow dispatch. That way we can trigger this action manually for the sake of this tutorial. However, in your normal setup, you may want to trigger the action when you deploy to a certain branch, for example. I leave that for you to decide. So next for us would be adding some jobs to this workflow. We only need a single job and we will call it deploy. Give our job a name, deploy.aws. And make it run on Ubuntu 22.04. And for the steps, we will add a single step called deploy.

And make it run on Ubuntu 22.04. And for the steps, we will add a single step called deploy. This one uses the ApplePoy SSH action version 0.1.8. Then we provide the action input. For the host, we will paste our server's IP address. Then the port is 22. The username is our SSH user, laracast. And then for the key, we will refer to an action secret that we will create later. We will call this secret privateKey. And finally, for our script, we will add cd www.

We will call this secret private key. And finally, for our script, we will add cd www. Since we will be inside the home laracast directory when we SSH, we just need to go inside the www directory. Open the command and start a new one, git pull origin main. This script will change the working directory to www and run git pull. And that's all we need. Now let's go push our changes to the github repo. I'm using an alias called web that just pushes the changes with a command message of web. And done.

Adding GitHub Secret Key6:06

I'm using an alias called web that just pushes the changes with a command message of web. And done. Now all we need to do is add this private key secret to our github settings. And then run our deployment action. So let's go grab the private key from our local machine and add it to github. Let's print the private key by running cat storage/key. And copy the contents of the file. Then we go to our github repo. Go to settings, secrets and variables, actions. Then we click on new repository secret.

Running and Extending Deployment7:12

Then we run the workflow. Our workflow is now queued and waiting for a github worker to process it. Give it a few seconds. And here we go, it's complete. Let's go check the results. Click on the deploy to AWS job. Then inspect the deploy step. Scroll down. And here it is. The action was able to SSH into the server and run git pull.

And here it is. The action was able to ssh into the server and run git pull. The output indicates that git found two changes in our deploy.yml file and the .gitignore file, which are the changes we applied in this lesson. So inside this script input in the GitHub Action, you can add any deployment commands you want, like running composer install and php artisan migrate and any commands that you want for the deployment to be successful. And as I mentioned earlier, you can configure the action to run when you push a commit to any of the branches in your GitHub repository. That way you get automated push to deploy.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟