How to auto-update composer packages with webhooks


In the previous tutorial, created, published and used our own package, we saw that by default, packages don’t auto-update. This implies that whenever we update our repository where these packages are hosted, we need to manually login to Packagist and update this package manually by clicking the update button in our dashboard. This experience could be very tiresome, and we wouldn’t want to do that. Thus, the need to auto-update our packages on Packagist each time our packet repository changes.

In this tutorial, we will make that package we created in the last tutorial to auto-update, wherever there is a change in our git repository.

How to auto-update composer packages with webhooks

To achieve this, we will make use of Webhook feature in GitHub, whose functionality could be summarized as that of an  HTTP callback.

The concept of webhook could be easily understood, it is more of a http call back, an HTTP POST that gets fired whenever something happens by sending a simple event notification.

In our case, this HTTP POST will get fired automatically by GitHub anytime there is push to our GitHub repository.

#Activating Webhook on GitHub

To activate this feature on our package, we will follow the steps below:

  • We will navigate to GitHub, and open the repository that has our published package
  • On this page we will click on “Settings” at the menu bar.
  • Then from the nav bar on the left, Select the option Webhook.
Activating webhook on github

When the Webhook tab opens, we will have some couple of fields to fill, which includes: Payload URL, Content Type and Secret.

#Payload: This is a unique URL, that is specific to your package on Packagist.

#Content type: This specifies how we want GitHub to send our updated packages to Packagist.

#Secret:  This is a unique key generate by Packagist, to ensure that any data its receiving is fully authorized.

These three options could be gotten from our Packagist dashboard here.

Activating webhook on github-2

After filling in these fields, the above screenshot shows that GitHub has been successfully been integrated into our package on Packagist and will be automatically update each time our GitHub repository is updated.

Yahh! finally we have successfully made our package to be auto-updating. Don’t forget to like and share and comment on the comment section. Follow us on twitter for more updates.

Previous: Create, publish and use your first composer package
Next: Making libraries/packages installable using composer

PHP: Tips of the Day

How do I check if a string contains a specific word?

You can use the strpos() function which is used to find the occurrence of one string inside another one:


$a = 'How are you?';

if (strpos($a, 'are') !== false) {
    echo 'true';

Note that the use of !== false is deliberate (neither != false nor === true will return the desired result); strpos() returns either the offset at which the needle string begins in the haystack string, or the boolean false if the needle isn't found. Since 0 is a valid offset and 0 is "falsey", we can't use simpler constructs like !strpos($a, 'are').

Ref : https://bit.ly/3i50vWQ