Skip to content

Commit

Permalink
Fix exit on error during migrations, CI updates, and improve notices …
Browse files Browse the repository at this point in the history
…and messages (#490)

* Force do not exit on error for the try loop on migrations. Fixes #489

* Update notice for custom commands

* Removed Alpine from 7.4.33 because Alpine 3.16 reached EOL (#426 (comment))

* Removed Alpine from PHP 8.0 because Alpine 3.16 reached EOL (#426 (comment))

* Added PHP version and image version to container info start up

* Switch to GitHub runners on Ubuntu 24.04

* Set GHA cache

* Re-added alpine for 7.4 and 8.0 (#426 (comment))

* Converted to buildx and added platform support

* Adjusted PRs to run automatically

* Deprecated and removed linux/arm/v7

* Sign into docker registry only if we're pushing the image

* Added docs to clarify why we don't use subshells (#481 (comment))

* Fixed link
  • Loading branch information
jaydrogers authored Nov 7, 2024
1 parent f2250d8 commit 2095c21
Show file tree
Hide file tree
Showing 12 changed files with 103 additions and 21 deletions.
18 changes: 14 additions & 4 deletions .github/workflows/action_publish-images-prs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ name: Docker Publish (PR Images)

on:
workflow_dispatch:
inputs:
pr_number:
description: 'PR number to build (leave empty for manual branch build)'
required: false
type: string
pull_request:
types: [opened, synchronize, reopened]
paths:
Expand All @@ -12,11 +17,16 @@ on:

jobs:
build-dev-images:
if: ${{ github.event_name == 'pull_request' && ! github.event.pull_request.head.repo.fork }}
uses: ./.github/workflows/service_docker-build-and-publish.yml
with:
registry-repositories: "docker.io/serversideup/php-dev" # Set to our development repository
tag-prefix: "${{ github.event.pull_request.number }}"
registry-repositories: "docker.io/serversideup/php-dev"
# Use PR number from input if provided, otherwise use the PR event number
tag-prefix: ${{ inputs.pr_number || github.event.pull_request.number }}
release-type: testing
authenticate_with_ghcr: false
secrets: inherit
push-to-registry: >-
${{
github.event_name == 'workflow_dispatch' ||
(github.event_name == 'pull_request' && github.event.pull_request.head.repo.owner.type == 'Organization')
}}
secrets: inherit
2 changes: 1 addition & 1 deletion .github/workflows/scheduled-task_update-sponsors.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ on:
- cron: 30 15 * * 0-6
jobs:
deploy:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
steps:
- name: Checkout 🛎️
uses: actions/checkout@v4
Expand Down
32 changes: 20 additions & 12 deletions .github/workflows/service_docker-build-and-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,14 @@ on:
type: string
default: 'testing'
description: 'The type of release to create. Options: testing, latest'
push-to-registry:
type: boolean
default: true
description: 'Whether to push the images to the registry.'

jobs:
setup-matrix:
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
outputs:
php-version-map-json: ${{ steps.get-php-versions.outputs.php-version-map-json }}
steps:
Expand Down Expand Up @@ -68,10 +72,12 @@ jobs:

docker-publish:
needs: setup-matrix
runs-on:
- runs-on
- runner=4cpu-linux-x64
- run-id=${{ github.run_id }}
runs-on: ubuntu-24.04
## Use AWS runners
# runs-on:
# - runs-on
# - runner=4cpu-linux-x64
# - run-id=${{ github.run_id }}
strategy:
matrix: ${{fromJson(needs.setup-matrix.outputs.php-version-map-json)}}

Expand All @@ -93,13 +99,14 @@ jobs:
##
- name: Login to DockerHub
uses: docker/login-action@v3
if: ${{ inputs.push-to-registry }}
with:
username: ${{ secrets.DOCKER_HUB_USERNAME }}
password: ${{ secrets.DOCKER_HUB_ACCESS_TOKEN }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v3
if: ${{ inputs.authenticate_with_ghcr }}
if: ${{ inputs.push-to-registry && inputs.authenticate_with_ghcr }}
with:
registry: ghcr.io
username: ${{ github.actor }}
Expand Down Expand Up @@ -139,23 +146,24 @@ jobs:
echo "REPOSITORY_BUILD_VERSION=git-${SHORT_SHA}-${{ github.run_id }}" >> $GITHUB_ENV
fi
- name: Build and push
- name: Build images
uses: docker/build-push-action@v6
with:
file: src/variations/${{ matrix.php_variation }}/Dockerfile
cache-from: type=s3,blobs_prefix=cache/${{ github.repository }}/,manifests_prefix=cache/${{ github.repository }}/,region=${{ env.RUNS_ON_AWS_REGION }},bucket=${{ env.RUNS_ON_S3_BUCKET_CACHE }}
cache-to: type=s3,blobs_prefix=cache/${{ github.repository }}/,manifests_prefix=cache/${{ github.repository }}/,region=${{ env.RUNS_ON_AWS_REGION }},bucket=${{ env.RUNS_ON_S3_BUCKET_CACHE }},mode=max
cache-from: type=gha
cache-to: type=gha
## Run-on cache
# cache-from: type=s3,blobs_prefix=cache/${{ github.repository }}/,manifests_prefix=cache/${{ github.repository }}/,region=${{ env.RUNS_ON_AWS_REGION }},bucket=${{ env.RUNS_ON_S3_BUCKET_CACHE }}
# cache-to: type=s3,blobs_prefix=cache/${{ github.repository }}/,manifests_prefix=cache/${{ github.repository }}/,region=${{ env.RUNS_ON_AWS_REGION }},bucket=${{ env.RUNS_ON_S3_BUCKET_CACHE }},mode=max
build-args: |
BASE_OS_VERSION=${{ matrix.base_os }}
PHP_VERSION=${{ matrix.patch_version }}
PHP_VARIATION=${{ matrix.php_variation }}
REPOSITORY_BUILD_VERSION=${{ env.REPOSITORY_BUILD_VERSION }}
platforms: |
linux/amd64
linux/arm/v7
linux/arm64/v8
pull: true
push: true
push: ${{ inputs.push-to-registry }}
tags: ${{ env.DOCKER_TAGS }}
outputs: type=image,name=target,annotation-index.org.opencontainers.image.description=Supercharge your PHP experience with Docker images that are based off the official PHP images but are optimized to be run in production environments for Laravel and WordPress and more
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Your script should:
- Located in the `/etc/entrypoint.d` directory
- Have the file extension ending in `.sh`
::

We recommend writing your script in `/bin/sh` for the best compatibility between Alpine and Debian. If you choose to use `/bin/bash`, your script will only be able to run on Debian-based images.

## Choose your execution order
Expand All @@ -27,6 +28,15 @@ Since [we provide default entrypoint scripts](/docs/getting-started/default-conf
If you want to disable our entrypoint scripts, you can set `DISABLE_DEFAULT_CONFIG` to `true` in your environment variables.
::

## Long running services
::note
Don't use entrypoint scripts for long-running services. You want your services to be monitored and restarted if they crash.
::

Anything in the `/etc/entrypoint.d` directory are scripts that are intended to run quickly and then move on. If you run a service as an entrypoint script, that service may crash and not be restarted.

Instead, learn about [using S6 overlay](/docs/guide/using-s6-overlay) so your services can be properly initialized and monitored. See the [S6 Overylay project](https://github.com/just-containers/s6-overlay) for more details on how to write your own S6 service.

## Example: Create a custom entrypoint script
In this example, let's create a `99-my-script.sh` so it executes after all the other default scripts.

Expand Down Expand Up @@ -95,6 +105,11 @@ services:
In the above file, we're building our image using the `Dockerfile` in the current directory. We're also mounting our current directory to `/var/www/html` in the container.

## Don't use `exit 0` in your script
If you use `exit 0` in your script, it will stop the execution of the rest of the entrypoint scripts. We recommend using `return 0` instead. [See this discussion](https://github.com/serversideup/docker-php/issues/481#issuecomment-2463082306) for more details on why.

Long story short, we don't use subshells to execute your scripts, so `exit 0` will not work as expected. We do this because we want to ensure your script has access to the environment variables that are set in the entrypoint scripts.

## Running our example
When we run `docker compose up`, we should see the following output:

Expand All @@ -121,4 +136,4 @@ example-project | 2023/12/05 19:52:38 [info] 67#67 OpenSSL 3.0.11 19 Sep 2023,
```
::

You can see our `👋 Hello, world!` is executing *after* the initialization of `10-init-unit.sh`.
You can see our `👋 Hello, world!` is executing *after* the initialization of `10-init-unit.sh`.
33 changes: 31 additions & 2 deletions scripts/dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ PHP_BUILD_PREFIX=""
DOCKER_REPOSITORY="${DOCKER_REPOSITORY:-"serversideup/php"}"
DOCKER_ADDITIONAL_BUILD_ARGS=()
CUSTOM_REGISTRY=""
PLATFORM=""

# UI Colors
function ui_set_yellow {
Expand Down Expand Up @@ -68,11 +69,34 @@ check_vars() {
return 0
}

detect_platform() {
local arch=$(uname -m)
case $arch in
x86_64)
echo "linux/amd64"
;;
arm64|aarch64)
echo "linux/arm64/v8"
;;
*)
echo "Unsupported architecture: $arch" >&2
exit 1
;;
esac
}

build_docker_image() {
build_tag="${DOCKER_REPOSITORY}:${PHP_BUILD_PREFIX}${PHP_BUILD_VERSION}-${PHP_BUILD_VARIATION}-${PHP_BUILD_BASE_OS}"
echo_color_message yellow "🐳 Building Docker Image: $build_tag"
docker build \
${DOCKER_ADDITIONAL_BUILD_ARGS[@]} \

# Set default platform if not specified
if [ -z "$PLATFORM" ]; then
PLATFORM=$(detect_platform)
fi

docker buildx build \
"${DOCKER_ADDITIONAL_BUILD_ARGS[@]}" \
--platform "$PLATFORM" \
--build-arg PHP_VARIATION="$PHP_BUILD_VARIATION" \
--build-arg PHP_VERSION="$PHP_BUILD_VERSION" \
--build-arg BASE_OS_VERSION="$PHP_BUILD_BASE_OS" \
Expand Down Expand Up @@ -105,6 +129,7 @@ help_menu() {
echo " --os <os> Set the base OS (e.g., bullseye, bookworm, alpine)"
echo " --prefix <prefix> Set the prefix for the Docker image (e.g., beta)"
echo " --registry <registry> Set a custom registry (e.g., localhost:5000)"
echo " --platform <platform> Set the platform (default: detected from system architecture)"
echo " --* Any additional options will be passed to the docker buildx command"
echo
echo "Environment Variables:"
Expand Down Expand Up @@ -136,6 +161,10 @@ while [[ $# -gt 0 ]]; do
CUSTOM_REGISTRY="$2"
shift 2
;;
--platform)
PLATFORM="$2"
shift 2
;;
--*)
# If there's a next argument and it starts with '--', treat the current argument as standalone.
# Otherwise, pair the current argument with the next.
Expand Down
2 changes: 2 additions & 0 deletions src/common/etc/entrypoint.d/0-container-info.sh
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Docker user: $(whoami)
Docker uid: $(id -u)
Docker gid: $(id -g)
OPcache: $PHP_OPCACHE_MESSAGE
PHP Version: $(php -r 'echo phpversion();')
Image Version: $(cat /etc/serversideup-php-version)
"

if [ "$PHP_OPCACHE_STATUS" = "0" ]; then
Expand Down
6 changes: 6 additions & 0 deletions src/common/etc/entrypoint.d/50-laravel-automations.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ if [ "$DISABLE_DEFAULT_CONFIG" = "false" ]; then
echo "🚀 Clearing Laravel cache before attempting migrations..."
php "$APP_BASE_DIR/artisan" config:clear

# Do not exit on error for this loop
set +e
echo "⚡️ Attempting database connection..."
while [ $count -lt "$timeout" ]; do
test_db_connection > /dev/null 2>&1
status=$?
Expand All @@ -64,6 +67,9 @@ if [ "$DISABLE_DEFAULT_CONFIG" = "false" ]; then
fi
done

# Re-enable exit on error
set -e

if [ $count -eq "$timeout" ]; then
echo "Database connection failed after multiple attempts."
return 1
Expand Down
2 changes: 1 addition & 1 deletion src/s6/etc/entrypoint.d/10-init-webserver-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ script_name="init-webserver-config"

# Check if S6 is initialized
if [ "$S6_INITIALIZED" != "true" ]; then
echo "ℹ️ [NOTICE]: S6 is not initialized. Skipping web server configuration and running custom command."
echo "ℹ️ [NOTICE]: Running custom command instead of web server configuration: '$*'"
return 0
fi

Expand Down
3 changes: 3 additions & 0 deletions src/variations/cli/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ RUN docker-php-serversideup-dep-install-alpine "${DEPENDENCY_PACKAGES_ALPINE}" &
chown -R www-data:www-data /var/www && \
chmod -R 755 /var/www && \
\
# Set the image version
echo "${REPOSITORY_BUILD_VERSION}" > /etc/serversideup-php-version && \
\
# Make composer cache directory
mkdir -p "${COMPOSER_HOME}" && \
chown -R www-data:www-data "${COMPOSER_HOME}" && \
Expand Down
3 changes: 3 additions & 0 deletions src/variations/fpm-apache/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ RUN docker-php-serversideup-dep-install-debian "${DEPENDENCY_PACKAGES_DEBIAN}"
chown -R www-data:www-data /var/www && \
chmod -R 755 /var/www && \
\
# Set the image version
echo "${REPOSITORY_BUILD_VERSION}" > /etc/serversideup-php-version && \
\
# Make composer cache directory
mkdir -p "${COMPOSER_HOME}" && \
chown -R www-data:www-data "${COMPOSER_HOME}" && \
Expand Down
3 changes: 3 additions & 0 deletions src/variations/fpm-nginx/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,9 @@ RUN docker-php-serversideup-dep-install-alpine "${DEPENDENCY_PACKAGES_ALPINE}" &
chown -R www-data:www-data /var/www && \
chmod -R 755 /var/www && \
\
# Set the image version
echo "${REPOSITORY_BUILD_VERSION}" > /etc/serversideup-php-version && \
\
# Make composer cache directory
mkdir -p "${COMPOSER_HOME}" && \
chown -R www-data:www-data "${COMPOSER_HOME}" && \
Expand Down
3 changes: 3 additions & 0 deletions src/variations/fpm/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,9 @@ RUN rm -rf /usr/local/etc/php-fpm.d/*.conf && \
chown -R www-data:www-data /var/www && \
chmod -R 755 /var/www && \
\
# Set the image version
echo "${REPOSITORY_BUILD_VERSION}" > /etc/serversideup-php-version && \
\
# Make composer cache directory
mkdir -p "${COMPOSER_HOME}" && \
chown -R www-data:www-data "${COMPOSER_HOME}" && \
Expand Down

0 comments on commit 2095c21

Please sign in to comment.