#!/bin/sh set -eu START_DIR="$(pwd)" echo "[bump-version] invoked from ${START_DIR}" SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" REPO_ROOT="" if command -v git >/dev/null 2>&1; then REPO_ROOT="$(git rev-parse --show-toplevel 2>/dev/null || true)" fi if [ -z "${REPO_ROOT}" ]; then REPO_ROOT="$(cd "${SCRIPT_DIR}/../../.." && pwd)" fi echo "[bump-version] repo root resolved to ${REPO_ROOT}" cd "${REPO_ROOT}" VERSION_FILE="./version" if [ ! -f "${VERSION_FILE}" ]; then if git cat-file -e "HEAD:version" 2>/dev/null; then echo "[bump-version] version file missing in workspace, restoring from HEAD" >&2 git show "HEAD:version" > "${VERSION_FILE}" else echo "[bump-version] version file not found: ${VERSION_FILE}" >&2 exit 1 fi fi CURRENT_VERSION="$(cat "${VERSION_FILE}")" NEXT_VERSION="$(printf '%s' "${CURRENT_VERSION}" | awk -F. -v OFS=. ' function pad(value, width, result, i) { result=value "" if (length(result) >= width) { return result } i = width - length(result) while (i-- > 0) { result = "0" result } return result } NF==1 { print ++$NF; next } { last = $NF + 1 $NF = pad(last, length($NF)) print }')" printf '%s\n' "${NEXT_VERSION}" > "${VERSION_FILE}" echo "[bump-version] ${CURRENT_VERSION} -> ${NEXT_VERSION}" git add "${VERSION_FILE}" if git diff --cached --quiet; then echo "[bump-version] no changes staged, skipping commit" exit 0 fi AUTHOR_NAME="${GIT_AUTHOR_NAME:-woodpecker}" AUTHOR_EMAIL="${GIT_AUTHOR_EMAIL:-ci@sendico.io}" git config user.name "${AUTHOR_NAME}" git config user.email "${AUTHOR_EMAIL}" git commit -m "chore(ci): bump version to ${NEXT_VERSION}" BRANCH="${WOODPECKER_BRANCH:-}" if [ -z "${BRANCH}" ] || [ "${BRANCH}" = "HEAD" ]; then BRANCH="$(git rev-parse --abbrev-ref HEAD)" fi NETRC_MACHINE="${CI_NETRC_MACHINE:-${WOODPECKER_NETRC_MACHINE:-}}" NETRC_USERNAME="${CI_NETRC_USERNAME:-${WOODPECKER_NETRC_USERNAME:-${CI_NETRC_LOGIN:-${WOODPECKER_NETRC_LOGIN:-}}}}" NETRC_PASSWORD="${CI_NETRC_PASSWORD:-${WOODPECKER_NETRC_PASSWORD:-}}" if [ -n "${NETRC_MACHINE}" ] && [ -n "${NETRC_USERNAME}" ] && [ -n "${NETRC_PASSWORD}" ]; then NETRC_FILE="${HOME:-/root}/.netrc" if [ ! -f "${NETRC_FILE}" ]; then { printf 'machine %s\n' "${NETRC_MACHINE}" printf 'login %s\n' "${NETRC_USERNAME}" printf 'password %s\n' "${NETRC_PASSWORD}" } > "${NETRC_FILE}" chmod 600 "${NETRC_FILE}" echo "[bump-version] wrote credentials for ${NETRC_MACHINE}" fi fi git push origin "HEAD:${BRANCH}"