在上一篇我們用actions/labeler寫了一個簡單 workflow
這篇我們會來會來看一下如何透過在workflow內呼叫另一個workflow的方式複用workflow
除了直接把workflow的內容複製貼上,我們也可以透過在workflow內呼叫另一個workflow的方式來複用,這種方式的觸發
workflow事件是workflow_call
呼叫其他workflow的workflow稱作caller,一個
caller workflow可以呼叫多個workflow
,而被呼叫的workflow稱作called workflow
called workflow會被當作caller workflow的一部分執行
,也就是說如果called workflow 內有用actions/checkout,那會clone的是caller workflow 所在的repo
不過env的話則是仍遵循scope,也就是說called workflow內無法直接取用caller workflow內的env,除非透過called workflow的input傳給它。反過來的情況則是需要透過output傳遞
caller workflow和called workflow不一定要在同個repo內
,只要called workflow在一個public repo內、在private repo但是允許caller repo access它,也可以跨repo複用workflow
但是複用workflow仍然一些有限制
不可以呼叫到超過4層
caller workflow,與其呼叫的called workflow組成的整個樹狀結構中不可以超過20個called workflow
以下圖來說這樣是3層,有6個called workflow
called workflow
只能被當作job,不能被當作step使用
name: Try reusable
on:
workflow_call:
inputs:
name:
required: true
type: string
outputs: # 一定要定義outputs,才能讓caller取用
reply:
value: ${{ jobs.first_job_of_reusable_workflow.outputs.output1 }}
jobs:
first_job_of_reusable_workflow:
runs-on: ubuntu-latest
outputs: # 一定要定義outputs,才能讓caller取用
output1: ${{ steps.answer-question.outputs.ANSWER }}
steps:
- name: say hello
run: |
echo Hello ${{ inputs.name }}
- name: answer question from caller
id: answer-question
run: |
echo "ANSWER=It\'s great." >> $GITHUB_OUTPUT
name: Try caller
on:
push:
branches:
- master
jobs:
call_reusable_workflow_1:
uses: tempura327/try-github-actions/.github/workflows/try-reusable-1.yml@master
with:
name: Tempura327
greeting:
runs-on: ubuntu-latest
# 因為跨job,所以一定要定義needs才能取用ouputs
needs: call_reusable_workflow_1
steps:
- name: greeting
run: |
echo How\'s it going with your new workflow?
echo ${{ needs.call_reusable_workflow_1.outputs.reply }}
完成之後跑起來會像下圖這樣