From 0c62e90c3f268d3b482472f38430512335aa9db7 Mon Sep 17 00:00:00 2001 From: Grische Date: Thu, 20 Jun 2024 08:57:00 +0200 Subject: [PATCH] contrib: rework update-modules to fix race cond. Previously, the modules file was read and written to at the same time (if there were updates). This change works around this by reworking the file parsing. Notable changes - only update the modules that are in GLUON_SITE_FEEDS - avoid SIGPIPE by listing only the commit for the target branch --- contrib/actions/update-modules.sh | 59 ++++++++++++++++++------------- 1 file changed, 34 insertions(+), 25 deletions(-) diff --git a/contrib/actions/update-modules.sh b/contrib/actions/update-modules.sh index 68f3f29a..cf080758 100755 --- a/contrib/actions/update-modules.sh +++ b/contrib/actions/update-modules.sh @@ -1,30 +1,39 @@ #!/bin/bash +set -eEu +set -o pipefail -# Die Datei, die die Repository-Informationen enthaelt +# file that contains the module information MODULES_FILE="modules" -# Durchlaufe die Datei, um alle Repository-URLs und ihre entsprechenden Commit-Variablen zu finden -# shellcheck disable=SC2094 -while IFS= read -r line; do - # Überprüfe, ob die Zeile eine Repository-URL ist - if [[ "$line" == "PACKAGES_"*"_REPO="* ]]; then - # Extrahiere den Repository-Namen und die URL - REPO_NAME=$(echo "$line" | cut -d '=' -f 1) - REPO_URL=$(echo "$line" | cut -d '=' -f 2-) - - # Den neuesten Commit für das Repository abrufen - NEW_COMMIT=$(git ls-remote "$REPO_URL" | grep -oE '[0-9a-f]{40}' | head -n1) - - # Überprüfen, ob ein neuer Commit gefunden wurde - if [ -n "$NEW_COMMIT" ]; then - # Den Wert des Commits in der Datei aktualisieren - # shellcheck disable=SC2094 - COMMIT_LINE=$(grep -n "${REPO_NAME/_REPO/_COMMIT}" "$MODULES_FILE" | cut -d ':' -f 1) - # shellcheck disable=SC2094 - sed -i "${COMMIT_LINE}s/.*/${REPO_NAME/_REPO/_COMMIT}=$NEW_COMMIT/" "$MODULES_FILE" - echo "Der Wert von ${REPO_NAME/_REPO/_COMMIT} wurde auf den neuesten Commit ($NEW_COMMIT) aktualisiert." - else - echo "Fehler: Der neueste Commit f r $REPO_NAME konnte nicht abgerufen werden." - fi +if ((BASH_VERSINFO[0] < 4)); then + echo "This script requires Bash 4.0 or above." + exit 1 +fi + +function get_value_from_line() { + cat | cut -d '=' -f 2 +} + +function get_all_repo_names() { + local file=$1 + grep "^GLUON_SITE_FEEDS" "${file}" | get_value_from_line | tr -d "'" +} + +for repo in $(get_all_repo_names "${MODULES_FILE}"); do + REPO_URL=$(grep "^PACKAGES_${repo^^}_REPO" "${MODULES_FILE}" | get_value_from_line) + REPO_COMMIT=$(grep "^PACKAGES_${repo^^}_COMMIT" "${MODULES_FILE}" | get_value_from_line) + REPO_BRANCH=$(grep "^PACKAGES_${repo^^}_BRANCH" "${MODULES_FILE}" | get_value_from_line) + + # Get newest commit of the repo + NEW_COMMIT=$(git ls-remote --heads "${REPO_URL}" "${REPO_BRANCH}" | grep -oE '[0-9a-f]{40}') + + # Check if the commit has changed + if [[ "${REPO_COMMIT}" == "${NEW_COMMIT}" ]]; then + echo "No updates for ${repo} repository" + continue fi -done < "$MODULES_FILE" + + # Update the value of the commit + sed -i "s/${REPO_COMMIT}/${NEW_COMMIT}/" "${MODULES_FILE}" + echo "Updated commit of ${repo} (${REPO_COMMIT}) to the newest commit (${NEW_COMMIT})." +done