Deploying PHP Applications with Deployer on GitHub Actions

Deploying PHP Applications with Deployer on GitHub Actions

ยท

4 min read

In this comprehensive guide, we'll delve into the intricacies of deploying PHP applications using Deployer through GitHub Actions. Deployer is a powerful deployment tool written in PHP, offering "Zero Downtime Deployments" out of the box. By integrating Deployer with GitHub Actions, we unlock a plethora of possibilities for automating and streamlining our deployment processes.

Introduction to Deployer and GitHub Actions

What is Deployer?

Deployer simplifies the deployment process by automating tasks such as compiling CSS and JavaScript files, installing Composer dependencies, and seamlessly linking the new deployment with the webserver. This ensures that your application remains accessible and functional during the deployment process, eliminating downtime. Additionally, Deployer facilitates atomic deployments, allowing for easy rollback in case of issues.

What are GitHub Actions?

GitHub Actions is GitHub's built-in continuous integration and delivery feature, empowering developers to automate workflows in response to various events, such as code pushes, pull requests, or new releases. By harnessing the power of GitHub Actions, we can trigger deployments based on a wide range of events, enabling seamless integration with our development workflow.

Why Deploy with GitHub Actions?

Deploying applications through GitHub Actions offers numerous advantages:

  1. Accessibility: By moving the deployment process to GitHub, we make it accessible to a broader audience within our team or organization.

  2. Automation: GitHub Actions allows us to automate deployment tasks, reducing manual intervention and human error.

  3. Integration: Integration with third-party services such as Slack, Alfred, or iOS Shortcuts opens up endless possibilities for triggering deployments from various platforms and devices.

  4. Event-driven Deployment: GitHub Actions provides a vast array of events that can trigger deployments, ranging from code pushes to scheduled tasks, enhancing flexibility and adaptability.

Determining Deployment Triggers

Before diving into the deployment workflows, it's crucial to define when deployments should occur. This decision depends on factors such as team workflows, project requirements, and release schedules. Common deployment triggers include:

  • Manual deployments triggered through the GitHub UI or API requests.

  • Automatic deployments triggered by events such as code pushes, tag releases, or scheduled tasks.

Workflow Examples

Let's explore some workflow examples for deploying PHP applications with Deployer on GitHub Actions:

Example 1: Manual Deployment

This workflow allows for manual triggering of deployments through the GitHub UI or API requests. Here's an overview of the workflow:

  • Event Trigger: The workflow is triggered manually through the GitHub UI or API request.

  • Deployment Environment: Users can specify the deployment environment (e.g., staging or production).

  • Workflow Steps:

    • Clone the project repository.

    • Set up PHP environment.

    • Install dependencies using Composer.

    • Deploy the application using Deployer to the specified environment.

Triggering Deployment

  • GitHub UI: Navigate to the "Actions" tab, select the "Deploy (Manual)" workflow, and click "Run workflow." Specify the desired environment and trigger the deployment.

  • API Request: Utilize the GitHub API to dispatch a workflow event, specifying the deployment environment.

Example CURL Request

curl \
  -X POST \
  -H "Authorization: token PRIVATE_ACCESS_TOKEN" \
  -H "Accept: application/vnd.github.v3+json" \
  https://api.github.com/repos/YOUR_ORG/YOUR_REPOSITORY/actions/workflows/deploy_manual.yaml/dispatches \
  -d '{"ref":"main", "inputs": {"deploy_env": "prod"}}'

Example 2: Tag-based Production Deployment

This workflow automates deployment to the production environment whenever a new version is tagged and released. The steps include:

  • Event Trigger: Deployment is triggered automatically upon tagging and releasing a new version.

  • Deployment Process: Similar to manual deployment, but targeted specifically for production environment.

Example 3: Continuous Integration Deployment

In this scenario, deployments are triggered automatically upon code pushes to the main branch. This ensures continuous integration and deployment for ongoing development.

Example 4: Scheduled Nightly Build

Automate nightly builds and deployments to a test environment using scheduled workflows. This ensures regular testing and validation of the application.

Conclusion

Deploying PHP applications with Deployer on GitHub Actions offers a seamless and efficient deployment solution, integrating seamlessly with your development workflow. By leveraging GitHub Actions' event-driven architecture, we can automate deployments, enhance collaboration, and streamline the release process. With the provided examples and insights, you're equipped to optimize your deployment strategy and elevate your development workflow to new heights.

For further exploration, refer to the accompanying example Laravel application and GitHub repository for practical implementation and experimentation.

Happy deploying! ๐Ÿš€

ย