From 40469c5de5ef11707b71a652e04053c4e0e59774 Mon Sep 17 00:00:00 2001 From: Michel Roegl-Brunner <73236783+michelroegl-brunner@users.noreply.github.com> Date: Mon, 24 Feb 2025 15:54:51 +0200 Subject: [PATCH] [gh] Update Script Test Workflow (#2599) * Update Workflow * Update Workflow --- .github/workflows/script-test.yml | 47 ++++------- .../scripts/app-test/pr-alpine-install.func | 20 ++--- .../workflows/scripts/app-test/pr-build.func | 36 ++++---- .../scripts/app-test/pr-create-lxc.sh | 83 +++++++++++-------- .../scripts/app-test/pr-install.func | 44 +++++----- 5 files changed, 115 insertions(+), 115 deletions(-) diff --git a/.github/workflows/script-test.yml b/.github/workflows/script-test.yml index 6b9e100eb..3b86f3d89 100644 --- a/.github/workflows/script-test.yml +++ b/.github/workflows/script-test.yml @@ -13,7 +13,7 @@ jobs: run-install-script: runs-on: pvenode steps: - - name: Checkout PR branch (supports forks) + - name: Checkout PR branch uses: actions/checkout@v4 with: ref: ${{ github.event.pull_request.head.ref }} @@ -37,7 +37,8 @@ jobs: echo "Changed files: $CHANGED_FILES" echo "SCRIPT=$CHANGED_FILES" >> $GITHUB_ENV env: - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + - name: Get scripts id: check-install-script @@ -68,11 +69,11 @@ jobs: echo "Running Test for: $STRIPPED_NAME" if [[ $FILE =~ ^install/.*-install\.sh$ ]]; then CT_SCRIPT="ct/$STRIPPED_NAME.sh" + echo $CT_SCRIPT if [[ ! -f $CT_SCRIPT ]]; then echo "No CT script found for $STRIPPED_NAME" ERROR_MSG="No CT script found for $FILE" - echo "$ERROR_MSG" > result_$STRIPPED_NAME.log - continue + RUN=false fi echo "Found CT script for $STRIPPED_NAME" chmod +x "$CT_SCRIPT" @@ -82,32 +83,19 @@ jobs: if [[ ! -f $INSTALL_SCRIPT ]]; then echo "No install script found for $STRIPPED_NAME" ERROR_MSG="No install script found for $FILE" - echo "$ERROR_MSG" > result_$STRIPPED_NAME.log - continue + RUN=false fi echo "Found install script for $STRIPPED_NAME" chmod +x "$INSTALL_SCRIPT" RUNNING_FILE=$FILE fi - git remote add community-scripts https://github.com/community-scripts/ProxmoxVE.git - git fetch community-scripts - rm -f .github/workflows/scripts/app-test/pr-build.func || true - rm -f .github/workflows/scripts/app-test/pr-install.func || true - rm -f .github/workflows/scripts/app-test/pr-alpine-install.func || true - rm -f .github/workflows/scripts/app-test/pr-create-lxc.sh || true - git checkout community-scripts/main -- .github/workflows/scripts/app-test/pr-build.func - git checkout community-scripts/main -- .github/workflows/scripts/app-test/pr-install.func - git checkout community-scripts/main -- .github/workflows/scripts/app-test/pr-alpine-install.func - git checkout community-scripts/main -- .github/workflows/scripts/app-test/pr-create-lxc.sh - chmod +x $RUNNING_FILE - chmod +x .github/workflows/scripts/app-test/pr-create-lxc.sh - chmod +x .github/workflows/scripts/app-test/pr-install.func - chmod +x .github/workflows/scripts/app-test/pr-alpine-install.func - chmod +x .github/workflows/scripts/app-test/pr-build.func - sed -i 's|source <(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/build.func)|source .github/workflows/scripts/app-test/pr-build.func|g' "$RUNNING_FILE" - echo "Executing $RUNNING_FILE" - ERROR_MSG=$(./$RUNNING_FILE 2>&1 > /dev/null) - echo "Finished running $FILE" + if [[ $RUN != "false" ]]; then + chmod +x $RUNNING_FILE + sed -i 's|source <(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/misc/build.func)|source <(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/.github/workflows/scripts/app-test/pr-build.func)|g' "$RUNNING_FILE" + echo "Executing $RUNNING_FILE" + ERROR_MSG=$(./$RUNNING_FILE 2>&1 > /dev/null) + echo "Finished running $FILE" + fi if [ -n "$ERROR_MSG" ]; then echo "ERROR in $STRIPPED_NAME: $ERROR_MSG" echo "$ERROR_MSG" > result_$STRIPPED_NAME.log @@ -125,12 +113,13 @@ jobs: pct stop $container_id pct destroy $container_id fi - done + done + - name: Post error comments run: | ERROR="false" - SEARCH_LINE=".github/workflows/scripts/app-test/pr-build.func: line 253:" + SEARCH_LINE=".github/workflows/scripts/app-test/pr-build.func: line 255" for FILE in ${{ env.ALL_FILES }}; do STRIPPED_NAME=$(basename "$FILE" | sed 's/-install//' | sed 's/\.sh$//') if [[ ! -f result_$STRIPPED_NAME.log ]]; then @@ -145,8 +134,6 @@ jobs: gh pr comment ${{ github.event.pull_request.number }} \ --repo ${{ github.repository }} \ --body ":warning: The script _**$FILE**_ failed with the following message:
${CLEANED_ERROR_MSG}
" - - ERROR="true" fi done @@ -157,3 +144,5 @@ jobs: - name: Fail if error if: ${{ env.ERROR == 'true' }} run: exit 1 + + diff --git a/.github/workflows/scripts/app-test/pr-alpine-install.func b/.github/workflows/scripts/app-test/pr-alpine-install.func index 39a6a82fb..ae3bc174d 100644 --- a/.github/workflows/scripts/app-test/pr-alpine-install.func +++ b/.github/workflows/scripts/app-test/pr-alpine-install.func @@ -1,6 +1,6 @@ #!/usr/bin/env bash # Copyright (c) 2021-2025 community-scripts ORG -# Author: michelroegl-brunner +# Author: Michel Roegl-Brunner (michelroegl-brunner) # License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE color() { @@ -11,7 +11,7 @@ catch_errors() { trap 'error_handler $LINENO "$BASH_COMMAND"' ERR } - +# This function handles errors error_handler() { local line_number="$1" local command="$2" @@ -21,8 +21,8 @@ error_handler() { exit 0 } verb_ip6() { - STD="" - return + STD="" + return } msg_info() { @@ -30,13 +30,13 @@ msg_info() { echo -ne "${msg}\n" } -msg_ok() { +msg_ok() { local msg="$1" echo -e "${msg}\n" } msg_error() { - + local msg="$1" echo -e "${msg}\n" } @@ -71,7 +71,7 @@ network_check() { } update_os() { - msg_info "Updating Container OS" + msg_info "Updating Container OS" apk update apk upgrade msg_ok "Updated Container OS" @@ -82,7 +82,5 @@ motd_ssh() { } customize() { - return -} - - + return +} \ No newline at end of file diff --git a/.github/workflows/scripts/app-test/pr-build.func b/.github/workflows/scripts/app-test/pr-build.func index 360c15ede..4ca15d099 100644 --- a/.github/workflows/scripts/app-test/pr-build.func +++ b/.github/workflows/scripts/app-test/pr-build.func @@ -6,12 +6,13 @@ variables() { NSAPP=$(echo ${APP,,} | tr -d ' ') # This function sets the NSAPP variable by converting the value of the APP variable to lowercase and removing any spaces. var_install="${NSAPP}-install" # sets the var_install variable by appending "-install" to the value of NSAPP. + } NEXTID=$(pvesh get /cluster/nextid) timezone=$(cat /etc/timezone) -header_info(){ - return +header_info() { + return } base_settings() { @@ -20,10 +21,10 @@ base_settings() { DISK_SIZE="4" CORE_COUNT="1" RAM_SIZE="1024" - VERBOSE="${1:-no}" + VERBOSE="no" PW="" CT_ID=$NEXTID - HN="Testing" + HN=$NSAPP BRG="vmbr0" NET="dhcp" GATE="" @@ -106,7 +107,7 @@ catch_errors() { } # This function handles errors -error_handler() { +error_handler() { local line_number="$1" local command="$2" SCRIPT_NAME=$(basename "$0") @@ -120,17 +121,17 @@ msg_info() { echo -ne "${msg}\n" } -msg_ok() { +msg_ok() { local msg="$1" echo -e "${msg}\n" } msg_error() { - + local msg="$1" echo -e "${msg}\n" } -start(){ +start() { base_settings return } @@ -146,9 +147,9 @@ build_container() { TEMP_DIR=$(mktemp -d) pushd $TEMP_DIR >/dev/null if [ "$var_os" == "alpine" ]; then - export FUNCTIONS_FILE_PATH="$(cat /root/actions-runner/_work/ProxmoxVE/ProxmoxVE/.github/workflows/scripts/app-test/pr-alpine-install.func)" + export FUNCTIONS_FILE_PATH="$(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/.github/workflows/scripts/app-test/pr-alpine-install.func)" else - export FUNCTIONS_FILE_PATH="$(cat /root/actions-runner/_work/ProxmoxVE/ProxmoxVE/.github/workflows/scripts/app-test/pr-install.func)" + export FUNCTIONS_FILE_PATH="$(curl -s https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/.github/workflows/scripts/app-test/pr-install.func)" fi export CACHER="$APT_CACHER" @@ -182,9 +183,8 @@ build_container() { " echo "Container ID: $CTID" - # This executes create_lxc.sh and creates the container and .conf file - bash /root/actions-runner/_work/ProxmoxVE/ProxmoxVE/.github/workflows/scripts/app-test/pr-create-lxc.sh + bash -c "$(wget -qLO - https://raw.githubusercontent.com/community-scripts/ProxmoxVE/main/.github/workflows/scripts/app-test/pr-create-lxc.sh)" LXC_CONFIG=/etc/pve/lxc/${CTID}.conf if [ "$CT_TYPE" == "0" ]; then @@ -233,6 +233,7 @@ EOF fi fi fi + # This starts the container and executes -install.sh msg_info "Starting LXC Container" pct start "$CTID" @@ -242,7 +243,7 @@ EOF msg_error "No install script found for $APP" exit 1 fi - if [ "$var_os" == "alpine" ]; then + if [ "$var_os" == "alpine" ]; then sleep 3 pct exec "$CTID" -- /bin/sh -c 'cat </etc/apk/repositories http://dl-cdn.alpinelinux.org/alpine/latest-stable/main @@ -250,11 +251,10 @@ http://dl-cdn.alpinelinux.org/alpine/latest-stable/community EOF' pct exec "$CTID" -- ash -c "apk add bash >/dev/null" fi - lxc-attach -n "$CTID" -- bash -c "$(cat /root/actions-runner/_work/ProxmoxVE/ProxmoxVE/install/$var_install.sh)" $var_install.sh + lxc-attach -n "$CTID" -- bash -c "$(cat /root/actions-runner/_work/ProxmoxVE/ProxmoxVE/install/$var_install.sh)" } -description(){ - IP=$(pct exec "$CTID" ip a s dev eth0 | awk '/inet / {print $2}' | cut -d/ -f1) - return -} +description() { + IP=$(pct exec "$CTID" ip a s dev eth0 | awk '/inet / {print $2}' | cut -d/ -f1) +} \ No newline at end of file diff --git a/.github/workflows/scripts/app-test/pr-create-lxc.sh b/.github/workflows/scripts/app-test/pr-create-lxc.sh index 6ca899cf4..0bb2a9d2f 100644 --- a/.github/workflows/scripts/app-test/pr-create-lxc.sh +++ b/.github/workflows/scripts/app-test/pr-create-lxc.sh @@ -11,8 +11,9 @@ catch_errors() { trap 'error_handler $LINENO "$BASH_COMMAND"' ERR } +# This function handles errors error_handler() { - local exit_code="$?" + local exit_code="$?" local line_number="$1" local command="$2" local error_message="Failure in line $line_number: exit code $exit_code: while executing command $command" @@ -20,7 +21,7 @@ error_handler() { exit 100 } verb_ip6() { - return + return } msg_info() { @@ -28,18 +29,17 @@ msg_info() { echo -ne "${msg}\n" } -msg_ok() { +msg_ok() { local msg="$1" echo -e "${msg}\n" } msg_error() { - + local msg="$1" echo -e "${msg}\n" } - VALIDCT=$(pvesm status -content rootdir | awk 'NR>1') if [ -z "$VALIDCT" ]; then msg_error "Unable to detect a valid Container Storage location." @@ -64,9 +64,12 @@ function select_storage() { CONTENT='vztmpl' CONTENT_LABEL='Container template' ;; - *) false || { msg_error "Invalid storage class."; exit 201; };; + *) false || { + msg_error "Invalid storage class." + exit 201 + } ;; esac - + # This Queries all storage locations local -a MENU while read -r line; do @@ -80,23 +83,32 @@ function select_storage() { fi MENU+=("$TAG" "$ITEM" "OFF") done < <(pvesm status -content $CONTENT | awk 'NR>1') - + # Select storage location - if [ $((${#MENU[@]}/3)) -eq 1 ]; then + if [ $((${#MENU[@]} / 3)) -eq 1 ]; then printf ${MENU[0]} else - msg_error "STORAGE ISSUES!" - exit 202 + msg_error "STORAGE ISSUES!" + exit 202 fi } +[[ "${CTID:-}" ]] || { + msg_error "You need to set 'CTID' variable." + exit 203 +} +[[ "${PCT_OSTYPE:-}" ]] || { + msg_error "You need to set 'PCT_OSTYPE' variable." + exit 204 +} +# Test if ID is valid +[ "$CTID" -ge "100" ] || { + msg_error "ID cannot be less than 100." + exit 205 +} -[[ "${CTID:-}" ]] || { msg_error "You need to set 'CTID' variable."; exit 203; } -[[ "${PCT_OSTYPE:-}" ]] || { msg_error "You need to set 'PCT_OSTYPE' variable."; exit 204; } - -[ "$CTID" -ge "100" ] || { msg_error "ID cannot be less than 100."; exit 205; } - +# Test if ID is in use if pct status $CTID &>/dev/null; then echo -e "ID '$CTID' is already in use." unset CTID @@ -110,10 +122,12 @@ CONTAINER_STORAGE=$(select_storage container) || exit pveam update >/dev/null - TEMPLATE_SEARCH=${PCT_OSTYPE}-${PCT_OSVERSION:-} mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($TEMPLATE_SEARCH.*\)/\1/p" | sort -t - -k 2 -V) -[ ${#TEMPLATES[@]} -gt 0 ] || { msg_error "Unable to find a template when searching for '$TEMPLATE_SEARCH'."; exit 207; } +[ ${#TEMPLATES[@]} -gt 0 ] || { + msg_error "Unable to find a template when searching for '$TEMPLATE_SEARCH'." + exit 207 +} TEMPLATE="${TEMPLATES[-1]}" TEMPLATE_PATH="/var/lib/vz/template/cache/$TEMPLATE" @@ -121,28 +135,29 @@ TEMPLATE_PATH="/var/lib/vz/template/cache/$TEMPLATE" if ! pveam list "$TEMPLATE_STORAGE" | grep -q "$TEMPLATE"; then [[ -f "$TEMPLATE_PATH" ]] && rm -f "$TEMPLATE_PATH" pveam download "$TEMPLATE_STORAGE" "$TEMPLATE" >/dev/null || - { msg_error "A problem occurred while downloading the LXC template."; exit 208; } + { + msg_error "A problem occurred while downloading the LXC template." + exit 208 + } fi - -grep -q "root:100000:65536" /etc/subuid || echo "root:100000:65536" >> /etc/subuid -grep -q "root:100000:65536" /etc/subgid || echo "root:100000:65536" >> /etc/subgid +grep -q "root:100000:65536" /etc/subuid || echo "root:100000:65536" >>/etc/subuid +grep -q "root:100000:65536" /etc/subgid || echo "root:100000:65536" >>/etc/subgid PCT_OPTIONS=(${PCT_OPTIONS[@]:-${DEFAULT_PCT_OPTIONS[@]}}) [[ " ${PCT_OPTIONS[@]} " =~ " -rootfs " ]] || PCT_OPTIONS+=(-rootfs "$CONTAINER_STORAGE:${PCT_DISK_SIZE:-8}") +if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[@]}" &>/dev/null; then + [[ -f "$TEMPLATE_PATH" ]] && rm -f "$TEMPLATE_PATH" + + pveam download "$TEMPLATE_STORAGE" "$TEMPLATE" >/dev/null || + { + msg_error "A problem occurred while re-downloading the LXC template." + exit 208 + } if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[@]}" &>/dev/null; then - [[ -f "$TEMPLATE_PATH" ]] && rm -f "$TEMPLATE_PATH" - - - pveam download "$TEMPLATE_STORAGE" "$TEMPLATE" >/dev/null || - { msg_error "A problem occurred while re-downloading the LXC template."; exit 208; } - - - if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[@]}" &>/dev/null; then - msg_error "A problem occurred while trying to create container after re-downloading template." - exit 200 - fi + msg_error "A problem occurred while trying to create container after re-downloading template." + exit 200 fi - +fi diff --git a/.github/workflows/scripts/app-test/pr-install.func b/.github/workflows/scripts/app-test/pr-install.func index 49195f365..1719a1a9e 100644 --- a/.github/workflows/scripts/app-test/pr-install.func +++ b/.github/workflows/scripts/app-test/pr-install.func @@ -1,31 +1,31 @@ #!/usr/bin/env bash # Copyright (c) 2021-2025 community-scripts ORG -# Author: michelroegl-brunner +# Author: Michel Roegl-Brunner (michelroegl-brunner) # License: MIT | https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE color() { return } -SCRIPT_NAME="${BASH_SOURCE[0]:-unknown_script}" catch_errors() { - set -Euoe pipefail + set -Euo pipefail trap 'error_handler $LINENO "$BASH_COMMAND"' ERR } error_handler() { local line_number="$1" local command="$2" - local error_message="$SCRIPT_NAME: Failure in line $line_number while executing command '$command'" - echo -e "\n$error_message" - exit 300 + local error_message="Failure in line $line_number while executing command '$command'" + echo -e "\n$error_message\n" >&2 + exit 1 } verb_ip6() { - STD="silent" - silent() { - "$@" >/dev/null 2>&1 || error_handler "${BASH_LINENO[0]}" "$*" - } + STD="silent" + silent() { + "$@" >/dev/null 2>&1 || error_handler "${BASH_LINENO[0]}" "$*" + } + return } msg_info() { @@ -33,19 +33,21 @@ msg_info() { echo -ne "${msg}\n" } -msg_ok() { +msg_ok() { local msg="$1" echo -e "${msg}\n" } msg_error() { - + local msg="$1" echo -e "${msg}\n" } - RETRY_NUM=10 - RETRY_EVERY=3 + +RETRY_NUM=10 +RETRY_EVERY=3 setting_up_container() { + sed -i "/$LANG/ s/\(^# \)//" /etc/locale.gen locale_line=$(grep -v '^#' /etc/locale.gen | grep -E '^[a-zA-Z]' | awk '{print $1}' | head -n 1) echo "LANG=${locale_line}" >/etc/default/locale @@ -53,12 +55,11 @@ setting_up_container() { export LANG=${locale_line} echo $tz >/etc/timezone ln -sf /usr/share/zoneinfo/$tz /etc/localtime - + for ((i = RETRY_NUM; i > 0; i--)); do if [ "$(hostname -I)" != "" ]; then break fi - echo 1>&2 -en "No Network! " sleep $RETRY_EVERY done if [ "$(hostname -I)" = "" ]; then @@ -68,8 +69,6 @@ setting_up_container() { fi rm -rf /usr/lib/python3.*/EXTERNALLY-MANAGED systemctl disable -q --now systemd-networkd-wait-online.service - msg_ok "Set up Container OS" - msg_ok "Network Connected: $(hostname -I)" } network_check() { @@ -79,11 +78,10 @@ network_check() { } update_os() { - msg_info "Updating Container OS" - apt-get update - apt-get -o Dpkg::Options::="--force-confold" -y dist-upgrade + export DEBIAN_FRONTEND=noninteractive + apt-get update >/dev/null 2>&1 + apt-get -o Dpkg::Options::="--force-confold" -y dist-upgrade >/dev/null rm -rf /usr/lib/python3.*/EXTERNALLY-MANAGED - msg_ok "Updated Container OS" } motd_ssh() { @@ -91,5 +89,5 @@ motd_ssh() { } customize() { - return + return } \ No newline at end of file