13493de7b2
CI Feature / Load example-gitea-env.conf to pipeline env (push) Successful in 22s
POC GitOps E2E / e2e (push) Successful in 53s
unit-tests Bats test report
CI Feature / Bats tests (push) Failing after 1m35s
acc-tests Cucumber test report
CI Feature / Cucumber tests (push) Failing after 1m35s
CI Feature / Report Summary (push) Successful in 6s
78 lines
2.8 KiB
Bash
Executable File
78 lines
2.8 KiB
Bash
Executable File
#!/usr/bin/env bash
|
|
set -euo pipefail
|
|
|
|
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
|
|
|
_gitops_fail() {
|
|
local MSG="${1:-GitOps update failed}"
|
|
echo "[ERROR] ${MSG}" >&2
|
|
|
|
if [ -n "${GITOPS_REPO:-}" ] && [ -n "${GITOPS_SHA:-}" ] && \
|
|
[ -n "${SOURCE_REPO:-}" ] && [ -n "${SOURCE_COMMIT:-}" ] && \
|
|
[ -n "${GITEA_API_URL:-}" ] && [ -n "${GITEA_TOKEN:-}" ]; then
|
|
local SOURCE_URL="${GITEA_API_URL}/${SOURCE_REPO}/commits/${SOURCE_COMMIT}"
|
|
ROOT_REPO="${GITOPS_REPO}" ROOT_COMMIT="${GITOPS_SHA}" \
|
|
GITEA_API_URL="${GITEA_API_URL}" GITEA_TOKEN="${GITEA_TOKEN}" \
|
|
bash "${SCRIPT_DIR}/report-status.sh" failure "${MSG}" \
|
|
"source/${SOURCE_REPO}" "" "${SOURCE_URL}" 2>/dev/null || true
|
|
fi
|
|
|
|
exit 1
|
|
}
|
|
|
|
_gitops_validate() {
|
|
[ -n "${INPUT_FILE:-}" ] || _gitops_fail "INPUT_FILE is required"
|
|
[ -n "${YQ_TPL:-}" ] || _gitops_fail "YQ_TPL is required"
|
|
[ -n "${VERSION:-}" ] || _gitops_fail "VERSION is required"
|
|
[ -n "${SOURCE_REPO:-}" ] || _gitops_fail "SOURCE_REPO is required"
|
|
[ -n "${SOURCE_COMMIT:-}" ] || _gitops_fail "SOURCE_COMMIT is required"
|
|
[ -n "${GITOPS_REPO:-}" ] || _gitops_fail "GITOPS_REPO is required"
|
|
[ -n "${GITEA_TOKEN:-}" ] || _gitops_fail "GITEA_TOKEN is required"
|
|
[ -n "${GITEA_API_URL:-}" ] || _gitops_fail "GITEA_API_URL is required"
|
|
}
|
|
|
|
_gitops_success() {
|
|
local SOURCE_URL="${GITEA_API_URL}/${SOURCE_REPO}/commits/${SOURCE_COMMIT}"
|
|
|
|
ROOT_REPO="${GITOPS_REPO}" ROOT_COMMIT="${GITOPS_SHA}" \
|
|
GITEA_API_URL="${GITEA_API_URL}" GITEA_TOKEN="${GITEA_TOKEN}" \
|
|
bash "${SCRIPT_DIR}/report-status.sh" success "Source build" \
|
|
"source/${SOURCE_REPO}" "" "${SOURCE_URL}"
|
|
}
|
|
|
|
_gitops_substitute() {
|
|
echo "$1" | sed "s/{{VERSION}}/$2/g"
|
|
}
|
|
|
|
_gitops_update() {
|
|
local CLONE_DIR="${GITOPS_TARGET_DIR:-$(mktemp -d)}"
|
|
|
|
if [ -n "${GITOPS_CLONE_URL:-}" ]; then
|
|
git clone "${GITOPS_CLONE_URL}" "${CLONE_DIR}" || _gitops_fail "Failed to clone GitOps repo"
|
|
else
|
|
git clone "${CLONE_URL}" "${CLONE_DIR}" || _gitops_fail "Failed to clone GitOps repo"
|
|
fi
|
|
|
|
cd "${CLONE_DIR}" || _gitops_fail "Failed to enter clone directory"
|
|
yq eval -i "${YQ_EXPR}" "${INPUT_FILE}" || _gitops_fail "Failed to update ${INPUT_FILE}"
|
|
git add "${INPUT_FILE}" || _gitops_fail "Failed to stage ${INPUT_FILE}"
|
|
git -c user.name="gitea-ci-bot" \
|
|
-c user.email="ci@keskikuja.site" \
|
|
commit -m "[skip ci] gitops: update version to ${VERSION}" || _gitops_fail "Failed to commit"
|
|
GITOPS_SHA="$(git rev-parse HEAD)"
|
|
git push || _gitops_fail "Failed to push"
|
|
|
|
_gitops_success
|
|
}
|
|
|
|
_gitops_validate
|
|
|
|
YQ_EXPR=$(_gitops_substitute "${YQ_TPL}" "${VERSION}")
|
|
|
|
GITEA_HOST=$(echo "${GITEA_API_URL}" | sed 's|https://||' | sed 's|http://||')
|
|
CLONE_URL="${GITOPS_CLONE_URL:-https://${GITEA_TOKEN}@${GITEA_HOST}/${GITOPS_REPO}.git}"
|
|
|
|
if [ "${BASH_SOURCE[0]}" = "${0}" ]; then
|
|
_gitops_update
|
|
fi
|