This commit introduces a mechanism to use the setup-php scripts for configuring PHP environments on local Linux/Ubuntu machines. Key changes: - Added `src/local_installer.ts`: A Node.js script that takes command-line arguments to define the desired PHP setup (version, extensions, tools, INI settings) and generates a `run.sh` script. - Added `src/local_utils.ts`: A utility module with functions adapted for local execution, removing dependencies on `@actions/core`. It supports fetching inputs from yargs/environment variables. - Modified `src/fetch.ts`: Replaced `@actions/http-client` with `node-fetch` for compatibility. - Updated `src/config.ts`, `src/coverage.ts`, `src/extensions.ts`, and `src/tools.ts` to import utilities from `local_utils.ts` instead of `utils.ts`. - Added `LOCAL_SETUP_INSTRUCTIONS.md`: Comprehensive documentation on prerequisites, how to run the local installer, command-line options, and troubleshooting. This allows users to leverage the robust PHP environment configuration capabilities of this project directly on their development machines.
7.8 KiB
Local PHP Environment Setup using setup-php Scripts
This document provides instructions on how to use the setup-php action's scripts to configure a PHP environment on your local Linux/Ubuntu machine, outside of GitHub Actions.
This process involves using a Node.js script (src/local_installer.ts) to generate a shell script (run.sh) tailored to your desired PHP setup. You then execute run.sh to perform the actual installation and configuration.
Prerequisites
- Linux/Ubuntu Environment: These instructions are primarily for Ubuntu. Debian or other Ubuntu derivatives might work but are not explicitly tested.
- Node.js and npm: Required to run the
local_installer.tsscript.- Install Node.js (which includes npm) from NodeSource or your distribution's package manager. Version 16+ is recommended.
- Curl: Used by the scripts to download PHP versions and tools.
sudo apt update sudo apt install curl - Build Tools (Recommended): For installing PECL extensions or some PHP versions from source.
sudo apt install -y build-essential autoconf automake bison re2c libtool make pkg-config - Git: To clone the repository.
sudo apt install -y git - Sudo Access: The generated
run.shscript will requiresudoprivileges to install packages and configure PHP.
Setup Steps
-
Clone the
setup-phpRepository:git clone https://github.com/shivammathur/setup-php.git cd setup-php -
Install Node.js Dependencies: This will install
yargs(for command-line argument parsing bylocal_installer.ts) andnode-fetch(used by the adaptedsrc/fetch.ts).npm install yargs node-fetch@2 # If your project uses ES Modules and you prefer node-fetch v3: # npm install yargs node-fetchNote:
node-fetch@2is CJS compatible which is generally easier withts-node. If you have an ESM setup, v3+ might be used. -
Compile TypeScript (Optional but Recommended): While you can run
.tsfiles directly withts-node, compiling them to JavaScript first is a cleaner approach for repeated use.npm install -g typescript ts-node # Install TypeScript and ts-node globally if not already # Or as dev dependencies: npm install --save-dev typescript ts-node @types/node @types/yargs tsc --project tsconfig.json # Ensure your tsconfig.json is set up for CJS output to a 'dist' folder typicallyIf you compile, the command in the next step will change (e.g.
node dist/local_installer.js). For simplicity, the following steps will usets-node. If you compile, adjust the path accordingly. -
Run
local_installer.tsto Generaterun.sh: Executesrc/local_installer.tsusingts-node, providing your desired PHP configuration as command-line arguments.Command Syntax:
npx ts-node src/local_installer.ts [options]Available Options:
--php-version <version>: PHP version (e.g.,8.2,7.4,latest,nightly). Default:latest.--php-version-file <path>: Path to a.php-versionor similar file. Default:.php-version.--extensions <csv_list>: Comma-separated list of PHP extensions (e.g.,mbstring,gd,intl,zip). Default:"".--ini-file <type>: Basephp.inito use (production,development,none). Default:production.--ini-values <csv_list>: Comma-separated list ofphp.inivalues (e.g.,memory_limit=512M,post_max_size=128M). Default:"".--coverage <driver>: Code coverage driver (xdebug,pcov,none). Default:"".--tools <csv_list>: Comma-separated list of tools to install (e.g.,composer,phpunit:latest,php-cs-fixer). Default:composer.--fail-fast <boolean>: Exit immediately if a tool or extension fails. Default:false.--phpts <type>: PHP thread safety (ntsorzts/ts). Default:nts.--update <boolean>: Force update PHP to the latest patch version. Default:false.--debug <boolean>: Install debug build of PHP. Default:false.--tools-dir <path>: Directory for installing global tools. Default:/usr/local/bin.--runner-tool-cache <path>: Directory for caching downloaded tools (simulates GitHub ActionsRUNNER_TOOL_CACHE). Default:/opt/hostedtoolcache. (Ensure this directory is writable by the user runningsudo bash run.shor create it with appropriate permissions:sudo mkdir -p /opt/hostedtoolcache && sudo chmod -R 777 /opt/hostedtoolcache)
Example: To set up PHP 8.2 with extensions
mbstring,gd,zip,intland toolscomposerandphpunit(latest version):npx ts-node src/local_installer.ts \ --php-version 8.2 \ --extensions "mbstring,gd,zip,intl" \ --tools "composer,phpunit:latest" \ --ini-values "memory_limit=256M,date.timezone=UTC"This will generate a
run.shfile in your current directory (setup-php). -
Review and Execute
run.sh: Inspect the generatedrun.shscript to understand the commands that will be executed.less run.shExecute the script with
sudousingbash:sudo bash run.shThe script will then proceed to install and configure PHP and the specified extensions and tools. This may take some time.
-
Verify Installation: After the script finishes, verify your PHP setup:
php -v php -m # List compiled modules (extensions) composer --version # If installed phpunit --version # If installed
Environment Variables for Composer (Optional)
If you use private Composer repositories or hit GitHub API rate limits, you might need to set authentication environment variables before running sudo bash run.sh:
GITHUB_TOKEN: Your GitHub Personal Access Token for Composer to access private GitHub repositories or increase rate limits.export GITHUB_TOKEN="your_github_pat" # Then run: sudo -E bash run.sh (the -E preserves user environment variables)PACKAGIST_TOKEN: For private Packagist.export PACKAGIST_TOKEN="your_packagist_token" # Then run: sudo -E bash run.shCOMPOSER_AUTH_JSON: For other types of Composer authentication, as a JSON string.
It's often better to pass these toexport COMPOSER_AUTH_JSON='{"http-basic": {"example.org": {"username": "user", "password": "password"}}}' # Then run: sudo -E bash run.shsudousing the-Eflag if you set them in your user shell:sudo -E bash run.sh.
Troubleshooting
- Script Failures: If
run.shfails, examine the output for error messages. These often indicate missing system dependencies (e.g.,-devlibrary packages for a PECL extension) or network issues. - Permissions: Ensure the
RUNNER_TOOL_CACHEdirectory (default/opt/hostedtoolcache) is writable. Thelocal_installer.tsscript will print a message if it's using this. You might need to create and set permissions for it:sudo mkdir -p /opt/hostedtoolcache && sudo chown -R $(whoami):$(whoami) /opt/hostedtoolcache(orsudo chmod -R 777 /opt/hostedtoolcache). fetch.ts/node-fetch: The adaptedsrc/fetch.tsusesnode-fetch. Ensure it's installed correctly.- Path Issues: After installation, if tools like
composerare not found, ensure that thetools-dir(default/usr/local/bin) is in your system'sPATH. The scripts attempt to add relevant paths to shell profiles, but you might need to source your profile (source ~/.bashrc) or open a new terminal.
This local setup mechanism provides a powerful way to replicate parts of the setup-php GitHub Action's functionality directly on your Ubuntu machine.