132 lines
3.0 KiB
Bash
132 lines
3.0 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
set -euo pipefail
|
|
|
|
resolve_inputs() {
|
|
local service=""
|
|
local target_env=""
|
|
local image_tag=""
|
|
local image_name=""
|
|
local kustomization=""
|
|
|
|
if [[ "${GITHUB_EVENT_NAME:-}" == "workflow_dispatch" ]]; then
|
|
service="${INPUT_SERVICE:-}"
|
|
target_env="${INPUT_TARGET_ENV:-}"
|
|
image_tag="${INPUT_IMAGE_TAG:-}"
|
|
else
|
|
service="${EVENT_SERVICE:-}"
|
|
target_env="${EVENT_TARGET_ENV:-}"
|
|
image_tag="${EVENT_IMAGE_TAG:-}"
|
|
fi
|
|
|
|
if [[ -z "$service" || -z "$target_env" || -z "$image_tag" ]]; then
|
|
echo "Missing service/target_env/image_tag input" >&2
|
|
exit 1
|
|
fi
|
|
|
|
case "$service" in
|
|
auth-server)
|
|
image_name="ghcr.io/donghyeonka/project-auth-server"
|
|
;;
|
|
api-server)
|
|
image_name="ghcr.io/donghyeonka/project-api-server"
|
|
;;
|
|
*)
|
|
echo "Unsupported service: $service" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
case "$target_env" in
|
|
dev|prod)
|
|
;;
|
|
*)
|
|
echo "Unsupported environment: $target_env" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
|
|
if [[ ! "$image_tag" =~ ^[A-Za-z0-9][A-Za-z0-9._-]{0,127}$ ]]; then
|
|
echo "Unsupported image tag format: $image_tag" >&2
|
|
exit 1
|
|
fi
|
|
|
|
kustomization="apps/${service}/overlays/${target_env}/kustomization.yaml"
|
|
|
|
{
|
|
echo "SERVICE=$service"
|
|
echo "TARGET_ENV=$target_env"
|
|
echo "IMAGE_TAG=$image_tag"
|
|
echo "IMAGE_NAME=$image_name"
|
|
echo "KUSTOMIZATION=$kustomization"
|
|
} >> "$GITHUB_ENV"
|
|
}
|
|
|
|
update_kustomization() {
|
|
if ! command -v kustomize >/dev/null 2>&1; then
|
|
echo "kustomize is required to update the image tag" >&2
|
|
exit 1
|
|
fi
|
|
|
|
if [[ ! -f "${KUSTOMIZATION:-}" ]]; then
|
|
echo "Kustomization not found: ${KUSTOMIZATION:-}" >&2
|
|
exit 1
|
|
fi
|
|
|
|
cd "$(dirname "$KUSTOMIZATION")"
|
|
kustomize edit set image "${IMAGE_NAME}=${IMAGE_NAME}:${IMAGE_TAG}"
|
|
}
|
|
|
|
validate_overlay() {
|
|
if ! command -v kubectl >/dev/null 2>&1; then
|
|
echo "kubectl is required to validate the overlay" >&2
|
|
exit 1
|
|
fi
|
|
|
|
kubectl kustomize "$(dirname "$KUSTOMIZATION")" >/dev/null
|
|
}
|
|
|
|
check_changes() {
|
|
if git diff --quiet -- "$KUSTOMIZATION"; then
|
|
echo "changed=false" >> "$GITHUB_OUTPUT"
|
|
echo "No changes to commit"
|
|
else
|
|
echo "changed=true" >> "$GITHUB_OUTPUT"
|
|
git diff -- "$KUSTOMIZATION"
|
|
fi
|
|
}
|
|
|
|
commit_dev() {
|
|
git config user.name "github-actions[bot]"
|
|
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
|
|
git add "$KUSTOMIZATION"
|
|
git commit -m "chore(gitops): update ${SERVICE} ${TARGET_ENV} image to ${IMAGE_TAG}"
|
|
git push origin "HEAD:${DEFAULT_BRANCH}"
|
|
}
|
|
|
|
main() {
|
|
case "${1:-}" in
|
|
resolve-inputs)
|
|
resolve_inputs
|
|
;;
|
|
update-kustomization)
|
|
update_kustomization
|
|
;;
|
|
validate-overlay)
|
|
validate_overlay
|
|
;;
|
|
check-changes)
|
|
check_changes
|
|
;;
|
|
commit-dev)
|
|
commit_dev
|
|
;;
|
|
*)
|
|
echo "Usage: scripts/ci/update-image-tag.sh <resolve-inputs|update-kustomization|validate-overlay|check-changes|commit-dev>" >&2
|
|
exit 1
|
|
;;
|
|
esac
|
|
}
|
|
|
|
main "$@"
|