static-php-cli/bin/spc-alpine-docker

179 lines
5.1 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env bash
set -e
2023-04-30 12:42:19 +08:00
# This file is using docker to run commands
2025-07-10 20:20:29 +08:00
SPC_DOCKER_VERSION=v5
2023-04-30 12:42:19 +08:00
# Detect docker can run
if ! which docker >/dev/null; then
echo "Docker is not installed, please install docker first !"
exit 1
2023-04-30 12:42:19 +08:00
fi
DOCKER_EXECUTABLE="docker"
# shellcheck disable=SC2046
2023-04-30 12:42:19 +08:00
if [ $(id -u) -ne 0 ]; then
if ! docker info > /dev/null 2>&1; then
if [ "$SPC_USE_SUDO" != "yes" ]; then
echo "Docker command requires sudo"
# shellcheck disable=SC2039
2023-04-30 12:42:19 +08:00
echo -n 'To use sudo to run docker, run "export SPC_USE_SUDO=yes" and run command again'
exit 1
fi
DOCKER_EXECUTABLE="sudo docker"
fi
fi
# Convert uname to gnu arch
CURRENT_ARCH=$(uname -m)
if [ "$CURRENT_ARCH" = "arm64" ]; then
CURRENT_ARCH=aarch64
fi
if [ -z "$SPC_USE_ARCH" ]; then
SPC_USE_ARCH=$CURRENT_ARCH
2023-04-30 12:42:19 +08:00
fi
# parse SPC_USE_ARCH
2023-04-30 12:42:19 +08:00
case $SPC_USE_ARCH in
x86_64|amd64)
SPC_USE_ARCH=x86_64
if [ "$CURRENT_ARCH" != "x86_64" ]; then
PLATFORM_ARG="--platform linux/amd64"
ALPINE_FROM=multiarch/alpine:x86_64-edge
fi
2023-04-30 12:42:19 +08:00
;;
aarch64|arm64)
SPC_USE_ARCH=aarch64
if [ "$CURRENT_ARCH" != "aarch64" ]; then
PLATFORM_ARG="--platform linux/arm64"
ALPINE_FROM=multiarch/alpine:aarch64-edge
fi
2023-04-30 12:42:19 +08:00
;;
*)
echo "Current arch is not supported to run in docker: $SPC_USE_ARCH"
exit 1
;;
esac
2025-07-10 20:10:54 +08:00
# if ALPINE_FROM is not set, use alpine:3.21
if [ -z "$ALPINE_FROM" ]; then
2025-07-10 20:10:54 +08:00
ALPINE_FROM=alpine:3.21
fi
if [ "$SPC_USE_ARCH" != "$CURRENT_ARCH" ]; then
echo "* Using different arch needs to setup qemu-static for docker !"
ALPINE_FROM=multiarch/alpine:$SPC_USE_ARCH-edge
if [ "$(uname -s)" = "Linux" ]; then
$DOCKER_EXECUTABLE run --rm --privileged multiarch/qemu-user-static:register --reset > /dev/null
fi
else
2025-07-10 20:10:54 +08:00
ALPINE_FROM=alpine:3.21
fi
2023-04-30 12:42:19 +08:00
if [ "$SPC_USE_MIRROR" = "yes" ]; then
SPC_USE_MIRROR="RUN sed -i 's/dl-cdn.alpinelinux.org/mirrors.tuna.tsinghua.edu.cn/g' /etc/apk/repositories"
else
SPC_USE_MIRROR="RUN echo 'Using original repo'"
fi
# Detect docker env is setup
if ! $DOCKER_EXECUTABLE images | grep -q cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION; then
2023-04-30 12:42:19 +08:00
echo "Docker container does not exist. Building docker image ..."
$DOCKER_EXECUTABLE build $PLATFORM_ARG -t cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION -f- . <<EOF
2023-04-30 12:42:19 +08:00
FROM $ALPINE_FROM
$SPC_USE_MIRROR
2023-08-22 11:13:51 +02:00
RUN apk update; \
2024-07-07 22:57:42 +08:00
apk upgrade -a; \
2023-08-22 11:13:51 +02:00
apk add --no-cache \
autoconf \
automake \
gettext \
2023-08-22 11:13:51 +02:00
bash \
binutils \
bison \
build-base \
cmake \
curl \
file \
flex \
g++ \
gcc \
git \
jq \
libgcc \
2023-12-10 18:27:19 +08:00
libtool \
2023-08-22 11:13:51 +02:00
libstdc++ \
linux-headers \
m4 \
make \
pkgconfig \
re2c \
2023-08-22 11:13:51 +02:00
wget \
xz \
gettext-dev \
binutils-gold \
patchelf
2023-09-14 01:00:51 +02:00
RUN curl -#fSL https://dl.static-php.dev/static-php-cli/bulk/php-8.4.4-cli-linux-\$(uname -m).tar.gz | tar -xz -C /usr/local/bin && \
chmod +x /usr/local/bin/php
RUN curl -#fSL https://getcomposer.org/download/latest-stable/composer.phar -o /usr/local/bin/composer && \
chmod +x /usr/local/bin/composer
2023-04-30 12:42:19 +08:00
WORKDIR /app
ADD ./src /app/src
COPY ./composer.* /app/
2023-04-30 12:42:19 +08:00
ADD ./bin /app/bin
RUN composer install --no-dev
2023-04-30 12:42:19 +08:00
EOF
fi
2023-04-30 12:52:24 +08:00
# Check if in ci (local terminal can execute with -it)
if [ -t 0 ]; then
INTERACT=-it
else
INTERACT=''
fi
2024-07-09 16:14:33 +08:00
# Mounting volumes
MOUNT_LIST=""
# shellcheck disable=SC2089
MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/config:/app/config"
MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/src:/app/src"
MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/buildroot:/app/buildroot"
MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/source:/app/source"
MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/dist:/app/dist"
MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/downloads:/app/downloads"
MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/pkgroot:/app/pkgroot"
if [ -f "$(pwd)/craft.yml" ]; then
MOUNT_LIST="$MOUNT_LIST -v ""$(pwd)""/craft.yml:/app/craft.yml"
fi
2024-07-09 16:14:33 +08:00
# Environment variable passthrough
ENV_LIST=""
ENV_LIST="$ENV_LIST -e SPC_FIX_DEPLOY_ROOT="$(pwd)""
if [ ! -z "$GITHUB_TOKEN" ]; then
ENV_LIST="$ENV_LIST -e GITHUB_TOKEN=$GITHUB_TOKEN"
fi
2023-04-30 12:42:19 +08:00
# Run docker
# shellcheck disable=SC2068
2024-07-09 16:14:33 +08:00
# shellcheck disable=SC2086
# shellcheck disable=SC2090
if [ "$SPC_DOCKER_DEBUG" = "yes" ]; then
echo "* Debug mode enabled, run docker in interactive mode."
echo "* You can use 'exit' to exit the docker container."
echo "* You can use 'bin/spc' like normal builds."
echo "*"
echo "* Mounted directories:"
echo "* ./config: $(pwd)/config"
echo "* ./src: $(pwd)/src"
echo "* ./buildroot: $(pwd)/buildroot"
echo "* ./source: $(pwd)/source"
echo "* ./dist: $(pwd)/dist"
echo "* ./downloads: $(pwd)/downloads"
echo "* ./pkgroot: $(pwd)/pkgroot"
echo "*"
set -ex
$DOCKER_EXECUTABLE run $PLATFORM_ARG --rm $INTERACT $ENV_LIST $MOUNT_LIST cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION /bin/bash
else
$DOCKER_EXECUTABLE run $PLATFORM_ARG --rm $INTERACT $ENV_LIST $MOUNT_LIST cwcc-spc-$SPC_USE_ARCH-$SPC_DOCKER_VERSION bin/spc $@
fi