feat: add support for nixpacks as a builder

Closes #5338
This commit is contained in:
Jose Diaz-Gonzalez
2023-08-27 12:36:50 -04:00
parent 76a979e139
commit fe6a0f796e
21 changed files with 648 additions and 5 deletions

View File

@@ -0,0 +1,67 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/internal-functions"
source "$PLUGIN_AVAILABLE_PATH/config/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-builder-nixpacks-builder-build() {
declare desc="builder-nixpacks builder-build plugin trigger"
declare trigger="builder-build"
declare BUILDER_TYPE="$1" APP="$2" SOURCECODE_WORK_DIR="$3"
if [[ "$BUILDER_TYPE" != "nixpacks" ]]; then
return
fi
dokku_log_info1 "Building $APP from nixpacks"
if ! command -v "nixpacks" &>/dev/null; then
dokku_log_fail "Missing nixpacks, install it"
fi
local IMAGE=$(get_app_image_name "$APP")
local DOCKER_BUILD_LABEL_ARGS=("--label=dokku" "--label=org.label-schema.schema-version=1.0" "--label=org.label-schema.vendor=dokku" "--label=com.dokku.image-stage=build" "--label=com.dokku.builder-type=dockerfile" "--label=com.dokku.app-name=$APP")
pushd "$SOURCECODE_WORK_DIR" &>/dev/null
plugn trigger pre-build "$BUILDER_TYPE" "$APP" "$SOURCECODE_WORK_DIR"
no_cache="$(fn-builder-nixpacks-computed-no-cache "$APP")"
NIXPACKS_ARGS=""
if [[ "$no_cache" == "true" ]]; then
NIXPACKS_ARGS="$NIXPACKS_ARGS --no-cache"
fi
local DOCKER_ARGS=$(: | plugn trigger docker-args-build "$APP" "$BUILDER_TYPE")
DOCKER_ARGS+=$(: | plugn trigger docker-args-process-build "$APP" "$BUILDER_TYPE")
# strip --link, --volume and -v args from DOCKER_ARGS
local DOCKER_ARGS=$(sed -e "s/^--link=[[:graph:]]\+[[:blank:]]\?//g" -e "s/^--link[[:blank:]]\?[[:graph:]]\+[[:blank:]]\?//g" -e "s/^--volume=[[:graph:]]\+[[:blank:]]\?//g" -e "s/^--volume[[:blank:]]\?[[:graph:]]\+[[:blank:]]\?//g" -e "s/^-v[[:blank:]]\?[[:graph:]]\+[[:blank:]]\?//g" <<<"$DOCKER_ARGS")
declare -a ARG_ARRAY
eval "ARG_ARRAY=($DOCKER_ARGS)"
eval "$(config_export app "$APP" --merged)"
if [[ -f "$SOURCECODE_WORK_DIR/Procfile" ]]; then
if procfile-util exists --process-type release; then
procfile-util delete --process-type release
fi
fi
# shellcheck disable=SC2086
if ! nixpacks build "${DOCKER_BUILD_LABEL_ARGS[@]}" $DOKKU_GLOBAL_BUILD_ARGS "${ARG_ARRAY[@]}" $NIXPACKS_ARGS --name "$IMAGE" "$SOURCECODE_WORK_DIR"; then
dokku_log_warn "Failure building image"
return 1
fi
if ! suppress_output "$DOCKER_BIN" image build -f "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/dockerfiles/builder-build.Dockerfile" --build-arg APP_IMAGE="$IMAGE" -t "$IMAGE" "$SOURCECODE_WORK_DIR"; then
dokku_log_warn "Failure injecting docker labels and custom entrypoint on image"
return 1
fi
plugn trigger post-build "$BUILDER_TYPE" "$APP" "$SOURCECODE_WORK_DIR"
popd &>/dev/null || pushd "/tmp" >/dev/null
}
trigger-builder-nixpacks-builder-build "$@"

View File

@@ -0,0 +1,17 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/config/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-builder-nixpacks-builder-detect() {
declare desc="builder-nixpacks builder-detect plugin trigger"
declare trigger="builder-detect"
declare APP="$1" SOURCECODE_WORK_DIR="$2"
if [[ -f "$SOURCECODE_WORK_DIR/nixpacks.toml" ]]; then
echo "nixpacks"
return
fi
}
trigger-builder-nixpacks-builder-detect "$@"

View File

@@ -0,0 +1,31 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-builder-nixpacks-builder-release() {
declare desc="builder-nixpacks builder-release plugin trigger"
declare trigger="builder-release"
declare BUILDER_TYPE="$1" APP="$2" IMAGE_TAG="$3"
if [[ "$BUILDER_TYPE" != "nixpacks" ]]; then
return
fi
local DOCKER_BUILD_LABEL_ARGS="--label=org.label-schema.schema-version=1.0 --label=org.label-schema.vendor=dokku --label=com.dokku.app-name=$APP --label=com.dokku.image-stage=release --label=dokku"
plugn trigger pre-release-builder "$BUILDER_TYPE" "$APP" "$IMAGE"
TMP_WORK_DIR="$(mktemp -d "/tmp/dokku-${DOKKU_PID}-${FUNCNAME[0]}.XXXXXX")"
trap "rm -rf '$TMP_WORK_DIR' >/dev/null" RETURN INT TERM EXIT
local IMAGE=$(get_app_image_name "$APP" "$IMAGE_TAG")
if ! suppress_output "$DOCKER_BIN" image build "${DOCKER_BUILD_LABEL_ARGS[@]}" $DOKKU_GLOBAL_BUILD_ARGS -f "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/dockerfiles/builder-release.Dockerfile" --build-arg APP_IMAGE="$IMAGE" -t "$IMAGE" "$TMP_WORK_DIR"; then
dokku_log_warn "Failure injecting docker labels on image"
return 1
fi
plugn trigger post-release-builder "$BUILDER_TYPE" "$APP" "$IMAGE"
}
trigger-builder-nixpacks-builder-release "$@"

View File

@@ -0,0 +1,15 @@
#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
source "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/help-functions"
case "$1" in
help | builder-nixpacks:help)
cmd-builder-nixpacks-help "$@"
;;
*)
exit "$DOKKU_NOT_IMPLEMENTED_EXIT"
;;
esac

View File

@@ -0,0 +1,30 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-builder-nixpacks-core-post-extract() {
declare desc="builder-nixpacks post-extract plugin trigger"
declare trigger="post-extract"
declare APP="$1" SOURCECODE_WORK_DIR="$2"
local NEW_NIXPACKS_YML="$(fn-builder-nixpacks-computed-nixpackstoml-path "$APP")"
pushd "$TMP_WORK_DIR" >/dev/null
if [[ -z "$NEW_NIXPACKS_YML" ]]; then
return
fi
if [[ ! -f "$NEW_NIXPACKS_YML" ]]; then
rm -f nixpacks.toml
return
fi
if [[ "$NEW_NIXPACKS_YML" != "nixpacks.toml" ]]; then
mv "$NEW_NIXPACKS_YML" nixpacks.toml
fi
popd &>/dev/null || pushd "/tmp" >/dev/null
}
trigger-builder-nixpacks-core-post-extract "$@"

View File

@@ -0,0 +1,7 @@
ARG APP_IMAGE
FROM $APP_IMAGE
RUN printf '#!/usr/bin/env bash\nexec bash -l -c -- \"$*\"\n' > /usr/local/bin/entrypoint && \
chmod +x /usr/local/bin/entrypoint
ENTRYPOINT ["/usr/local/bin/entrypoint"]

View File

@@ -0,0 +1,2 @@
ARG APP_IMAGE
FROM $APP_IMAGE

View File

@@ -0,0 +1,33 @@
#!/usr/bin/env bash
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-builder-nixpacks-help() {
declare desc="help command"
declare CMD="$1"
local plugin_name="builder-nixpacks"
local plugin_description="Manage the nixpacks builder integration for an app"
if [[ "$CMD" == "${plugin_name}:help" ]]; then
echo -e "Usage: dokku ${plugin_name}[:COMMAND]"
echo ''
echo "$plugin_description"
echo ''
echo 'Additional commands:'
fn-help-content | sort | column -c2 -t -s,
elif [[ $(ps -o command= $PPID) == *"--all"* ]]; then
fn-help-content
else
cat <<help_desc
$plugin_name, $plugin_description
help_desc
fi
}
fn-help-content() {
declare desc="return help content"
cat <<help_content
builder-nixpacks:report [<app>] [<flag>], Displays a builder-nixpacks report for one or more apps
builder-nixpacks:set <app> <property> (<value>), Set or clear a builder-nixpacks property for an app
help_content
}

View File

@@ -0,0 +1,16 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-builder-nixpacks-install() {
declare desc="installs the builder-nixpacks plugin"
declare trigger="install"
fn-plugin-property-setup "builder-nixpacks"
mkdir -p "${DOKKU_LIB_ROOT}/data/builder-nixpacks"
chown -R "${DOKKU_SYSTEM_USER}:${DOKKU_SYSTEM_GROUP}" "${DOKKU_LIB_ROOT}/data/builder-nixpacks"
}
trigger-builder-nixpacks-install "$@"

View File

@@ -0,0 +1,116 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-builder-nixpacks-report() {
declare desc="displays a builder-nixpacks report for one or more apps"
declare cmd="builder-nixpacks:report"
[[ "$1" == "$cmd" ]] && shift 1
declare APP="$1" INFO_FLAG="$2"
if [[ -n "$APP" ]] && [[ "$APP" == --* ]]; then
INFO_FLAG="$APP"
APP=""
fi
if [[ -z "$APP" ]] && [[ -z "$INFO_FLAG" ]]; then
INFO_FLAG="true"
fi
if [[ -z "$APP" ]]; then
for app in $(dokku_apps); do
cmd-builder-nixpacks-report-single "$app" "$INFO_FLAG" | tee || true
done
else
cmd-builder-nixpacks-report-single "$APP" "$INFO_FLAG"
fi
}
cmd-builder-nixpacks-report-single() {
declare APP="$1" INFO_FLAG="$2"
if [[ "$INFO_FLAG" == "true" ]]; then
INFO_FLAG=""
fi
verify_app_name "$APP"
local flag_map=(
"--builder-nixpacks-computed-nixpackstoml-path: $(fn-builder-nixpacks-computed-nixpackstoml-path "$APP")"
"--builder-nixpacks-global-nixpackstoml-path: $(fn-builder-nixpacks-global-nixpackstoml-path "$APP")"
"--builder-nixpacks-nixpackstoml-path: $(fn-builder-nixpacks-nixpackstoml-path "$APP")"
"--builder-nixpacks-computed-no-cache: $(fn-builder-nixpacks-computed-no-cache "$APP")"
"--builder-nixpacks-global-no-cache: $(fn-builder-nixpacks-global-no-cache "$APP")"
"--builder-nixpacks-no-cache: $(fn-builder-nixpacks-no-cache "$APP")"
)
if [[ -z "$INFO_FLAG" ]]; then
dokku_log_info2_quiet "${APP} builder-nixpacks information"
for flag in "${flag_map[@]}"; do
key="$(echo "${flag#--}" | cut -f1 -d' ' | tr - ' ')"
dokku_log_verbose "$(printf "%-30s %-25s" "${key^}" "${flag#*: }")"
done
else
local match=false
local value_exists=false
for flag in "${flag_map[@]}"; do
valid_flags="${valid_flags} $(echo "$flag" | cut -d':' -f1)"
if [[ "$flag" == "${INFO_FLAG}:"* ]]; then
value=${flag#*: }
size="${#value}"
if [[ "$size" -ne 0 ]]; then
echo "$value" && match=true && value_exists=true
else
match=true
fi
fi
done
[[ "$match" == "true" ]] || dokku_log_fail "Invalid flag passed, valid flags:${valid_flags}"
[[ "$value_exists" == "true" ]] || dokku_log_fail "not deployed"
fi
}
fn-builder-nixpacks-computed-nixpackstoml-path() {
declare APP="$1"
file="$(fn-builder-nixpacks-nixpackstoml-path "$APP")"
if [[ "$file" == "" ]]; then
file="$(fn-builder-nixpacks-global-nixpackstoml-path "$APP")"
fi
echo "$file"
}
fn-builder-nixpacks-global-nixpackstoml-path() {
declare APP="$1"
fn-plugin-property-get "builder-nixpacks" "--global" "nixpackstoml-path" "nixpacks.toml"
}
fn-builder-nixpacks-nixpackstoml-path() {
declare APP="$1"
fn-plugin-property-get "builder-nixpacks" "$APP" "nixpackstoml-path" ""
}
fn-builder-nixpacks-computed-no-cache() {
declare APP="$1"
file="$(fn-builder-nixpacks-no-cache "$APP")"
if [[ "$file" == "" ]]; then
file="$(fn-builder-nixpacks-global-no-cache "$APP")"
fi
echo "$file"
}
fn-builder-nixpacks-global-no-cache() {
declare APP="$1"
fn-plugin-property-get "builder-nixpacks" "--global" "no-cache" "false"
}
fn-builder-nixpacks-no-cache() {
declare APP="$1"
fn-plugin-property-get "builder-nixpacks" "$APP" "no-cache" ""
}

View File

@@ -0,0 +1,4 @@
[plugin]
description = "dokku core builder-nixpacks plugin"
version = "0.31.4"
[plugin.config]

View File

@@ -0,0 +1,14 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-builder-nixpacks-post-app-clone-setup() {
declare desc="sets up properties for new app"
declare trigger="post-app-clone-setup"
declare OLD_APP="$1" NEW_APP="$2"
fn-plugin-property-clone "builder-nixpacks" "$OLD_APP" "$NEW_APP"
}
trigger-builder-nixpacks-post-app-clone-setup "$@"

View File

@@ -0,0 +1,15 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-builder-nixpacks-post-app-rename-setup() {
declare desc="updates settings for new app"
declare trigger="post-app-rename-setup"
declare OLD_APP="$1" NEW_APP="$2"
fn-plugin-property-clone "builder-nixpacks" "$OLD_APP" "$NEW_APP"
fn-plugin-property-destroy "builder-nixpacks" "$OLD_APP"
}
trigger-builder-nixpacks-post-app-rename-setup "$@"

View File

@@ -0,0 +1,15 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-builder-nixpacks-post-delete() {
declare desc="destroys the builder-nixpacks properties for a given app"
declare trigger="post-delete"
declare APP="$1"
fn-plugin-property-destroy "builder-nixpacks" "$APP"
rm -rf "${DOKKU_LIB_ROOT}/data/builder-nixpacks/$APP"
}
trigger-builder-nixpacks-post-delete "$@"

View File

@@ -0,0 +1,6 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-builder-nixpacks-report-single "$@"

View File

@@ -0,0 +1,6 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/help-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-builder-nixpacks-help "builder-nixpacks:help"

View File

@@ -0,0 +1,6 @@
#!/usr/bin/env bash
source "$PLUGIN_AVAILABLE_PATH/builder-nixpacks/internal-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-builder-nixpacks-report "$@"

View File

@@ -0,0 +1,30 @@
#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
source "$PLUGIN_CORE_AVAILABLE_PATH/common/property-functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
cmd-builder-nixpacks-set() {
declare desc="set or clear a builder-nixpacks property for an app"
declare cmd="builder-nixpacks:set"
[[ "$1" == "$cmd" ]] && shift 1
declare APP="$1" KEY="$2" VALUE="$3"
local VALID_KEYS=("nixpackstoml-path" "no-cache")
[[ "$APP" == "--global" ]] || verify_app_name "$APP"
[[ -z "$KEY" ]] && dokku_log_fail "No key specified"
if ! fn-in-array "$KEY" "${VALID_KEYS[@]}"; then
dokku_log_fail "Invalid key specified, valid keys include: nixpackstoml-path, no-cache"
fi
if [[ -n "$VALUE" ]]; then
dokku_log_info2_quiet "Setting ${KEY} to ${VALUE}"
fn-plugin-property-write "builder-nixpacks" "$APP" "$KEY" "$VALUE"
else
dokku_log_info2_quiet "Unsetting ${KEY}"
fn-plugin-property-delete "builder-nixpacks" "$APP" "$KEY"
fi
}
cmd-builder-nixpacks-set "$@"