提交自定义包到githb并设置自动composer包自动更新
项目中频繁使用的代码片段，通常会被封装成全局方法或类，以提高程序的复用性。但初始化新项目，手动引入会相对麻烦，此时可以提交到composer上实现自动安装。大致步骤创建Git代码仓库使用composer在本地初始化提交代码到线上Git仓库，如Git、GitLab等在packagist平台提交git版本库设置packagist自动更新1. 创建Git仓库各Git托管平台操作类似，以GitHub为例:2. 初始化composer创建完成Git仓库后，克隆仓库到本地，在仓库根目录执行composer init进行初始化composer init按照命令要求依次录入项目命名空间、项目描述、作者信息、最低稳定版本、项目类型、授权类型、依赖信息等信息，生成composer.json文件 Welcome to the Composer config generator This command will guide you through creating your composer.json config. Package name (<vendor>/<name>) [ponycool/ponycool-library]: Description []: PonyCool Library is an extension library for the CodeIgniter framework Author [Pony <pony@ponycool.com>, n to skip]: n Minimum Stability []: Package Type (e.g. library, project, metapackage, composer-plugin) []: library License []: MIT Define your dependencies. Would you like to define your dependencies (require) interactively [yes]? yes Search for a package: php Enter the version constraint to require (or leave blank to use the latest version): 8.0 Search for a package: Would you like to define your dev dependencies (require-dev) interactively [yes]? yes Search for a package: Add PSR-4 autoload mapping? Maps namespace "Pony\PonycoolLibrary" to the entered relative path. [src/, n to skip]: { "name": "Ponycool/ponycool-library", "description": "PonyCool Library is an extension library for the CodeIgniter framework", "type": "library", "require": { "php": "8.0" }, "license": "MIT", "autoload": { "psr-4": { "Ponycool\\PonycoolLibrary\\": "src/" } } } Do you confirm generation [yes]? yes Would you like the vendor directory added to your .gitignore [yes]? yes Would you like to install dependencies now [yes]? yescomposer.json文件的详细参数可以参考此链接3. 提交代码到代码仓库提交代码到线上代码平台操作非常简单，在这不再赘述。线上代码仓库尽量选择GitHub、GitLab、Bitbucket、Gitea。目前packagist官方暂时只支持以上4个平台的webhook，其他代码平台需自行实现update-packages4. 在packagist平台提交git版本库打开packagist，通过用户名/密码登录到首页后，点击首页上方的submit按钮提交代码仓库录入Git仓库的仓库地址，点击check，如果仓库中缺少composer.json文件是不能提交的。会提示以下内容：Warning:No composer.json was found in the main branch.检查通过后，点击submit按钮进行提交提交成功后，这时项目还不能够自动更新，会有以下提示:This package is not auto-updated. Please set up the GitHub Hook for Packagist so that it gets updated whenever you push!5. 设置packagist自动更新首先需要获取Packagist API Token访问 https://packagist.org/profile/，在页面上点击「Show API Token」按钮，获取你的 Packagist API Token设置自动更新 WebHook在需要设置发布和自动更新的 GitHub 仓库下，通过「Settings」->「Webhooks」添加一个新的 WebHook，配置参考如下：URL：https://packagist.org/api/github?username=YOUR_PACKAGIST_USERNAMEContent Type: application/json，Secret: your Packagist API Token其中：YOUR_PACKAGIST_USERNAME 需要替换为你在 Packagist.org 上的用户名，Secret 为上文中获得到的 Packagist API Token。事件勾选 Just the push event. 即可。配置完成后，通过向仓库推送代码。即可触发自动更新到 Packagist.org。Packagist平台上的自动更新提示将变为以下内容:This package is auto-updated.目前packagist官方暂时只支持GitHub、GitLab、Bitbucket、Gitea以上4个平台的webhook，其他代码平台需自行实现update-packages。参考链接