如何将调用者Workflow中的默认工作目录传递至可复用Workflow
How to Pass a Working Directory from Caller to Reusable GitHub Workflow
To pass your specified working directory (myfolder/python/serviceA) from the caller workflow to the reusable workflow and make it the default for all run steps, follow these corrected implementations:
1. Caller Workflow Adjustments
You need to properly reference the default working directory using GitHub Actions expression syntax (${{ ... }}) and pass it as an input to the reusable job. Here's the fixed code:
defaults: run: working-directory: myfolder/python/serviceA jobs: reusable-job: uses: <owner>/<repo>/.github/workflows/<reusable-workflow-file>.yml@master with: working-directory: ${{ defaults.run.working-directory }}
Key fixes here:
- Replaced the incorrect double curly braces (
{{ ... }}) with GitHub's valid expression syntax${{ ... }} - Corrected the typo
deafultstodefaults
2. Reusable Workflow Setup
In the reusable workflow, define a required input for the working directory, then set it as the default working directory for all run steps:
on: workflow_call: inputs: working-directory: required: true type: string # Explicitly define the input type for clarity defaults: run: working-directory: ${{ inputs.working-directory }} # Apply the input as default jobs: my-reusable-job: runs-on: ubuntu-latest steps: - name: Checkout repository uses: actions/checkout@v4 - name: Verify working directory run: pwd # This will output the passed directory path (myfolder/python/serviceA) - name: Run a custom command run: echo "Working in the specified directory!"
Notes:
- All
runsteps in the reusable workflow will automatically use the passed directory unless you explicitly override it with aworking-directoryfield in individual steps. - The input is marked as
requiredto ensure the caller always provides a value, preventing runtime errors.
内容的提问来源于stack exchange,提问作者reach jaango
相关产品推荐
相关产品推荐

