Skip to content

Commit

Permalink
Container Image Build Fix - Allow for Push to a different container r…
Browse files Browse the repository at this point in the history
…egistry while building from GitHub (#116)

* add build-from-github to build_app and build_containerImage

* additional logging

---------

Co-authored-by: alexlianides <[email protected]>
  • Loading branch information
alexlianides and alexlianides authored Oct 15, 2024
1 parent d24db94 commit 7d74aa4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
20 changes: 18 additions & 2 deletions build/build_containerImage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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}'";
Expand All @@ -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}\" "
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions build/dotnet/build_app.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ DEVCONTAINER_JSON_FILE=".devcontainer/devcontainer.json"
APP_NAME=""
CONTAINER_ID=""
DEVCONTAINER_JSON=""
BUILD_FROM_GITHUB=false
PUSH_ENABLED=true
############################################################
# Help #
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 \
Expand Down

0 comments on commit 7d74aa4

Please sign in to comment.