php-actions/composer@v6

Using php-actions/composer@v6 in GitHub Actions

In this article, we are going to have a look at the process of installing Composer packages in GitHub Actions with php-actions/composer@v6 action as well as consider what GitHub Action is.

What is GitHub Action

GitHub Action – is a custom application for the GitHub Actions platform that performs a complex and often repetitive task. GitHub Action is used to reduce the amount of repetitive code you write in workflow files.
GitHub Action can git pull your git repository from GitHub set the right toolkit for your workflow, or set up authentication for your cloud provider.

You can also create your own action.

Action php-actions/composer@v6

php-actions/composer@v6 – is a GitHub action that allows you to install Composer packages to run linter, UnitTests, Integration Tests, static analyzers, or for building projects.

Benefits of using php-actions/composer@v6:

  • Ease of Use: This GitHub Action is easy to set up and use in your project, giving you a quick start and making it easy to automate your work with Composer.
  • Integration with GitHub Actions: Its use within the GitHub Actions system provides a single tool to manage automation in your project.
  • Updates and Support: Actions is actively maintained by its developers to ensure timely bug fixes and new functionality.
  • Integration with other GitHub Actions: You can combine php-actions/composer@v6 with other GitHub Actions to create complex workflows.

Using

Create a GitHub Workflow file in  .github/workflows/ci.yml  with the following content:

name: CI

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3
      - uses: php-actions/composer@v6

     # now run your PHPCS package
      - name: Run PHPCS
        run: composer run phpcs

Now, when you run your code in the repository, and go to the Actions tab, you will see how the GitHub Action pipeline is running:

using php-actions/composer@v6

If you open the php-actions/composer@v6 tab you will see a log of installing Composer packages:

using php-actions/composer@v6

Cache using

GitHub Actions allows you to cache Composer package downloads from the Internet between different workflows/jobs. This speeds up project builds. If the composer.lock file is changed, the packages will be re-downloaded from the Internet because the cache will be invalid.

You can use cache in php-actions/composer@v6 the next way:

name: CI

on: [push]

jobs:
  build:
    runs-on: [ubuntu-latest]
    
    steps:
    - uses: actions/checkout@v3

    - name: Cache Composer dependencies
      uses: actions/cache@v3
      with:
        path: /tmp/composer-cache
        key: ${{ runner.os }}-${{ hashFiles('**/composer.lock') }}
      
    - uses: php-actions/composer@v6

    ...      

In the above example, a “key” is passed to the Cache, which consists of a hash of the composer.lock file. This means that as long as the contents of composer.lock are not changed between workflows, the Composer cache directory will be kept between workflows.

Example

For a real example of how to use Composer, PHPCS and GitHub Actions, check out the repository https://github.com/renakdup/using-phpcs-for-wordpress

Result

In this short article, you learned what a GitHub Action is, how to use php-actions/composer@v6 action, and how to use caching. There are also many other settings you can read about here.


Андрей Писаревский

Author: Andrei Pisarevskii 

PHP | WordPress Team Lead. I have commercial programming experience since 2010 and expertise in the full cycle of web development: Frontend, Backend, QA, Server administration, managing large teams and Enterprise projects.

Leave a Reply

Your email address will not be published. Required fields are marked *