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

如何让自定义GitHub Action在指定工作目录中运行?

解决方法

有两种可行的方式来实现你的需求,具体选哪种看场景:

1. 直接用GitHub Actions内置的working-directory参数

不用修改自定义Action,直接在调用它的步骤里添加working-directory配置,指定你想要运行的目录就行。比如你想在whatever-repo目录下执行这个Action,修改后的Workflow如下:

on:
  pull_request:

jobs:
  post-to-sheets:
    runs-on: [self-hosted, linux]
    steps:
      - name: Checkout whatever-repo
        uses: actions/checkout@v3
        with:
          repository: repo/whatever-repo
          path: whatever-repo
          token: ${{ secrets.PAT }}
      - name: Testing custom action
        uses: action/my-custom-action@master
        working-directory: ./whatever-repo  # 指定步骤运行的工作目录

这个参数会让整个步骤的所有命令都在指定目录下执行,相当于提前帮你完成了cd操作,简单高效。

2. 让自定义Action自身支持目录配置

如果你的自定义Action需要更灵活的目录控制(比如允许用户在Workflow里动态指定,或者Action内部有依赖目录的复杂逻辑),那才需要在Action代码里添加cd命令,并通过with暴露projectDir参数。

比如自定义Action的action.yml可以这么定义:

name: 'My Custom Action'
inputs:
  projectDir:
    description: '目标运行目录'
    required: false
    default: '.'
runs:
  using: 'composite'
  steps:
    - run: cd ${{ inputs.projectDir }} && ./your-script.sh
      shell: bash

然后在Workflow里调用时就可以传参:

- name: Testing custom action
  uses: action/my-custom-action@master
  with:
    projectDir: ./whatever-repo

总结:如果只是简单指定步骤运行目录,优先用第一种内置参数的方式;如果Action本身需要目录相关的定制逻辑,再用第二种方式。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 02:20:04