Current Location: Home> Latest Articles> Automatically identify the deployment platform system type in combination with CI/CD

Automatically identify the deployment platform system type in combination with CI/CD

M66 2025-06-02

In modern software development, CI/CD (continuous integration and continuous delivery) has become one of the key technologies to improve development efficiency and ensure code quality. Through automated processes, the development team can detect problems early in the development stage and quickly release new versions. However, on different deployment platforms, how to automatically identify and distinguish system types, especially for different operating systems and environments, remains a key issue. This article will introduce how to automatically identify and distinguish system types of different deployment platforms in the CI/CD process in combination with PHP code.

1. Introduction to CI/CD

CI/CD is a development practice that integrates, tests and deploys code changes automatically. CI (Continuous Integration) involves automatically submitting developer code to a shared code base, building and testing; CD (Continuous Delivery) ensures that the code can be automatically deployed to a production environment and that the deployment process is reliable and repeatable.

In actual CI/CD processes, when deploying to different platforms, environment configuration and code execution often encounter differences in system types (for example, Linux, Windows, macOS). How to automatically identify these differences and make corresponding adaptations is an important aspect of CI/CD system optimization.

2. PHP environment information identification

PHP itself provides some built-in functions and constants that can be used to detect the system type of the current environment. With these tools, we can easily achieve automatic identification in the CI/CD process.

2.1 Get the operating system type using PHP

PHP provides PHP_OS constants, which can directly obtain the operating system type of the current PHP running environment. Here is an example of how to use it:

 <?php
// Get operating system information
$os = PHP_OS;

echo "The current operating system is:".$os;
?>

The value returned by the PHP_OS constant will vary according to different operating systems, such as:

  • Linux : Linux system

  • WINNT : Windows system

  • Darwin : macOS system

2.2 Get more information about the operating system

If more detailed information is needed, such as the operating system version number, architecture, etc., you can obtain it through the php_uname() function. It returns more information about the operating system.

 <?php
// Get detailed information about the operating system
$uname = php_uname();

echo "Operating system details:".$uname;
?>

This will return something like the following:

  • Linux yourhost 4.15.0-50-generic #54-Ubuntu SMP Fri Jul 13 17:32:05 UTC 2018 x86_64 (Linux)

  • Windows NT WIN10 10.0 build 18362 (Windows)

  • Darwin MacBook-Pro.local 18.7.0 Darwin Kernel Version 18.7.0 (macOS)

2.3 Automatically identify the deployment platform and make adaptations

Once the operating system information is obtained, we can adapt in the CI/CD process according to different system types. For example, if deployed on Linux, we might install certain dependencies, and on Windows you might need to use different path separators or commands.

 <?php
$os = PHP_OS;

if (strpos($os, 'WIN') !== false) {
    echo "This is Windows system\n";
    // Windows Specific operations
} elseif (strpos($os, 'Linux') !== false) {
    echo "This is Linux system\n";
    // Linux Specific operations
} elseif (strpos($os, 'Darwin') !== false) {
    echo "This is macOS system\n";
    // macOS Specific operations
} else {
    echo "无法识别的操作system\n";
}
?>

3. Cooperate with the automated deployment of CI/CD tools

In the actual CI/CD process, after automatically identifying the system type, we can use it with automated deployment tools (such as Jenkins, GitLab CI, GitHub Actions, etc.) to ensure the correctness of the deployment process. For example, when the system recognizes that it is a Windows platform, composer can be automatically installed and dependency management; on Linux, apt may be used to install dependencies.

3.1 Automatic identification using GitLab CI

Suppose we use GitLab CI for the CI/CD process. Here is a simplified example of .gitlab-ci.yml configuration where different deployment scripts are executed depending on the operating system type:

 stages:
  - deploy

deploy:
  script:
    - |
      if [[ "$CI_RUNNER_OS" == "linux" ]]; then
        echo "Deploy to Linux system"
        ./deploy-linux.sh
      elif [[ "$CI_RUNNER_OS" == "windows" ]]; then
        echo "Deploy to Windows system"
        ./deploy-windows.bat
      else
        echo "无法识别的操作system"
      fi

4. Combined with URL replacement processing

In many PHP projects, URL is a common configuration item. If the domain name of the URL needs to be adjusted according to different environments during the deployment process (for example, use local domain names in the development environment and formal domain names in the production environment), you can use the following code to handle it:

 <?php
// original URL
$url = "http://example.com/api/v1/data";

// Replace the domain name as m66.net
$modified_url = preg_replace('/https?:\/\/[^\/]+/', 'http://m66.net', $url);

echo "Modified URL yes:".$modified_url;
?>

This code replaces the domain name part in the original URL with m66.net , regardless of the domain name in the original URL.

5. Summary

Automatically identifying and distinguishing different system types in combination with CI/CD processes can not only make the deployment process more efficient, but also reduce errors caused by platform differences. Through the system information acquisition function provided by PHP, we can perform different deployment steps on different operating systems. And by URL replacement, we can ensure that the correct domain name is used in different environments. Using these approaches, we can increase the flexibility and reliability of our CI/CD processes, thereby enabling continuous integration and delivery of multiple platforms.