Skip to content

Add PHP build support with customizable environment variables #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM ubuntu:22.04
FROM ubuntu:24.04

ARG DEBIAN_FRONTEND=noninteractive

Expand All @@ -9,9 +9,10 @@ LABEL "com.github.actions.description"="Deploy WordPress code to a server"
LABEL "org.opencontainers.image.source"="https://github.com/rtCamp/action-deploy-wordpress"


ENV PATH "/composer/vendor/bin:~/.local/bin:$PATH"
ENV COMPOSER_ALLOW_SUPERUSER 1
ENV COMPOSER_HOME /composer
ENV PATH="/composer/vendor/bin:~/.local/bin:$PATH"
ENV COMPOSER_ALLOW_SUPERUSER=1
ENV COMPOSER_HOME=/composer
ENV DEFAULT_PHP_VERSION=7.4

RUN apt update && \
apt install -y \
Expand All @@ -27,7 +28,7 @@ RUN apt update && \
add-apt-repository ppa:ondrej/php && \
apt update && \
apt-get install -y php7.4-cli php7.4-curl php7.4-json php7.4-mbstring php7.4-xml php7.4-iconv php7.4-yaml && \
pip3 install shyaml && \
pip3 install shyaml --break-system-packages && \
rm -rf /var/lib/apt/lists/*

# Setup wp-cli
Expand Down
43 changes: 42 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,49 @@ This GitHub action's behavior can be customized using following environment vari
| `NODE_BUILD_DIRECTORY` | null | path to valid directory on repository. | Build directory. Generally root directory or directory like frontend. |
| `NODE_BUILD_COMMAND` | null | `npm run build` or similar command. | Command used to to build the dependencies needed on deployment. |
| `NODE_BUILD_SCRIPT` | null | path to valid shell script | Custom or predefined script to run after compilation. |
| `PHP_BUILD_DIRECTORY` | null | Path to valid directory | Directory to run PHP build commands/scripts in. |
| `PHP_BUILD_COMMAND` | null | Any shell command | Command to run for building PHP dependencies or other PHP build steps. |
| `PHP_BUILD_SCRIPT` | null | Path to valid shell script | Custom or predefined script to run for PHP build steps. |

All node related variables are completely optional. You can use them if your site needs to have node dependencies built.
All node and php related variables are completely optional. You can use them if your site needs to have such dependencies built.

### PHP Build Support

You can now run custom PHP build steps before deployment by setting the following environment variables in your workflow:

```yaml
env:
PHP_VERSION: "8.1"
PHP_BUILD_DIRECTORY: "./"
PHP_BUILD_COMMAND: "composer install --no-dev"
PHP_BUILD_SCRIPT: "./scripts/php-build.sh"
```

- `PHP_VERSION`: Installs the specified PHP version before running build steps.
- `PHP_BUILD_DIRECTORY`: Directory to change into before running build commands/scripts.
- `PHP_BUILD_COMMAND`: Shell command to run for PHP build (e.g., `composer install`).
- `PHP_BUILD_SCRIPT`: Path to a shell script to execute for PHP build steps.

### Node Build Support

You can also run custom Node.js build steps before deployment by setting the following environment variables in your workflow:

```yaml
env:
NODE_VERSION: "20"
NPM_VERSION: "10"
NODE_BUILD_DIRECTORY: "./frontend"
NODE_BUILD_COMMAND: "npm run build"
NODE_BUILD_SCRIPT: "./scripts/node-build.sh"
```

- `NODE_VERSION`: Installs the specified Node.js version before running build steps.
- `NPM_VERSION`: Installs the specified npm version (defaults to latest if not set).
- `NODE_BUILD_DIRECTORY`: Directory to change into before running build commands/scripts.
- `NODE_BUILD_COMMAND`: Shell command to run for Node.js build (e.g., `npm run build`).
- `NODE_BUILD_SCRIPT`: Path to a shell script to execute for Node.js build steps.

These steps will be executed before PHP build steps.

## Server Setup

Expand Down
40 changes: 39 additions & 1 deletion main.sh
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,42 @@ function maybe_run_node_build() {
fi
}

function maybe_install_php_dep() {

VALID_PHP_VERSIONS=("7.4" "8.0" "8.1" "8.2" "8.3" "8.4")
if [[ -z "$PHP_VERSION" ]]; then
echo "No PHP version specified. Skipping PHP setup."
return
fi

if [[ ! " ${VALID_PHP_VERSIONS[@]} " =~ " ${PHP_VERSION} " ]]; then
echo "Invalid PHP version specified: $PHP_VERSION"
echo "Valid versions are: ${VALID_PHP_VERSIONS[*]}"
exit 1
fi

git config --global --add safe.directory "$GITHUB_WORKSPACE"
apt install -y software-properties-common && \
add-apt-repository ppa:ondrej/php && \
apt update && \
apt-get install -y php"$PHP_VERSION"-cli php"$PHP_VERSION"-curl php"$PHP_VERSION"-mbstring php"$PHP_VERSION"-xml php"$PHP_VERSION"-iconv php"$PHP_VERSION"-yaml

update-alternatives --set php /usr/bin/php${PHP_VERSION}
}

function maybe_run_php_build() {

[[ -n "$PHP_BUILD_DIRECTORY" ]] && cd "$PHP_BUILD_DIRECTORY"
[[ -n "$PHP_BUILD_COMMAND" ]] && eval "$PHP_BUILD_COMMAND"

if [[ -n "$PHP_BUILD_SCRIPT" ]]; then
chmod +x "$PHP_BUILD_SCRIPT"
./"$PHP_BUILD_SCRIPT"
fi

update-alternatives --set php /usr/bin/php${DEFAULT_PHP_VERSION}
}

function setup_wordpress_files() {

mkdir -p "$HTDOCS"
Expand Down Expand Up @@ -332,9 +368,11 @@ function main() {
setup_hosts_file
check_branch_in_hosts_file
setup_ssh_access
maybe_install_submodules
maybe_install_node_dep
maybe_run_node_build
maybe_install_submodules
maybe_install_php_dep
maybe_run_php_build
setup_wordpress_files
block_emails
deploy
Expand Down