[英]How to pass the output of a bash command to Github Action parameter
[英]Github Action output not saving
我正在嘗試保存 GitHub Actions 作業的結果以供另一個作業使用,如下所示:
jobs:
job1:
runs-on: ubuntu-latest
# Map a step output to a job output
outputs:
output1: ${{ steps.step1.outputs.test }}
steps:
- id: step1
run: |
./my-command
[ $? == 0 ] && result="Success" || result="Failure"
echo "result=$result" >> $GITHUB_OUTPUT
job2:
runs-on: ubuntu-latest
needs: job1
steps:
- run: echo ${{needs.job1.outputs.output1}}
我遇到的問題是這永遠不會將“失敗”顯示為 output,只會顯示“成功”。 如果./my-command
有一個非零的退出代碼,它不會顯示任何內容。
我錯過了什么嗎?
默認run
步驟bash --noprofile --norc -eo pipefail {0}
在引擎蓋下運行(見這里); 這包括“退出時出錯”選項,如果./my-command
不成功,它會使運行步驟中止。 為避免這種情況,您可以使用類似
if ./my-command; then
result="Success"
else
result="Failure"
fi
echo "result=$result" >> "$GITHUB_OUTPUT"
在run
步驟中。 條件中的非零退出狀態不會觸發-e
選項的退出錯誤行為。
除了@benjamin-w 寫的內容之外,您的 YAML 中還有一個變量命名不正確
jobs:
job1:
runs-on: ubuntu-latest
# Map a step output to a job output
outputs:
# The variable name of the output here needs to match what you write to $GITHUB_OUTPUT
output1: ${{ steps.step1.outputs.result }}
steps:
- id: step1
run: |
./my-command
[ $? == 0 ] && result="Success" || result="Failure"
echo "result=$result" >> $GITHUB_OUTPUT
job2:
runs-on: ubuntu-latest
needs: job1
steps:
- run: echo ${{needs.job1.outputs.output1}}
請注意,我將steps.step1.outputs.test
更改為steps.step1.outputs.result
。 當您將值寫出到$GITHUB_OUTPUT
時,您使用了名稱result
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.