diff --git a/build/build_containerImage.sh b/build/build_containerImage.sh index 92b1474..01dc237 100755 --- a/build/build_containerImage.sh +++ b/build/build_containerImage.sh @@ -28,6 +28,7 @@ REPO_DIR="" BUILD_ARGS="" EXTRA_PKGS="" ANNOTATION_CONFIG="" +BUILD_FROM_GITHUB=false BUILDDATE_VALUE=$(date -u +'%Y%m%dT%H%M%S') DEVCONTAINER_JSON_FILE=".devcontainer/devcontainer.json" SPACEFX_DEV_ENABLED=true @@ -52,6 +53,7 @@ function show_help() { echo "--no-spacefx-dev [OPTIONAL] Disable spacefx-dev feature provisioning if present. Useful in CI/CD pipelines to speed up builds that are coming from ./build/dotnet/build_app.sh" echo "--no-push [OPTIONAL] Do not push the built container image to the container registry. Useful to locally build and test a container image without pushing it to the registry." echo "--devcontainer-json [OPTIONAL] Change the path to the devcontainer.json file. Default is '.devcontainer/devcontainer.json' in the --repo-dir path" + echo "--build-from-github [OPTIONAL] If the destination container registry is different from ghcr.io/microsoft, this will force the container image to be built from the ghcr.io/microsoft container image" echo "--help | -h [OPTIONAL] Help script (this screen)" echo exit 1 @@ -116,6 +118,9 @@ while [[ "$#" -gt 0 ]]; do # Removing the trailing slash if there is one REPO_DIR=${REPO_DIR%/} ;; + --build-from-github) + BUILD_FROM_GITHUB=true + ;; *) echo "Unknown parameter passed: $1"; show_help ;; esac shift @@ -269,7 +274,7 @@ function build_prod_image_container(){ info_log "...adding tag '${DEST_REPO}:${IMAGE_TAG}_${ARCHITECTURE}'"; fullTagCmd+=" --tag ${DEST_REPO}:${IMAGE_TAG}_${ARCHITECTURE}" - info_log "...adding tag '${DEST_CONTAINER_REGISTRY}/:${IMAGE_TAG}'"; + info_log "...adding tag '${DEST_CONTAINER_REGISTRY}/${DEST_REPO}:${IMAGE_TAG}'"; fullTagCmd+=" --tag ${DEST_CONTAINER_REGISTRY}/${DEST_REPO}:${IMAGE_TAG}" info_log "...adding tag '${DEST_REPO}:${IMAGE_TAG}'"; @@ -278,7 +283,17 @@ function build_prod_image_container(){ buildArgs+="--build-arg APP_NAME=\"${APP_NAME}\" " labelArgs="--label \"org.app_name=${APP_NAME}\" " - buildArgs+="--build-arg CONTAINER_REGISTRY=\"${DEST_CONTAINER_REGISTRY}\" " + if [[ "${BUILD_FROM_GITHUB}" == true ]]; then + info_log "BUILD_FROM_GITHUB is true. Building from microsoft maintined ghcr.io container image" + # Extract the repositoryPrefix from the ghcr.io entry + repository_prefix=$(jq -r '.config.containerRegistries[] | select(.url == "ghcr.io") | .repositoryPrefix' "${SPACEFX_DIR}/tmp/config/spacefx-config.json") + BUILD_CONTAINER_REGISTRY="ghcr.io/${repository_prefix}" + + write_parameter_to_log BUILD_CONTAINER_REGISTRY + buildArgs+="--build-arg CONTAINER_REGISTRY=\"${BUILD_CONTAINER_REGISTRY}\" " + else + buildArgs+="--build-arg CONTAINER_REGISTRY=\"${DEST_CONTAINER_REGISTRY}\" " + fi buildArgs+="--build-arg APP_VERSION=\"${IMAGE_TAG}\" " labelArgs+="--label \"org.spacefx.app_version=${IMAGE_TAG}\" " @@ -333,6 +348,7 @@ function main() { write_parameter_to_log APP_NAME write_parameter_to_log DOCKERFILE write_parameter_to_log REPO_DIR + write_parameter_to_log BUILD_FROM_GITHUB write_parameter_to_log PUSH_ENABLED for i in "${!BUILD_ARGS[@]}"; do diff --git a/build/dotnet/build_app.sh b/build/dotnet/build_app.sh index 280f85d..8f0407d 100755 --- a/build/dotnet/build_app.sh +++ b/build/dotnet/build_app.sh @@ -26,6 +26,7 @@ DEVCONTAINER_JSON_FILE=".devcontainer/devcontainer.json" APP_NAME="" CONTAINER_ID="" DEVCONTAINER_JSON="" +BUILD_FROM_GITHUB=false PUSH_ENABLED=true ############################################################ # Help # @@ -45,6 +46,7 @@ function show_help() { echo "--nuget-project | -n [OPTIONAL] Relative path to a nuget project for the service (if applicable) from within the devcontainer. Will generate a nuget package in the output directory. Can be passed multiple times" echo "--no-container-build [OPTIONAL] Do not build a container image. This will only build nuget packages" echo "--devcontainer-json [OPTIONAL] Change the path to the devcontainer.json file. Default is '.devcontainer/devcontainer.json' in the --repo-dir path" + echo "--build-from-github [OPTIONAL] If the destination container registry is different from ghcr.io/microsoft, this will force the container image to be built from the ghcr.io/microsoft container image" echo "--no-push [OPTIONAL] Do not push the built container image to the container registry. Useful to locally build and test a container image without pushing it to the registry." echo "--help | -h [OPTIONAL] Help script (this screen)" echo @@ -107,6 +109,9 @@ while [[ "$#" -gt 0 ]]; do shift OUTPUT_DIR=$1 ;; + --build-from-github) + BUILD_FROM_GITHUB=true + ;; *) echo "Unknown parameter passed: $1"; show_help ;; esac shift @@ -404,6 +409,7 @@ function main() { write_parameter_to_log BUILDDATE_VALUE write_parameter_to_log CONTAINER_BUILD write_parameter_to_log ANNOTATION_CONFIG + write_parameter_to_log BUILD_FROM_GITHUB write_parameter_to_log PUSH_ENABLED if [[ -n "${ANNOTATION_CONFIG}" ]]; then @@ -487,6 +493,8 @@ function main() { local extra_cmds="" [[ "${PUSH_ENABLED}" == false ]] && extra_cmds="${extra_cmds} --no-push" + [[ "${BUILD_FROM_GITHUB}" == true ]] && extra_cmds="${extra_cmds} --build-from-github" + run_a_script "${SPACEFX_DIR}/build/build_containerImage.sh \ --dockerfile ${SPACEFX_DIR}/build/dotnet/Dockerfile.app-base \ --image-tag ${APP_VERSION}_base \