Skip to content

Commit

Permalink
Container building pipeline supports git tags
Browse files Browse the repository at this point in the history
  • Loading branch information
mquhuy committed Nov 27, 2023
1 parent 3d8ac03 commit 58c676a
Showing 1 changed file with 42 additions and 23 deletions.
65 changes: 42 additions & 23 deletions scripts/build-container-image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,26 @@ set -o pipefail
HUB="${CONTAINER_IMAGE_HUB:-quay.io}"
HUB_ORG="${CONTAINER_IMAGE_HUB_ORG:-metal3-io}"
IMAGE_NAME=${BUILD_CONTAINER_IMAGE_NAME:-${1}}
BRANCH_NAME=${BUILD_CONTAINER_IMAGE_BRANCH:-main}
KEEP_TAGS=${BUILD_CONTAINER_IMAGE_KEEP_TAGS:-3}
GIT_REFERENCE=${BUILD_CONTAINER_IMAGE_BRANCH:-main}

case "${GIT_REFERENCE}" in
*"refs/heads"*)
GIT_REFERENCE_TYPE="branch"
GIT_REFERENCE=$(echo "${GIT_REFERENCE}" | sed 's/refs\/heads\///')
echo "Detected branch name: ${GIT_REFERENCE}"
;;
*"refs/tags"*)
GIT_REFERENCE_TYPE="tags"
GIT_REFERENCE=$(echo "${GIT_REFERENCE}" | sed 's/refs\/tags\///')
echo "Detected tag name: ${GIT_REFERENCE}"
;;
*)
GIT_REFERENCE_TYPE="branch"
echo "No reference type detected. Treating as a branch name: ${GIT_REFERENCE}"
;;
esac

KEEP_TAGS=${BUILD_CONTAINER_IMAGE_KEEP_TAGS:-10}
REPO_LOCATION="/tmp/metal3-io"
NEEDED_TOOLS=("git" "curl" "docker" "jq")
__dir__=$(realpath "$(dirname "$0")")
Expand Down Expand Up @@ -47,22 +65,17 @@ git_get_current_commit_short_hash() {
git rev-parse --short HEAD
}

git_get_current_branch() {
git rev-parse --abbrev-ref HEAD
}

get_date() {
# Should this be current date, or latest commit date?
date +%Y%m%d
}

get_image_tag() {
echo "$(git_get_current_branch)_$(get_date)_$(git_get_current_commit_short_hash)"
}

cleanup_old_tags() {
all_tags=$(list_tags)
branch=$(git_get_current_branch)
if [[ "${GIT_REFERENCE_TYPE}" != "branch" ]]; then
return
fi
all_tags=($(list_tags))
branch="${GIT_REFERENCE}"

declare -a branch_tags=()

Expand All @@ -81,24 +94,30 @@ cleanup_old_tags() {
fi
}

get_image_latest_tag() {
echo "$(git_get_current_branch)_latest"
}

get_image_path() {
image_tag=${1:?}
echo "${HUB}/${HUB_ORG}/${IMAGE_NAME}:${image_tag}"
}

build_container_image() {
image_tag=$(get_image_tag)
image_latest_tag=$(get_image_latest_tag)
image_path=$(get_image_path "${image_tag}")
image_latest_path=$(get_image_path "${image_latest_tag}")
image_path=$(get_image_path "${GIT_REFERENCE}")
echo "Building the image as ${image_path}"
docker build -t "${image_path}" .
docker push "${image_path}"
docker tag "${image_path}" "${image_latest_path}"
docker push "${image_latest_path}"
if [[ "${GIT_REFERENCE_TYPE}" != "branch" ]]; then
return
fi
# If the image was built for a branch, we include some more tags
image_tags=("${GIT_REFERENCE}_$(get_date)_$(git_get_current_commit_short_hash)")
if [[ "${GIT_REFERENCE}" == "main" ]]; then
image_tags+=("latest")
fi
for image_tag in "${image_tags[@]}"; do
new_image_path=$(get_image_path "${image_tag}")
echo "Tagging the image as ${new_image_path}"
docker tag "${image_path}" "${new_image_path}"
docker push "${new_image_path}"
done
}

build_image() {
Expand All @@ -114,7 +133,7 @@ build_image() {
git clone "${repo_link}" "${repo_name}"
dockerfile_directory=$(jq < "${IMAGES_JSON}" -r .\""${IMAGE_NAME}"\".dockerfile_location)
cd "${repo_name}"
git checkout "${BRANCH_NAME}"
git checkout "${GIT_REFERENCE}"
cd "${REPO_LOCATION}/${repo_name}${dockerfile_directory}"
build_container_image
}
Expand Down

0 comments on commit 58c676a

Please # to comment.