| 
									
										
										
										
											2021-10-25 20:04:59 +02:00
										 |  |  | #!/bin/bash
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-08 22:21:28 +01:00
										 |  |  | ## Setup for building multiplatform images | 
					
						
							|  |  |  | ## | 
					
						
							|  |  |  | ## apt install qemu-user-static | 
					
						
							| 
									
										
										
										
											2021-11-03 19:54:52 +01:00
										 |  |  | ## docker run --rm --privileged multiarch/qemu-user-static --reset -p yes | 
					
						
							| 
									
										
										
										
											2021-11-08 22:21:28 +01:00
										 |  |  | ## docker buildx create --use --name cross | 
					
						
							|  |  |  | ## docker buildx inspect --bootstrap | 
					
						
							|  |  |  | ## docker buildx build --platform linux/amd64,linux/arm/v7 -t octoprint:latest --target run . | 
					
						
							| 
									
										
										
										
											2021-11-03 19:54:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-25 20:04:59 +02:00
										 |  |  | set -e | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-11-10 19:26:36 +01:00
										 |  |  | app=${1} | 
					
						
							|  |  |  | registry=${2} | 
					
						
							| 
									
										
										
										
											2021-10-25 20:04:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-09 01:26:35 +02:00
										 |  |  | # Set build parameters | 
					
						
							| 
									
										
										
										
											2021-12-30 19:38:08 -07:00
										 |  |  | platform="linux/amd64,linux/arm/v7,linux/arm64/v8" | 
					
						
							| 
									
										
										
										
											2021-10-28 21:06:32 +02:00
										 |  |  | dockerfile=docker/${app}/Dockerfile | 
					
						
							|  |  |  | context=$(echo -n ${dockerfile} | rev | cut -f2- -d'/' | rev) | 
					
						
							| 
									
										
										
										
											2021-10-25 20:04:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-09 01:26:35 +02:00
										 |  |  | # Get latest commitref from upstream repo | 
					
						
							| 
									
										
										
										
											2021-10-28 21:06:32 +02:00
										 |  |  | source=$(grep "ARG REPO" ${dockerfile} | sed -r 's/.*REPO=(.*)$/\1/g') | 
					
						
							|  |  |  | ref=$(git ls-remote ${source} HEAD | cut -f1) | 
					
						
							|  |  |  | shortref=$(echo -n ${ref} | cut -c 1-7) | 
					
						
							| 
									
										
										
										
											2021-10-25 20:04:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-09 01:26:35 +02:00
										 |  |  | # Set label Values | 
					
						
							|  |  |  | label_date=$(date --rfc-3339=seconds) | 
					
						
							|  |  |  | if [ "${CI}" == "true" ]; then | 
					
						
							|  |  |  |   label_author="${GITHUB_REPOSITORY_OWNER}" | 
					
						
							|  |  |  |   label_url="${GITHUB_SERVER_URL}/${GITHUB_REPOSITORY}" | 
					
						
							|  |  |  |   label_doc="${label_url}/blob/main/docker/${app}/README.md" | 
					
						
							|  |  |  |   label_src="${label_url}/blob/main/docker/${app}" | 
					
						
							|  |  |  | else | 
					
						
							|  |  |  |   label_author="$(whoami)" | 
					
						
							|  |  |  |   label_url="local" | 
					
						
							|  |  |  |   label_doc="local" | 
					
						
							|  |  |  |   label_src="local" | 
					
						
							|  |  |  | fi | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Colorful output | 
					
						
							| 
									
										
										
										
											2022-03-09 22:36:54 +01:00
										 |  |  | function log { | 
					
						
							|  |  |  |   echo -e "\033[0;36m## ${1} \033[0m" | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-28 21:06:32 +02:00
										 |  |  | ## Explicitly build Targets, except 'build' | 
					
						
							|  |  |  | for target in $(grep "FROM .* as" ${dockerfile} | sed -r 's/.*FROM.*as (.*)/\1/g' | grep -v build); do | 
					
						
							| 
									
										
										
										
											2021-10-25 20:04:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-28 21:06:32 +02:00
										 |  |  |   ## Append Target to Tag unless it is 'run' | 
					
						
							|  |  |  |   if [ "${target}" != "run" ]; then | 
					
						
							|  |  |  |     tag_extra="-${target}" | 
					
						
							|  |  |  |   fi | 
					
						
							| 
									
										
										
										
											2021-10-25 20:04:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-07-07 21:50:40 +02:00
										 |  |  |   ## latest | 
					
						
							| 
									
										
										
										
											2021-10-28 21:32:49 +02:00
										 |  |  |   if docker manifest inspect ${registry}${app}:${shortref}${tag_extra} > /dev/null; then | 
					
						
							| 
									
										
										
										
											2022-03-09 22:36:54 +01:00
										 |  |  |     log "## Image ${registry}${app}:${shortref}${tag_extra} already exists, nothing to do." | 
					
						
							| 
									
										
										
										
											2021-10-28 21:32:49 +02:00
										 |  |  |   else | 
					
						
							| 
									
										
										
										
											2022-07-07 21:50:40 +02:00
										 |  |  |     log "## Building latest Image ${registry}${app}:${shortref}${tag_extra}" | 
					
						
							| 
									
										
										
										
											2022-03-09 22:36:54 +01:00
										 |  |  |     docker buildx build \
 | 
					
						
							|  |  |  |       --build-arg VERSION=${ref} \
 | 
					
						
							|  |  |  |       --platform ${platform} \
 | 
					
						
							|  |  |  |       --tag ${registry}${app}:${shortref}${tag_extra} \
 | 
					
						
							|  |  |  |       --tag ${registry}${app}:nightly${tag_extra} \
 | 
					
						
							| 
									
										
										
										
											2022-07-07 21:50:40 +02:00
										 |  |  |       --tag ${registry}${app}:latest${tag_extra} \
 | 
					
						
							| 
									
										
										
										
											2022-07-09 01:26:35 +02:00
										 |  |  |       --label org.prind.image.created="${label_date}" \
 | 
					
						
							|  |  |  |       --label org.prind.image.authors="${label_author}" \
 | 
					
						
							|  |  |  |       --label org.prind.image.url="${label_url}" \
 | 
					
						
							|  |  |  |       --label org.prind.image.documentation="${label_doc}" \
 | 
					
						
							|  |  |  |       --label org.prind.image.source="${label_src}" \
 | 
					
						
							|  |  |  |       --label org.prind.image.version="${ref}" \
 | 
					
						
							| 
									
										
										
										
											2022-03-09 22:36:54 +01:00
										 |  |  |       --target ${target} \
 | 
					
						
							|  |  |  |       --push \
 | 
					
						
							|  |  |  |       ${context} | 
					
						
							| 
									
										
										
										
											2021-10-28 21:32:49 +02:00
										 |  |  |   fi | 
					
						
							| 
									
										
										
										
											2021-10-25 20:04:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-28 21:06:32 +02:00
										 |  |  |   ## Tags | 
					
						
							| 
									
										
										
										
											2022-01-21 08:22:07 +01:00
										 |  |  |   for tag in $(git -c 'versionsort.suffix=-' ls-remote --tags --sort='version:refname' --refs ${source} | tail -n3 | rev | cut -f1 -d'/' | rev); do | 
					
						
							| 
									
										
										
										
											2021-10-28 21:32:49 +02:00
										 |  |  |     if docker manifest inspect ${registry}${app}:${tag}${tag_extra} > /dev/null; then | 
					
						
							| 
									
										
										
										
											2022-03-09 22:36:54 +01:00
										 |  |  |       log "## Image ${registry}${app}:${tag}${tag_extra} already exists, nothing to do." | 
					
						
							| 
									
										
										
										
											2021-10-28 21:32:49 +02:00
										 |  |  |     else | 
					
						
							| 
									
										
										
										
											2022-03-09 22:36:54 +01:00
										 |  |  |       log "## Building Image for tagged release ${registry}${app}:${tag}${tag_extra}" | 
					
						
							|  |  |  |       docker buildx build \
 | 
					
						
							|  |  |  |         --build-arg VERSION=${tag} \
 | 
					
						
							|  |  |  |         --platform ${platform} \
 | 
					
						
							|  |  |  |         --tag ${registry}${app}:${tag}${tag_extra} \
 | 
					
						
							| 
									
										
										
										
											2022-07-09 01:26:35 +02:00
										 |  |  |         --label org.prind.image.created="${label_date}" \
 | 
					
						
							|  |  |  |         --label org.prind.image.authors="${label_author}" \
 | 
					
						
							|  |  |  |         --label org.prind.image.url="${label_url}" \
 | 
					
						
							|  |  |  |         --label org.prind.image.documentation="${label_doc}" \
 | 
					
						
							|  |  |  |         --label org.prind.image.source="${label_src}" \
 | 
					
						
							|  |  |  |         --label org.prind.image.version="${tag}" \
 | 
					
						
							| 
									
										
										
										
											2022-03-09 22:36:54 +01:00
										 |  |  |         --target ${target} \
 | 
					
						
							|  |  |  |         --push \
 | 
					
						
							|  |  |  |         ${context} | 
					
						
							| 
									
										
										
										
											2021-10-28 21:32:49 +02:00
										 |  |  |     fi | 
					
						
							| 
									
										
										
										
											2021-10-28 21:06:32 +02:00
										 |  |  |   done | 
					
						
							| 
									
										
										
										
											2021-10-25 20:04:59 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-10-28 21:06:32 +02:00
										 |  |  |   unset tag_extra | 
					
						
							| 
									
										
										
										
											2021-10-25 20:04:59 +02:00
										 |  |  | done |