privatebin-docker-nginx-fpm.../buildx.sh

112 lines
3.1 KiB
Bash
Raw Normal View History

#!/bin/bash
# exit immediately on non-zero return code, including during a pipe stage or on
# accessing an uninitialized variable and print commands before executing them
set -euxo pipefail
2022-12-26 07:00:15 +01:00
EVENT="$1"
IMAGE="$2"
EDGE=false
2021-09-29 20:03:42 +02:00
[ "$3" = edge ] && EDGE=true
build_image() {
# shellcheck disable=SC2068
docker build \
2021-07-14 20:24:07 +02:00
--pull \
2021-10-14 21:26:14 +02:00
--no-cache \
2021-10-14 21:30:50 +02:00
--load \
2021-10-14 20:56:14 +02:00
$@ \
.
}
push_image() {
# shellcheck disable=SC2068
docker buildx build \
--platform linux/amd64,linux/386,linux/arm/v6,linux/arm/v7,linux/arm64,linux/ppc64le \
2021-10-14 21:32:23 +02:00
--pull \
--no-cache \
--push \
2021-10-14 20:56:14 +02:00
$@ \
2021-07-14 20:24:07 +02:00
.
}
2021-07-14 20:24:07 +02:00
docker_login() {
printenv DOCKER_PASSWORD | docker login \
2022-12-26 07:00:15 +01:00
--username "${DOCKER_USERNAME}" \
2021-07-14 20:24:07 +02:00
--password-stdin
}
is_image_push_required() {
2022-12-26 07:00:15 +01:00
[ "${EVENT}" != pull_request ] && { \
[ "${GITHUB_REF}" != refs/heads/master ] || \
[ "${EVENT}" = schedule ]
2021-07-14 20:24:07 +02:00
}
}
main() {
2022-12-26 07:00:15 +01:00
local TAG BUILD_ARGS IMAGE_TAGS
2022-12-26 07:00:15 +01:00
if [ "${EVENT}" = schedule ] ; then
2021-07-14 20:24:07 +02:00
TAG=nightly
else
TAG=${GITHUB_REF##*/}
fi
2022-12-26 07:00:15 +01:00
case "${IMAGE}" in
2021-09-29 20:03:42 +02:00
fs)
BUILD_ARGS="--build-arg ALPINE_PACKAGES= --build-arg COMPOSER_PACKAGES="
;;
2022-10-22 18:13:11 +02:00
gcs)
2022-11-12 07:58:20 +01:00
BUILD_ARGS="--build-arg ALPINE_PACKAGES=php81-openssl --build-arg COMPOSER_PACKAGES=google/cloud-storage"
2022-10-22 18:13:11 +02:00
;;
2021-09-29 20:03:42 +02:00
pdo)
2022-11-05 08:09:58 +01:00
BUILD_ARGS="--build-arg ALPINE_PACKAGES=php81-pdo_mysql,php81-pdo_pgsql --build-arg COMPOSER_PACKAGES="
2021-09-29 20:03:42 +02:00
;;
2022-10-22 18:13:11 +02:00
s3)
2022-11-12 07:58:20 +01:00
BUILD_ARGS="--build-arg ALPINE_PACKAGES=php81-openssl --build-arg COMPOSER_PACKAGES=aws/aws-sdk-php"
2021-09-29 20:03:42 +02:00
;;
*)
BUILD_ARGS=""
;;
esac
2022-12-26 06:55:38 +01:00
IMAGE="privatebin/${IMAGE}:"
IMAGE_TAGS="--tag ${IMAGE}latest --tag ${IMAGE}${TAG} --tag ${IMAGE}${TAG%%-*}"
2021-09-29 20:03:42 +02:00
2022-12-26 07:00:15 +01:00
if [ "${EDGE}" = true ] ; then
2022-12-26 06:34:52 +01:00
# build from alpine:edge instead of the stable release
sed -e 's/^FROM alpine:.*$/FROM alpine:edge/' Dockerfile > Dockerfile.edge
2022-12-26 06:34:52 +01:00
BUILD_ARGS+=" -f Dockerfile.edge"
# replace the default tags, build just the edge one
2022-12-26 06:55:38 +01:00
IMAGE_TAGS="--tag ${IMAGE}edge"
2022-12-26 06:34:52 +01:00
IMAGE+="edge"
else
2022-12-26 07:00:15 +01:00
if [ "${EVENT}" = push ] ; then
2022-12-26 06:34:52 +01:00
# append the stable tag on explicit pushes to master or (git) tags
2022-12-26 06:55:38 +01:00
IMAGE_TAGS+=" --tag ${IMAGE}stable"
2022-12-26 06:34:52 +01:00
fi
# always build latest on normal builds
2022-12-26 06:34:52 +01:00
IMAGE+="latest"
fi
2022-12-26 07:00:15 +01:00
build_image "${BUILD_ARGS} ${IMAGE_TAGS}"
2022-12-26 07:00:15 +01:00
docker run -d --rm -p 127.0.0.1:8080:8080 --read-only --name smoketest "${IMAGE}"
sleep 5 # give the services time to start up and the log to collect any errors that might occur
test "$(docker inspect --format="{{.State.Running}}" smoketest)" = true
curl --silent --show-error -o /dev/null http://127.0.0.1:8080/
if docker logs smoketest 2>&1 | grep -i -E "warn|emerg|fatal|panic|error"
then
exit 1
fi
docker stop smoketest
if is_image_push_required ; then
docker_login
2022-12-26 07:06:14 +01:00
push_image "${BUILD_ARGS} ${IMAGE_TAGS}"
fi
2022-12-26 07:00:15 +01:00
rm -f Dockerfile.edge "${HOME}/.docker/config.json"
}
2021-07-14 20:24:07 +02:00
[ "$(basename "$0")" = 'buildx.sh' ] && main