Files
dokku/plugins/builder-lambda/builder-build
Jose Diaz-Gonzalez f8ccf52079 refactor: only use detected port mapping if override is not specified
Previously, we would always set the port mapping during a dockerfile build, making it difficult for users to override mappings. We also only _sometimes_ updated the detected port mapping, further confusing issues when users were migrating from Dockerfile to Buildpacks for builds.

Now, we always detect the port mapping during the build process, and only use that detected port mapping if an override is not specified. This greatly simplifies the experience around port mapping, as now a user can create an app, set a port mapping, and that first deploy will respect the port mapping without an additional deploy.

The builder always has the best context for what the app should be listening on, and thus we can always specify a "default" port mapping at this stage. Users can override this map as desired later.

This change also results in the removal of a ton of internal code that is now centralized in the ports plugin.

Closes #4067
2023-08-05 10:58:57 -04:00

46 lines
1.8 KiB
Bash
Executable File

#!/usr/bin/env bash
source "$PLUGIN_CORE_AVAILABLE_PATH/common/functions"
set -eo pipefail
[[ $DOKKU_TRACE ]] && set -x
trigger-builder-lambda-builder-build() {
declare desc="builder-lambda builder-build plugin trigger"
declare trigger="builder-build"
declare BUILDER_TYPE="$1" APP="$2" SOURCECODE_WORK_DIR="$3"
if [[ "$BUILDER_TYPE" != "lambda" ]]; then
return
fi
dokku_log_info1 "Building $APP from lambda"
local IMAGE=$(get_app_image_name "$APP")
pushd "$SOURCECODE_WORK_DIR" &>/dev/null
plugn trigger pre-build-lambda "$APP"
lambda-builder build --generate-image --write-procfile --image-env=DOCKER_LAMBDA_STAY_OPEN=1 --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=lambda "--label=com.dokku.app-name=$APP" $DOKKU_GLOBAL_BUILD_ARGS --port 5000 --tag "$IMAGE" --working-directory "$SOURCECODE_WORK_DIR"
if [[ ! -f "$SOURCECODE_WORK_DIR/lambda.zip" ]]; then
dokku_log_warn "Compressed lambda.zip not detected, failed to build lambda function"
return 1
fi
local GIT_REV="$(plugn trigger git-revision "$APP")"
mkdir -p "${DOKKU_LIB_ROOT}/data/builder-lambda/$APP"
pushd "${DOKKU_LIB_ROOT}/data/builder-lambda/$APP" >/dev/null
rm -f -- *.zip
popd &>/dev/null || pushd "/tmp" >/dev/null
cp "$SOURCECODE_WORK_DIR/lambda.zip" "${DOKKU_LIB_ROOT}/data/builder-lambda/$APP/$GIT_REV.zip"
if [[ -f "$SOURCECODE_WORK_DIR/Procfile" ]]; then
cp "$SOURCECODE_WORK_DIR/Procfile" "${DOKKU_LIB_ROOT}/data/builder-lambda/$APP/$GIT_REV.Procfile"
fi
# ensure we have a port mapping
plugn trigger ports-configure "$APP"
plugn trigger ports-set-detected "$APP" "http:$(plugn trigger ports-get-property "$APP" proxy-port):5000"
plugn trigger post-build-lambda "$APP"
}
trigger-builder-lambda-builder-build "$@"