Skip to content

Commit

Permalink
chore(ci): improve code readability
Browse files Browse the repository at this point in the history
Signed-off-by: Raphanus Lo <yunchang.lo@suse.com>
  • Loading branch information
COLDTURNIP committed Feb 11, 2025
1 parent 7a852a6 commit 35b6a6f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 37 deletions.
12 changes: 5 additions & 7 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
outputs:
version_major: ${{ steps.build_info.outputs.version_major }}
version_minor: ${{ steps.build_info.outputs.version_minor }}
version_build: ${{ steps.build_info.outputs.version_build }}
version_patch: ${{ steps.build_info.outputs.version_patch }}
image_tag: ${{ steps.build_info.outputs.image_tag }}

steps:
Expand All @@ -26,7 +26,7 @@ jobs:
run: |
version_major=''
version_minor=''
version_build=''
version_patch=''
image_tag=''
branch=${GITHUB_HEAD_REF:-${GITHUB_REF#refs/heads/}}
Expand All @@ -35,23 +35,21 @@ jobs:
version=$(sed -E 's/^v([0-9]*\.[0-9]*\.[0-9]*).*$/\1/' <<<${{ github.ref_name }} )
version_major=$(cut -d. -f1 <<<$version)
version_minor=$(cut -d. -f2 <<<$version)
version_build=$(cut -d. -f3 <<<$version)
version_patch=$(cut -d. -f3 <<<$version)
image_tag=${{ github.ref_name }}
elif [[ "$ref" =~ 'refs/heads/' ]]; then
image_tag="${branch}-head"
elif [[ "$ref" =~ 'refs/pull/' ]]; then
image_tag="pr${{ github.event.number }}"
fi
echo "version_major=${version_major}" >>$GITHUB_OUTPUT
echo "version_minor=${version_minor}" >>$GITHUB_OUTPUT
echo "version_build=${version_build}" >>$GITHUB_OUTPUT
echo "version_patch=${version_patch}" >>$GITHUB_OUTPUT
echo "image_tag=${image_tag}" >>$GITHUB_OUTPUT
cat <<EOF
version_major=${version_major}
version_minor=${version_minor}
version_build=${version_build}
version_patch=${version_patch}
image_tag=${image_tag}
EOF
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ buildx-machine:
# - IID_FILE_FLAG: optional, options to generate image ID file
.PHONY: workflow-image-build-push workflow-image-build-push-secure
workflow-image-build-push: buildx-machine
MACHINE=$(MACHINE) OUTPUT_ARGS='--push' bash scripts/package
MACHINE=$(MACHINE) PUSH='true' bash scripts/package
workflow-image-build-push-secure: buildx-machine
MACHINE=$(MACHINE) OUTPUT_ARGS='--push' IS_SECURE=true bash scripts/package
MACHINE=$(MACHINE) PUSH='true' IS_SECURE=true bash scripts/package

trash: .dapper
./.dapper -m bind trash
Expand Down
62 changes: 34 additions & 28 deletions scripts/package
Original file line number Diff line number Diff line change
Expand Up @@ -5,53 +5,59 @@ source $(dirname $0)/version

cd $(dirname $0)/..

project=$(basename "$PWD")
PROJECT=$(basename "${PWD}")

command -v buildx >/dev/null && build_cmd=(buildx) || build_cmd=(docker buildx)
command -v buildx >/dev/null && BUILD_CMD=(buildx) || BUILD_CMD=(docker buildx)

# read configurable parameters
REPO=${REPO:-longhornio}
IMAGE_NAME=${IMAGE_NAME:-$project}
IMAGE_NAME=${IMAGE_NAME:-${PROJECT}}
TAG=${TAG:-''}
OUTPUT_ARGS=${OUTPUT_ARGS:-'--load'}
PUSH=${PUSH:-'false'}
IS_SECURE=${IS_SECURE:-'false'}
MACHINE=${MACHINE:-''}
TARGET_PLATFORMS=${TARGET_PLATFORMS:-''}
IID_FILE=${IID_FILE:-''}
IID_FILE_FLAG=${IID_FILE_FLAG:-''}

if [[ -z $TAG ]]; then
if api_version=$(./bin/backing-image-manager version --client-only | jq ".clientVersion.backingImageManagerAPIVersion"); then
TAG="v${api_version}_$(date -u +%Y%m%d)"
if [[ -z "${TAG}" ]]; then
if API_VERSION=$(./bin/backing-image-manager version --client-only | jq ".clientVersion.backingImageManagerAPIVersion"); then
TAG="v${API_VERSION}_$(date -u +%Y%m%d)"
else
TAG="$VERSION"
TAG="${VERSION}"
fi
fi

image="${REPO}/${IMAGE_NAME}:${TAG}"
IMAGE="${REPO}/${IMAGE_NAME}:${TAG}"

builder_args=()
[[ $MACHINE ]] && builder_args+=('--builder' "$MACHINE")
BUILDER_ARGS=()
[[ ${MACHINE} ]] && BUILDER_ARGS+=('--builder' "${MACHINE}")

IFS=' ' read -r -a iid_file_args <<<"$IID_FILE_FLAG"
[[ -n "$IID_FILE" && ${#iid_file_args} == 0 ]] && iid_file_args=('--iidfile' "$IID_FILE")
IFS=' ' read -r -a IID_FILE_ARGS <<<"${IID_FILE_FLAG}"
[[ -n "${IID_FILE}" && ${#IID_FILE_ARGS} == 0 ]] && IID_FILE_ARGS=('--iidfile' "${IID_FILE}")

IFS=' ' read -r -a buildx_args <<<"$OUTPUT_ARGS"
[[ $IS_SECURE == 'true' ]] && buildx_args+=('--sbom=true' '--attest' 'type=provenance,mode=max')
[[ $TARGET_PLATFORMS ]] && buildx_args+=('--platform' "$TARGET_PLATFORMS")
BUILDX_ARGS=()

echo "${build_cmd[@]}" build --no-cache \
"${builder_args[@]}" \
"${iid_file_args[@]}" \
"${buildx_args[@]}" \
-t "$image" -f package/Dockerfile .
"${build_cmd[@]}" build --no-cache \
"${builder_args[@]}" \
"${iid_file_args[@]}" \
"${buildx_args[@]}" \
-t "$image" -f package/Dockerfile .
if [[ "${PUSH}" == 'true' ]]; then
BUILDX_ARGS+=('--push')
else
BUILDX_ARGS+=('--load')
fi

[[ ${IS_SECURE} == 'true' ]] && BUILDX_ARGS+=('--sbom=true' '--attest' 'type=provenance,mode=max')
[[ ${TARGET_PLATFORMS} ]] && BUILDX_ARGS+=('--platform' "${TARGET_PLATFORMS}")

IMAGE_BUILD_CMD=(
"${BUILD_CMD[@]}" build --no-cache \
"${BUILDER_ARGS[@]}" \
"${IID_FILE_ARGS[@]}" \
"${BUILDX_ARGS[@]}" \
-t "${IMAGE}" -f package/Dockerfile .
)
echo "${IMAGE_BUILD_CMD[@]}"
"${IMAGE_BUILD_CMD[@]}"

echo "Built $image"
echo "Built ${IMAGE}"

mkdir ./bin || true
echo "$image" > ./bin/latest_image
echo "${IMAGE}" > ./bin/latest_image

0 comments on commit 35b6a6f

Please sign in to comment.