We’ve launched our first official GitHub Action for PHP-Prefixer, and it is now available on the GitHub marketplace. This initial action covers the core integration scenario to connect your GitHub builds with your PHP namespace and scope prefixing process.
PHP-Prefixer is a service to apply PHP prefixes to namespaces, functions, helpers, traits, interfaces, and more. You start with a Composer project, and a set of dependencies and prefix all library files at once to generate a consistent prefixed codebase.
This blog post demonstrates how you can get started with the GitHub Action to prefix the Hello Prefixed World plugin for WordPress.
What is ‘GitHub Actions’?
For starters, GitHub Actions is a popular platform to automate software development workflows, like CI/CD built around the GitHub ecosystem.
You define your workflow using a YAML configuration file and store it in your Git repository. You can compose your automation with reusable building blocks called ‘Actions‘. Workflows are executed in containers for a repeatable and reliable process.
GitHub organizes the available Actions on the GitHub marketplace. Below, you can find how a PHP-Prefixer Action looks on the Marketplace:
Getting started with GitHub Actions for PHP-Prefixer
To illustrate how to use the new GitHub Action for PHP-Prefixer, we updated the Hello Prefixed World plugin to automate the plugin prefixing.
The plugin has a call to the PHP Prefixer Build Action (.github/workflow/prefix.yml
) that integrates the service with GitHub Actions and PHP-Prefixer abstracts the complexity of manually applying prefixes to PHP files. The service automates and streamlines the process of prefixing while providing the scalability and simplicity of serverless computing.
The complete workflow file is available in the sample plugin repository:
#
# Getting started with GitHub Actions for PHP-Prefixer
#
name: PHP-Prefixer
on: [workflow_dispatch]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Run PHP-Prefixer
uses: PHP-Prefixer/php-prefixer-build-action@main
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
personal_access_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
project_id: ${{ secrets.PROJECT_ID }}
Note that we reference two secrets in this configuration. One is for the PHP-Prefixer Personal Access Token to authenticate, and the second is for the Project ID to integrate with your configured project.
Running PHP Prefixer Build Action
The Action is a straightforward implementation of the PHP-Prefixer CLI. The Action clones the repository, packages the project in a ZIP file, sends the source code to PHP-Prefixer and receives the results. The new package is then committed to the prefixed branch to make it available at the repository level.
The sample Action is configured on the sample plugin repository that you can run manually.
Success!
In the Step 3 screenshot, we can see how the Action runs and processes the output that the user receives on the screen. It might take a few iterations to fix syntax and get everything right, but a successful prefixed project is what you get as an outcome.
Beyond the basic configuration
The Action requires two parameters to function, and it can receive additional parameters for GitHub integration:
Parameter | Description | Required | Example |
---|---|---|---|
PERSONAL_ACCESS_TOKEN | The PHP-Prefixer PAT/Personal Access Token. The token must be configured in the PHP-Prefixer account. | Yes | 789|123456789... |
PROJECT_ID | The project ID to process the source code. The project must be configured in your account in the PHP-Prefixer account. | Yes | 5432 |
SOURCE_DIR_PATH | The relative path to the source project directory. It must contain a .git repository and composer.json file. If not, the base repository directory will be used as the value. Example: foo/bar . | No | ./ |
TARGET_BRANCH | The branch in the repository where PHP-Prefixer will store the prefixed files after processing. Default value: prefixed . | No | |
GH_PERSONAL_ACCESS_TOKEN | The GitHub PAT/Personal Access Token to access private repositories. It is only required if the project, the library or the dependencies are private. | No | ghp_F4fZ9Cq7QF... |
Prefixing Private Repositories
Follow these steps to prefix private repositories:
- Step 1: Create a GitHub Personal Access Token for the Github account you wish to authenticate with.
- Step 2: Next, add the GitHub Personal Access Token to your project using Github Secrets, and pass it into the Action using the input.
#
# Snippet of the PHP-Prefixer Action declaring the GH_PERSONAL_ACCESS_TOKEN
#
jobs:
build:
...
- name: Run PHP-Prefixer
uses: PHP-Prefixer/php-prefixer-build-action@v0.0.6
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
personal_access_token: ${{ secrets.PERSONAL_ACCESS_TOKEN }}
project_id: ${{ secrets.PROJECT_ID }}
gh_personal_access_token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
Conclusion
GitHub Action for PHP-Prefixer is now available. This release includes installing and using the PHP-Prefixer CLI, and committing the prefixed codebase to a prefixed branch in the source repository.
You can now automate your builds with GitHub Actions and integrate with PHP-Prefixer for all your deployment and automation needs.
Please let us know in the comments if there are any you’d like us to improve.
Happy prefixing!