Monday 7 February 2022

Publishing your own packages globally to Install via Composer for the Projects Easily

 Version Controlling using Git is now an essential part of the development. But many of people will be unaware of the steps where their publish public repos can be installed via a composer command anywhere without any hassle. For eg: if your repo is located at https://github.com/gitusername/mylib, it can be make in a way for anyone to install in their application by simply typing  

composer require  gitusername/mylib:version

STEPS TO FOLLOW

1) Make sure you have made the package as a public repo in your gitaccount

2) Put readme.md file and try to at least define how to install your package, how to use .etc...

3) Now create composer.json file in the root of the repo

4) In composer.repo, please put the following script and make changes based on your requirement:-

{
"name": "gitusername/packagename",
"description": "Package description",
"type": "library",
"require": {
"php": ">=5.3.6"
},
"autoload": {
"psr-4": {
"Message\\": "src"
}
},
"license": "proprietary",
"minimum-stability": "dev",
"authors": [
{
"name": "Your Name",
"email": "Your Email "
}
]
} If you want to install additional packages along with this package u can mention in require section. for eg:-
"require": {
"phpunit/phpunit": "^6.5",
"squizlabs/php_codesniffer": "^3",
"php-coveralls/php-coveralls": "^2.2"
},

5)Now Put your library files inside src folder

6) Commit Everything and make sure your package is complete. Now in next step we are going to publish for composer installation

7) Now next step is to create a Tag and Release for the package.

8)For Creating tag and Release follow these steps:-

  Go Down your repo, you can see Release option like this:-



After that Click on Create a New Release option, following window will open








Put your version number v1.0 as per your choice, make sure select that create tag on publish 

Now Fill in the title field same version number v1.0








Fill Title, Description and then go down and select publish release






9) After the release is done, next step is we have to register our package to https://packagist.org/

10) Go to https://packagist.org/ and create one account for you

11) Then Click on submit on top






12) Now Copy paste the Public repo URL in this one and submit








13) After this for the repo to get all push changes we have to add a webhook into our Github Repo


14)Go to Settings-> Webhooks in the Github repo













15) Click create Webhook







16)In the Create Webhook Page fill the Url, Secret we get from packagist profile and save it









17) The Payload URL  Can be seen in the Packagist page https://packagist.org/about#how-to-update-packages











Copy this URL and paste in Payload url field in the webhook window


18) Now for getting secret, Go to Packagist profile


After Clicking Show Api token, it will show. Paste same to webhook field and create it


19) Now Final step is, go to the project.. Initiate the composer and type 

composer require  gitusername/packagename:version

Now the package will be installed auto...