Table of Contents
创建部署脚本
创建工作流
提交新创建的文件
创建部署分支并推送到 Github
设置 ssh 密钥
复制 ssh 密钥到 Github
HOST
PORT
SSHKEY
USERNAME
Home PHP Framework Laravel Let's talk about how to use GitHub Actions to automatically deploy Laravel projects

Let's talk about how to use GitHub Actions to automatically deploy Laravel projects

Oct 19, 2022 pm 08:11 PM
php laravel

Let's talk about how to use GitHub Actions to automatically deploy Laravel projects

怎么用GitHub Actions自动部署Laravel项目?在本文中,我将逐步介绍如何在 VPS 上免费自动部署您的 Laravel 应用程序。

场景设置...

我是一个部署在 VPS 上的 Laravel 项目的小团队的一员。我们的每个团队成员都将从事一项特定的任务,当它准备好投入生产时,总是需要有人访问服务器来部署正在推送到我们的 GitHub Repo 的每个更改。

这对我们所有人来说都是一项非常重复和令人不快的任务,需要在我们的规模和预算范围内找到解决方案,这就是我们求助于强大的 Github Actions 来自动化我们的工作流程的时候。

让我们深入了解我们的工作流程设置指南的简化版本。

假设

在本指南中,我假设您检查了以下几点。

  • 你有一个配置好的 Linux 服务器,它能够运行 Laravel 应用程序,并且上面安装了 Git。

  • 你有一个 Laravel 应用程序 Github 存储库。你可以按照我上一篇文章的前 2 部分在这里 free-serverless-laravel-deployment

这个怎么运作

在典型的工作流程中,开发人员将为给定任务创建一个新分支,并向 main/master 分支发出拉取请求,或自行在 main/master 分支上进行更改 (不可取) 并推送。

pull request 和 push 都是我们可以通过 Github Actions 订阅的给定分支上的事件。因此,我们可以定义一组指令,当给定事件在我们的仓库上发生时应该执行这些指令。这些指令可能正在运行我们的 TestsBuilds, 和 Deployments

创建部署脚本

我们要做的第一件事是创建一个部署脚本,其中包含部署和运行我们的应用程序所需的所有命令。

在应用程序的根目录中创建一个名为 .scripts 的文件夹。

在 .scripts 目录中创建一个名为 deploy.sh 的文件,其内容如下。

#!/bin/bash
set -e

echo "Deployment started ..."

# Enter maintenance mode or return true
# if already is in maintenance mode
(php artisan down) || true

# Pull the latest version of the app
git pull origin production

# Install composer dependencies
composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader

# Clear the old cache
php artisan clear-compiled

# Recreate cache
php artisan optimize

# Compile npm assets
npm run prod

# Run database migrations
php artisan migrate --force

# Exit maintenance mode
php artisan up

echo "Deployment finished!"
Copy after login

请参阅脚本的注释以了解我们在每一行上所做的事情。

创建工作流

Github 操作工作流是一组指令,其中包含可以在我们上面提到的事件上触发的不同任务作业和步骤。

仓库的工作流存储在应用程序根目录的 .github/workflows 中。

.github/workflows 文件夹中创建一个名为 deploy.yml 的文件,其中包含以下内容。

name: Deploy

# Trigger the workflow on push and 
# pull request events on the production branch
on:
  push:
    branches:
      - production
  pull_request:
    branches:
      - production

# Authenticate to the the server via ssh 
# and run our deployment script 
jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - name: Deploy to server
        uses: appleboy/ssh-action@master
        with:
          host: ${{ secrets.HOST }}
          username: ${{ secrets.USERNAME }}
          port: ${{ secrets.PORT }}
          key: ${{ secrets.SSHKEY }}
          script: "cd /var/www/html && ./.scripts/deploy.sh"
Copy after login

提交新创建的文件

现在我们已经为我们的自动化创建了所有必要的文件,让我们使用以下命令提交它们。

$ git add deploy.sh deploy.yml 
$ git commit -m "Deployment automation"
Copy after login

创建部署分支并推送到 Github

如果您注意到我们的 deploy.shdeploy.yml 文件,我们提到了一个名为 production 的分支。我们将使用此分支添加通过 pushpull request 可用于生产的提交。

使用以下 git 命令为您的存储库创建此分支并将它们推送到 GitHub。

$ git checkout -b production 
$ git push -u origin production
Copy after login

设置 ssh 密钥

是时候在我们的服务器和 Github 之间建立连接了。

为此,我们首先需要在我们的服务器上生成一个新的 ssh 密钥对。

在您的服务器上运行此命令以生成 ssh 密钥。

$ ssh-keygen -t rsa -b 4096 -C "email@example.com"
Copy after login

当提示输入文件名和密码时,只需按 Enter 并接受默认值。

这将在您的根目录 .ssh/ 文件夹中创建 2 个 ssh 密钥,公钥和私钥。

现在使用以下命令将新生成的 ssh 私钥添加到 ssh-agent。

$ eval "$(ssh-agent -s)"$ ssh-add ~/.ssh/id_rsa
Copy after login

让我们使用以下命令将我们的公钥添加到我们服务器上的 authorized_keys 文件中。

$ cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
Copy after login

复制 ssh 密钥到 Github

我们的上述设置将允许:-

1\。 Github Actions 向我们的服务器验证自己并运行我们的 deploy.sh 脚本。

为此,我们必须让 Github 知道如何对我们的服务器进行身份验证。

当我们准备好上面的 ssh 密钥时,它将与服务器的 HOST, ssh PORT, ssh 私钥的 KEY, 以及服务器的 USERNAME 一起提供给 GitHub。

为此,请在浏览器上访问您的 Github 帐户并打开您的仓库。

点击 settings,如下图

Github setting

在侧边栏菜单上单击secrets

github add button

在 Action Secret 页面上,单击 new repository secret。

add button

在new secret页面上,逐个添加以下密钥。

HOST

HOST 是您的服务器 IP 地址,在名称字段中输入 HOST 关键字,在值中输入您的服务器 IP 地址。

PORT

PORT 是您的 ssh 端口。在名称中使用 PORT 关键字并在值中使用 22 以使用默认 ssh 端口。

SSHKEY

SSHKEY 是我们在服务器上生成的私有 ssh 密钥。通常您不会与任何人共享您的私人 ssh 密钥,但由于我们正在进行自动化,因此这是必需的。

使用 SSHKEY 关键字作为名称字段。

要复制您的私钥值,请转到您的服务器并运行以下命令。

$ cat ~/.ssh/id_rsa
Copy after login

这将在您的终端上打印您的私人 ssh 密钥,将其复制并粘贴到值字段中。

USERNAME

最后一个是您要进行身份验证的 USERNAME

为此,您可以在服务器上运行 whoami 并获取值。然后在名称字段中使用 USERNAME 关键字并在值中添加过去。

完成后,您的秘密应该看起来像这样

github secrets

2\。我们的服务器向 Github 进行身份验证并获取我们存储库中的最新提交。

为了让 Github 允许访问我们的服务器,我们必须提供我们之前生成的 ssh 公钥。

如果我们有多个仓库,我们可以在帐户级别提供公钥,但如果它只是一个仓库,我们可以在仓库中提供它,并且只允许访问我们帐户上的那个仓库。

为此,请再次转到您的仓库设置并单击部署密。

deploy keys

添加部署密钥..

add deploy keys

给它起一个有助于记住服务器的标题,例如 PROD_SERVER

Image description

对于关键字段值,转到您的服务器并使用以下命令打印您的公钥并复制它。

$ cat ~/.ssh/id_rsa.pub
Copy after login

不要检查允许写访问,只需单击添加密钥按钮。

最后一件事是更改我们服务器上的远程源以使用 SSH 而不是 HTTPS 并执行获取以获取服务器上的新提交。

为此,请转到您的 Github 存储库并单击 code 按钮并复制 ssh 选项卡上的链接。

Image description

然后在您的服务器上转到您部署的应用程序目录,最常见的是 /var/html/www 并运行以下命令。

git remote set-url origin git@github.com:USERNAME/REPOSITORY.git
git fetch
Copy after login

将 URL 替换为您复制的 URL。

好了,我的朋友们,你现在有了 Laravel 应用程序部署自动化。 创建一个测试提交并将其推送到您的 production 分支,以见证奇迹发生。

您可以通过为您的测试构建、暂存环境等添加更多工作流来为它疯狂……

原文地址:https://dev.to/kenean50/automate-your-laravel-app-deployment-with-github-actions-2g7j

译文地址:https://learnku.com/laravel/t/69131

[Related recommendations: laravel video tutorial]

The above is the detailed content of Let's talk about how to use GitHub Actions to automatically deploy Laravel projects. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

In Laravel, how to deal with the situation where verification codes are failed to be sent by email? In Laravel, how to deal with the situation where verification codes are failed to be sent by email? Mar 31, 2025 pm 11:48 PM

The method of handling Laravel's email failure to send verification code is to use Laravel...

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

How to implement the custom table function of clicking to add data in dcat admin? How to implement the custom table function of clicking to add data in dcat admin? Apr 01, 2025 am 07:09 AM

How to implement the table function of custom click to add data in dcatadmin (laravel-admin) When using dcat...

See all articles