AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / coding / 问题 / 79475480
Accepted
Kris Rice
Kris Rice
Asked: 2025-02-28 20:14:01 +0800 CST2025-02-28 20:14:01 +0800 CST 2025-02-28 20:14:01 +0800 CST

为什么 bash 中的 wait 不被尊重?

  • 772

我写了一个bash脚本:

  • 获取最新的GithubCI/CD 运行
  • 检查下载的工件的摘要
  • 如果摘要发生变化,则生成一个进程来(并行)下载和提取每个工件
  • 等待下载和提取过程完成,然后重新运行脚本

我遇到了一个问题 - 脚本可以毫无问题地生成并行任务,但它每 10 秒执行一次。出于某种原因,wait没有得到尊重。

为什么会这样?我该如何解决?

这是我的脚本:

#!/bin/bash

# GitHub repository details
REPO="RiceKX/Hydra"            # Format: owner/repo, e.g., "octocat/Hello-World"
BRANCH="development"           # The branch you want to check for the latest build
GITHUB_TOKEN="${GITHUB_TOKEN:-$(cat /path/to/your/token)}"  # GitHub token with necessary permissions
GITHUB_API="https://api.github.com"
ARTIFACTS_DIR="latest_dev_build"    # Directory to store downloaded artifacts

# Ensure the artifacts directory exists
mkdir -p "$ARTIFACTS_DIR"

# Run the script in an infinite loop
while true; do
    # Check if repository, branch, and GitHub token are provided
    if [ -z "$REPO" ] || [ -z "$BRANCH" ] || [ -z "$GITHUB_TOKEN" ]; then
      echo "Please set the repository, branch, and GitHub token in the script."
      exit 1
    fi

    # Step 1: Fetch the latest successful run for the specified branch
    echo "Fetching the latest successful build for branch '$BRANCH'..."

    RUN_ID=$(curl -s \
      -H "Authorization: token $GITHUB_TOKEN" \
      "$GITHUB_API/repos/$REPO/actions/runs?branch=$BRANCH&status=success&per_page=1" | \
      jq -r '.workflow_runs[0].id')

    if [ "$RUN_ID" == "null" ] || [ -z "$RUN_ID" ]; then
      echo "No successful builds found for branch '$BRANCH'."
      exit 1
    fi

    echo "Latest successful run ID: $RUN_ID"

    # Step 2: Fetch the artifacts associated with this run
    echo "Fetching artifacts for the latest run..."

    ARTIFACTS=$(curl -s \
      -H "Authorization: token $GITHUB_TOKEN" \
      "$GITHUB_API/repos/$REPO/actions/runs/$RUN_ID/artifacts")

    # Check if we successfully got the artifacts
    if [ -z "$ARTIFACTS" ] || [ "$(echo "$ARTIFACTS" | jq '.artifacts | length')" -eq 0 ]; then
      echo "No artifacts found for this run."
      exit 1
    fi

    # Step 3: Extract artifact details and download them in parallel
    PIDS=()  # Store background process PIDs to wait for them later
    echo "$ARTIFACTS" | jq -r '.artifacts[] | "\(.name) \(.archive_download_url) \(.digest)"' | \
    while read ARTIFACT_NAME ARTIFACT_DOWNLOAD_URL ARTIFACT_DIGEST; do
        if [ -z "$ARTIFACT_NAME" ] || [ -z "$ARTIFACT_DOWNLOAD_URL" ] || [ -z "$ARTIFACT_DIGEST" ]; then
          echo "Error: Artifact name, download URL, or digest missing for artifact."
          continue
        fi

        ARTIFACT_PATH="$ARTIFACTS_DIR/$ARTIFACT_NAME.zip"

        # Check if the artifact already exists
        if [ -f "$ARTIFACT_PATH" ]; then
            # Calculate the local digest of the downloaded artifact
            LOCAL_DIGEST=$(sha256sum "$ARTIFACT_PATH" | cut -d' ' -f1)

            # If the local digest matches the GitHub digest, skip downloading
            if [ "$LOCAL_DIGEST" == "$ARTIFACT_DIGEST" ]; then
                echo "Artifact $ARTIFACT_NAME is already up to date (digest matches), skipping download."
                continue
            else
                echo "Artifact $ARTIFACT_NAME exists but the digest does not match. Re-downloading..."
            fi
        fi

        # Download and extract each artifact in parallel
        (
            echo "Downloading and extracting artifact: $ARTIFACT_NAME from $ARTIFACT_DOWNLOAD_URL"

            curl -L -o "$ARTIFACT_PATH" \
              -H "Authorization: token $GITHUB_TOKEN" \
              "$ARTIFACT_DOWNLOAD_URL"
            
            if [ $? -eq 0 ]; then
                echo "Artifact $ARTIFACT_NAME downloaded successfully."

                # Extract the downloaded artifact
                unzip -o "$ARTIFACT_PATH" -d "$HOME"
                echo "Artifact $ARTIFACT_NAME extracted to $HOME."
            else
                echo "Failed to download $ARTIFACT_NAME."
            fi
        ) &  # Run this process in the background

        PIDS+=($!)
    done

    # Wait for all background processes to finish before continuing
    echo "Waiting for background processes to finish..."
    for PID in "${PIDS[@]}"; do
        wait "$PID"
    done

    echo "All artifacts downloaded and extracted successfully."

    # Sleep for 10 seconds before running the script again
    echo "Waiting for 10 seconds before checking again..."
    sleep 10
done
bash
  • 1 1 个回答
  • 42 Views

1 个回答

  • Voted
  1. Best Answer
    choroba
    2025-02-28T20:43:27+08:002025-02-28T20:43:27+08:00

    您正在管道右侧(即子 shell 中)更改 $PIDS。变量值不会从子 shell 传播回父 shell。

    echo "$ARTIFACTS" \
        | jq -r '.artifacts[] | "\(.name) \(.archive_download_url) \(.digest)"' \
        | while read ARTIFACT_NAME ARTIFACT_DOWNLOAD_URL ARTIFACT_DIGEST; do
    

    要修复此问题,请设置lastpipe或使用进程替换替换重定向

    while read
        ...
    done < <(echo "$ARTIFACTS" | jq ...)
    
    • 2

相关问题

  • (macOS Bash) 2个看似相同的字符串并不相等,仅通过“set -x”显示差异

  • Xargs:尽管扩展了别名,但别名替换仍失败

  • Linux 环境中 $PATH 和 ${PATH:+:${PATH}} 的区别

  • awk 查找并替换为正则表达式和环境变量

  • 如何在 bash 中对任意长度的编号、分隔字母数字字符串的文件名进行零填充?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    重新格式化数字,在固定位置插入分隔符

    • 6 个回答
  • Marko Smith

    为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会?

    • 2 个回答
  • Marko Smith

    VScode 自动卸载扩展的问题(Material 主题)

    • 2 个回答
  • Marko Smith

    Vue 3:创建时出错“预期标识符但发现‘导入’”[重复]

    • 1 个回答
  • Marko Smith

    具有指定基础类型但没有枚举器的“枚举类”的用途是什么?

    • 1 个回答
  • Marko Smith

    如何修复未手动导入的模块的 MODULE_NOT_FOUND 错误?

    • 6 个回答
  • Marko Smith

    `(表达式,左值) = 右值` 在 C 或 C++ 中是有效的赋值吗?为什么有些编译器会接受/拒绝它?

    • 3 个回答
  • Marko Smith

    在 C++ 中,一个不执行任何操作的空程序需要 204KB 的堆,但在 C 中则不需要

    • 1 个回答
  • Marko Smith

    PowerBI 目前与 BigQuery 不兼容:Simba 驱动程序与 Windows 更新有关

    • 2 个回答
  • Marko Smith

    AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String”

    • 1 个回答
  • Martin Hope
    Fantastic Mr Fox msvc std::vector 实现中仅不接受可复制类型 2025-04-23 06:40:49 +0800 CST
  • Martin Hope
    Howard Hinnant 使用 chrono 查找下一个工作日 2025-04-21 08:30:25 +0800 CST
  • Martin Hope
    Fedor 构造函数的成员初始化程序可以包含另一个成员的初始化吗? 2025-04-15 01:01:44 +0800 CST
  • Martin Hope
    Petr Filipský 为什么 C++20 概念会导致循环约束错误,而老式的 SFINAE 不会? 2025-03-23 21:39:40 +0800 CST
  • Martin Hope
    Catskul C++20 是否进行了更改,允许从已知绑定数组“type(&)[N]”转换为未知绑定数组“type(&)[]”? 2025-03-04 06:57:53 +0800 CST
  • Martin Hope
    Stefan Pochmann 为什么 {2,3,10} 和 {x,3,10} (x=2) 的顺序不同? 2025-01-13 23:24:07 +0800 CST
  • Martin Hope
    Chad Feller 在 5.2 版中,bash 条件语句中的 [[ .. ]] 中的分号现在是可选的吗? 2024-10-21 05:50:33 +0800 CST
  • Martin Hope
    Wrench 为什么双破折号 (--) 会导致此 MariaDB 子句评估为 true? 2024-05-05 13:37:20 +0800 CST
  • Martin Hope
    Waket Zheng 为什么 `dict(id=1, **{'id': 2})` 有时会引发 `KeyError: 'id'` 而不是 TypeError? 2024-05-04 14:19:19 +0800 CST
  • Martin Hope
    user924 AdMob:MobileAds.initialize() - 对于某些设备,“java.lang.Integer 无法转换为 java.lang.String” 2024-03-20 03:12:31 +0800 CST

热门标签

python javascript c++ c# java typescript sql reactjs html

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve