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

refactor build_deploy.sh #893

Merged
merged 5 commits into from
Nov 17, 2023
Merged
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
114 changes: 75 additions & 39 deletions build_deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,90 @@

set -exv

IMAGE="quay.io/cloudservices/clowder"
IMAGE_TAG=$(git rev-parse --short=8 HEAD)
SECURITY_COMPLIANCE_TAG="sc-$(date +%Y%m%d)-$(git rev-parse --short=8 HEAD)"
CICD_BOOTSTRAP_URL='https://raw.githubusercontent.com/RedHatInsights/cicd-tools/main/src/bootstrap.sh'
# shellcheck source=/dev/null
source <(curl -sSL "$CICD_BOOTSTRAP_URL") image_builder

if [[ -z "$QUAY_USER" || -z "$QUAY_TOKEN" ]]; then
echo "QUAY_USER and QUAY_TOKEN must be set"
exit 1
fi
get_base_image_tag() {

if [[ -z "$RH_REGISTRY_USER" || -z "$RH_REGISTRY_TOKEN" ]]; then
echo "RH_REGISTRY_USER and RH_REGISTRY_TOKEN must be set"
exit 1
fi
local tag

BASE_TAG=`cat go.mod go.sum Dockerfile.base | sha256sum | head -c 8`
BASE_IMG=quay.io/cloudservices/clowder-base:$BASE_TAG
tag=$(cat "${BASE_IMAGE_FILES[@]}" | sha256sum | head -c 8)

DOCKER_CONF="$PWD/.docker"
mkdir -p "$DOCKER_CONF"
docker --config="$DOCKER_CONF" login -u="$QUAY_USER" -p="$QUAY_TOKEN" quay.io
docker --config="$DOCKER_CONF" login -u="$RH_REGISTRY_USER" -p="$RH_REGISTRY_TOKEN" registry.redhat.io
if _base_image_files_changed; then
CICD_IMAGE_BUILDER_IMAGE_TAG="$tag"
tag=$(cicd::image_builder::get_image_tag)
fi

RESPONSE=$( \
curl -Ls -H "Authorization: Bearer $QUAY_API_TOKEN" \
"https://quay.io/api/v1/repository/cloudservices/clowder-base/tag/?specificTag=$BASE_TAG" \
)
echo -n "$tag"
}

echo "received HTTP response: $RESPONSE"
base_image_tag_exists() {

# find all non-expired tags
VALID_TAGS_LENGTH=$(echo $RESPONSE | jq '[ .tags[] | select(.end_ts == null) ] | length')
local tag="$1"
local repository="cloudservices/clowder-base"

if [[ "$VALID_TAGS_LENGTH" -eq 0 ]]; then
docker --config="$DOCKER_CONF" build -f Dockerfile.base . -t "$BASE_IMG"
docker --config="$DOCKER_CONF" push "$BASE_IMG"
fi
response=$(curl -sSL \
"https://quay.io/api/v1/repository/${repository}/tag/?specificTag=${tag}&onlyActiveTags=true")

# If the "security-compliance" branch is used for the build, it will tag the image as such.
if [[ "$GIT_BRANCH" == "origin/security-compliance" ]]; then
IMAGE_TAG="$SECURITY_COMPLIANCE_TAG"
fi
echo "received HTTP response: ${response}"

# find all non-expired tags
[[ 1 -eq $(jq '.tags | length' <<<"$response") ]]
}

build_base_image() {

export CICD_IMAGE_BUILDER_IMAGE_NAME="$BASE_IMAGE_NAME"
export CICD_IMAGE_BUILDER_IMAGE_TAG="$BASE_IMAGE_TAG"
export CICD_IMAGE_BUILDER_CONTAINERFILE_PATH="Dockerfile.base"

cicd::image_builder::build_and_push
}

_base_image_files_changed() {

local target_branch=${ghprbTargetBranch:-master}

make update-version
docker --config="$DOCKER_CONF" build --platform linux/amd64 --build-arg BASE_IMAGE="$BASE_IMG" -t "${IMAGE}:${IMAGE_TAG}-amd64" --push .
docker --config="$DOCKER_CONF" build --platform linux/arm64 --build-arg BASE_IMAGE="$BASE_IMG" -t "${IMAGE}:${IMAGE_TAG}-arm64" --push .
# Use git to check for any non staged differences in the Base Image files
! git diff --quiet "$target_branch" -- "${BASE_IMAGE_FILES[@]}"
}

docker --config="$DOCKER_CONF" manifest create "${IMAGE}:${IMAGE_TAG}" \
"${IMAGE}:${IMAGE_TAG}-amd64" \
"${IMAGE}:${IMAGE_TAG}-arm64"
build_main_image() {

docker --config="$DOCKER_CONF" manifest push "${IMAGE}:${IMAGE_TAG}"
export CICD_IMAGE_BUILDER_BUILD_ARGS=("BASE_IMAGE=${BASE_IMAGE_NAME}:${BASE_IMAGE_TAG}")
export CICD_IMAGE_BUILDER_IMAGE_NAME="quay.io/cloudservices/clowder"
CICD_IMAGE_BUILDER_IMAGE_TAG=$(git rev-parse --short=8 HEAD)

# If the "security-compliance" branch is used for the build, it will tag the image as such.
if [[ "$GIT_BRANCH" == "origin/security-compliance" ]]; then
CICD_IMAGE_BUILDER_IMAGE_TAG="sc-$(date +%Y%m%d)-${CICD_IMAGE_BUILDER_IMAGE_TAG}"
fi

export CICD_IMAGE_BUILDER_IMAGE_TAG

cicd::image_builder::build_and_push
}

BASE_IMAGE_FILES=("go.mod" "go.sum" "Dockerfile.base")
BASE_IMAGE_NAME='quay.io/cloudservices/clowder-base'
BASE_IMAGE_TAG=$(get_base_image_tag)

if base_image_tag_exists "$BASE_IMAGE_TAG"; then
echo "Base image exists, skipping..."
else
if ! build_base_image; then
echo "Error building base image!"
exit 1
fi
fi

if ! make update-version; then
echo "Error updating version!"
exit 1
fi

if ! build_main_image; then
echo "Error building image!"
exit 1
fi
Loading