You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

GitHub Actions变量跨作业传递失败问题排查及解决求助

问题原因及解决方案

核心问题

GitHub Actions 默认仅在作业(job)成功完成时,才会处理并传递作业的 outputs。即使你通过 if: always() 让生成输出的步骤在作业失败时仍能执行,作业的 outputs 也会因为作业整体状态为失败,默认忽略该步骤的输出值,导致后续作业无法获取。

你提到的「其他变量能正常获取」,大概率是因为这些变量来自作业成功执行的步骤,GitHub Actions 会默认保留成功步骤的输出。

解决方案

在作业的 outputs 定义中,用 always() 函数包裹需要强制传递的输出引用,确保无论作业最终状态是成功还是失败,都能评估并传递该值。

修正后的配置(第二种方法)

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Deploy
        id: deployToCloud
        timeout-minutes: 1
        run: |
          echo "deploymentId=$DEPLOYMENT_ID" >> $GITHUB_OUTPUT

      - name: Get last notification
        if: always()
        id: lastNotification
        timeout-minutes: 1
        run: |
          LAST_NOTIFICATION=$(app get-last-notification)
          echo "$LAST_NOTIFICATION"
          echo "lastNotificationMessage=$LAST_NOTIFICATION" >> $GITHUB_OUTPUT

    outputs:
      deploymentId: ${{ steps.deployToCloud.outputs.deploymentId }}
      # 用 always() 包裹,强制传递步骤输出,不受作业状态影响
      lastNotificationMessage: ${{ always(steps.lastNotification.outputs.lastNotificationMessage) }}

  notification:
    runs-on: ubuntu-latest
    needs: deploy
    if: always()

    steps:
      - name: Notification
        timeout-minutes: 1
        run: |
          app send-notification \
            --last-message "$LAST_NOTIFICATION_MESSAGE"
        env:
          DEPLOYMENT_ID: ${{ needs.deploy.outputs.deploymentId }}
          LAST_NOTIFICATION_MESSAGE: ${{ needs.deploy.outputs.lastNotificationMessage }}

第一种方法的修正(基于环境变量)

如果你想保留用环境变量的方式,同样需要在 outputs 中用 always() 包裹:

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Get last notification
        if: always()
        id: notification
        timeout-minutes: 1
        run: |
          LAST_NOTIFICATION=$(app get-last-notification)
          echo "$LAST_NOTIFICATION"
          echo "CUSTOM_LAST_NOTIFICATION=$LAST_NOTIFICATION" >> $GITHUB_ENV

    outputs:
      # 用 always() 包裹环境变量引用
      lastNotificationMessage: ${{ always(env.CUSTOM_LAST_NOTIFICATION) }}
      # 其他变量保持不变

  notification:
    runs-on: ubuntu-latest
    needs: deploy
    if: always()

    steps:
      - name: Notification
        timeout-minutes: 1
        run: |
          app send-notification \
            --last-message "$LAST_NOTIFICATION_MESSAGE"
        env:
          LAST_NOTIFICATION_MESSAGE: ${{ needs.deploy.outputs.lastNotificationMessage }}

额外验证点

  1. 确认 Get last notification 步骤确实成功执行(从你的截图来看已经满足);
  2. 由于你已经用 base64url 编码处理了变量内容,无需担心特殊字符或换行导致的输出截断问题。

内容的提问来源于stack exchange,提问作者Horizon

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 03:10:57