Skip to content

Commit 7d74aa4

Browse files
alexlianidesalexlianides
and
alexlianides
authored
Container Image Build Fix - Allow for Push to a different container registry while building from GitHub (#116)
* add build-from-github to build_app and build_containerImage * additional logging --------- Co-authored-by: alexlianides <alianides@microsoft.com>
1 parent d24db94 commit 7d74aa4

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

build/build_containerImage.sh

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ REPO_DIR=""
2828
BUILD_ARGS=""
2929
EXTRA_PKGS=""
3030
ANNOTATION_CONFIG=""
31+
BUILD_FROM_GITHUB=false
3132
BUILDDATE_VALUE=$(date -u +'%Y%m%dT%H%M%S')
3233
DEVCONTAINER_JSON_FILE=".devcontainer/devcontainer.json"
3334
SPACEFX_DEV_ENABLED=true
@@ -52,6 +53,7 @@ function show_help() {
5253
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"
5354
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."
5455
echo "--devcontainer-json [OPTIONAL] Change the path to the devcontainer.json file. Default is '.devcontainer/devcontainer.json' in the --repo-dir path"
56+
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"
5557
echo "--help | -h [OPTIONAL] Help script (this screen)"
5658
echo
5759
exit 1
@@ -116,6 +118,9 @@ while [[ "$#" -gt 0 ]]; do
116118
# Removing the trailing slash if there is one
117119
REPO_DIR=${REPO_DIR%/}
118120
;;
121+
--build-from-github)
122+
BUILD_FROM_GITHUB=true
123+
;;
119124
*) echo "Unknown parameter passed: $1"; show_help ;;
120125
esac
121126
shift
@@ -269,7 +274,7 @@ function build_prod_image_container(){
269274
info_log "...adding tag '${DEST_REPO}:${IMAGE_TAG}_${ARCHITECTURE}'";
270275
fullTagCmd+=" --tag ${DEST_REPO}:${IMAGE_TAG}_${ARCHITECTURE}"
271276

272-
info_log "...adding tag '${DEST_CONTAINER_REGISTRY}/:${IMAGE_TAG}'";
277+
info_log "...adding tag '${DEST_CONTAINER_REGISTRY}/${DEST_REPO}:${IMAGE_TAG}'";
273278
fullTagCmd+=" --tag ${DEST_CONTAINER_REGISTRY}/${DEST_REPO}:${IMAGE_TAG}"
274279

275280
info_log "...adding tag '${DEST_REPO}:${IMAGE_TAG}'";
@@ -278,7 +283,17 @@ function build_prod_image_container(){
278283
buildArgs+="--build-arg APP_NAME=\"${APP_NAME}\" "
279284
labelArgs="--label \"org.app_name=${APP_NAME}\" "
280285

281-
buildArgs+="--build-arg CONTAINER_REGISTRY=\"${DEST_CONTAINER_REGISTRY}\" "
286+
if [[ "${BUILD_FROM_GITHUB}" == true ]]; then
287+
info_log "BUILD_FROM_GITHUB is true. Building from microsoft maintined ghcr.io container image"
288+
# Extract the repositoryPrefix from the ghcr.io entry
289+
repository_prefix=$(jq -r '.config.containerRegistries[] | select(.url == "ghcr.io") | .repositoryPrefix' "${SPACEFX_DIR}/tmp/config/spacefx-config.json")
290+
BUILD_CONTAINER_REGISTRY="ghcr.io/${repository_prefix}"
291+
292+
write_parameter_to_log BUILD_CONTAINER_REGISTRY
293+
buildArgs+="--build-arg CONTAINER_REGISTRY=\"${BUILD_CONTAINER_REGISTRY}\" "
294+
else
295+
buildArgs+="--build-arg CONTAINER_REGISTRY=\"${DEST_CONTAINER_REGISTRY}\" "
296+
fi
282297

283298
buildArgs+="--build-arg APP_VERSION=\"${IMAGE_TAG}\" "
284299
labelArgs+="--label \"org.spacefx.app_version=${IMAGE_TAG}\" "
@@ -333,6 +348,7 @@ function main() {
333348
write_parameter_to_log APP_NAME
334349
write_parameter_to_log DOCKERFILE
335350
write_parameter_to_log REPO_DIR
351+
write_parameter_to_log BUILD_FROM_GITHUB
336352
write_parameter_to_log PUSH_ENABLED
337353

338354
for i in "${!BUILD_ARGS[@]}"; do

build/dotnet/build_app.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ DEVCONTAINER_JSON_FILE=".devcontainer/devcontainer.json"
2626
APP_NAME=""
2727
CONTAINER_ID=""
2828
DEVCONTAINER_JSON=""
29+
BUILD_FROM_GITHUB=false
2930
PUSH_ENABLED=true
3031
############################################################
3132
# Help #
@@ -45,6 +46,7 @@ function show_help() {
4546
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"
4647
echo "--no-container-build [OPTIONAL] Do not build a container image. This will only build nuget packages"
4748
echo "--devcontainer-json [OPTIONAL] Change the path to the devcontainer.json file. Default is '.devcontainer/devcontainer.json' in the --repo-dir path"
49+
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"
4850
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."
4951
echo "--help | -h [OPTIONAL] Help script (this screen)"
5052
echo
@@ -107,6 +109,9 @@ while [[ "$#" -gt 0 ]]; do
107109
shift
108110
OUTPUT_DIR=$1
109111
;;
112+
--build-from-github)
113+
BUILD_FROM_GITHUB=true
114+
;;
110115
*) echo "Unknown parameter passed: $1"; show_help ;;
111116
esac
112117
shift
@@ -404,6 +409,7 @@ function main() {
404409
write_parameter_to_log BUILDDATE_VALUE
405410
write_parameter_to_log CONTAINER_BUILD
406411
write_parameter_to_log ANNOTATION_CONFIG
412+
write_parameter_to_log BUILD_FROM_GITHUB
407413
write_parameter_to_log PUSH_ENABLED
408414

409415
if [[ -n "${ANNOTATION_CONFIG}" ]]; then
@@ -487,6 +493,8 @@ function main() {
487493
local extra_cmds=""
488494
[[ "${PUSH_ENABLED}" == false ]] && extra_cmds="${extra_cmds} --no-push"
489495

496+
[[ "${BUILD_FROM_GITHUB}" == true ]] && extra_cmds="${extra_cmds} --build-from-github"
497+
490498
run_a_script "${SPACEFX_DIR}/build/build_containerImage.sh \
491499
--dockerfile ${SPACEFX_DIR}/build/dotnet/Dockerfile.app-base \
492500
--image-tag ${APP_VERSION}_base \

0 commit comments

Comments
 (0)