看起来您已经非常接近,但缺少 2 个重要步骤:设置临时 PHP 环境,并使用该环境安装依赖项(Composer)。
GitHub 操作设置
本指南假设您有一个可用的 Laravel 安装、一个 GitHub 帐户和一个可以使用用户名/密码通过 FTP 访问的共享托管帐户。
我找到了这个视频https://www.youtube.com/watch?v=UNWIXYSZfZY https://www.youtube.com/watch?v=UNWIXYSZfZY有助于基本了解如何部署简单的应用程序。为了使这个答案对更广泛的人有帮助,我将简要概述我的设置。实际上没有任何特定于 Laravel 的步骤。
工作流程目录设置
创建目录.github\workflows
在你的项目的根部。在里面workflows
目录中,创建一个以要推送到共享托管帐户的分支命名的 yml 文件。前任。master.yml
, staging.yml
, development.yml
等等。如果您只有一个分支,那么只需创建一个文件。名称很重要,应与分支机构的名称相匹配。
设计您的工作流程
这很大程度上取决于您的项目,但假设您有一个基本的 Laravel 应用程序,不需要 Node 等其他组件,那么这是一个基本的 GitHub Action,适用于我的各种项目。
基本操作文件由 2 部分组成,the workflow https://docs.github.com/en/actions/using-workflows/triggering-a-workflow, and the jobs https://docs.github.com/en/actions/using-jobs/using-jobs-in-a-workflow。工作流程触发作业。
Workflow
第 1-4 行 https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#on说这会在每次我们运行时运行push
to the master
branch.
on:
push:
branches:
- master
Line 5 https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#name是此工作流程的名称,将显示在您的“操作”页面上。将其设置为描述性内容。
name: ???? Deploy website on push (Master)
设置职位
此部分有 5 个职位。有些接受参数,有些则不接受。我不会在这里解释所有细节,但如果您需要详细信息,我会链接到相应的存储库。
- 检查您的代码,以便工作流程可以访问它,https://github.com/actions/checkout https://github.com/actions/checkout
name: ???? Get latest code
uses: actions/checkout@v2
- 设置一个临时 PHP 环境,以便您可以运行以下内容
作曲家,https://github.com/shivammathur/setup-php https://github.com/shivammathur/setup-php。请务必在此处设置您的 PHP 版本,否则在安装具有意外 PHP 版本的 Composer 软件包时可能会遇到问题。
name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.2
- 缓存您的依赖项以加快部署速度,https://github.com/actions/cache https://github.com/actions/cache
name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- 安装您的依赖项
composer.json
and composer.lock
files.
name: Install dependencies
run: composer install --prefer-dist --no-progress
- 将您的代码部署到远程共享托管站点,https://github.com/SamKirkland/FTP-Deploy-Action https://github.com/SamKirkland/FTP-Deploy-Action。注意使用
${{ secrets.ftp_username }}
and ${{ secrets.ftp_password }}
。这些是在您的存储库的机密部分中设置的。看https://docs.github.com/en/actions/security-guides/encrypted-secrets https://docs.github.com/en/actions/security-guides/encrypted-secrets
name: ???? Sync files
uses: SamKirkland/[email protected] /cdn-cgi/l/email-protection
with:
server: name_of_server.com
username: ${{ secrets.ftp_username }}
password: ${{ secrets.ftp_password }}
server-dir: public_html/
最终文件
on:
push:
branches:
- master
name: ???? Deploy website on push (Master)
jobs:
web-deploy:
name: ???? Deploy
runs-on: ubuntu-latest
steps:
- name: ???? Get latest code
uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: 7.2
- name: Cache Composer packages
id: composer-cache
uses: actions/cache@v2
with:
path: vendor
key: ${{ runner.os }}-php-${{ hashFiles('**/composer.lock') }}
restore-keys: |
${{ runner.os }}-php-
- name: Install dependencies
run: composer install --prefer-dist --no-progress
- name: ???? Sync files
uses: SamKirkland/[email protected] /cdn-cgi/l/email-protection
with:
server: name_of_server.com
username: ${{ secrets.ftp_username }}
password: ${{ secrets.ftp_password }}
server-dir: public_html/
运行工作流程
-
报到.github\workflows\master.yml
,以及其他适当的情况,
进入您的 GitHub 存储库。如果没有这些文件,则什么也不签入
当您将更改推送到分支时会发生。
-
Go to your Actions tab and ensure the workflow shows up there.
-
Push a change to your branch and watch the Actions tab. Click into
the running action to see details about the run.
-
修复控制台中显示的任何错误。
最后,您在评论中提到了一些有关 NPM 的内容。如果您将 Node 作为项目中的组件,您只需运行两个额外的步骤即可捆绑您的资产并与其余代码一起部署。
Good luck!