Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Container Image Build Fix - Allow for Push to a different container registry while building from GitHub #116

Merged
merged 2 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading