如何访问可重用工作流路径下的相对路径文件?
可重用工作流访问自身仓库文件的解决方法
问题根源
调用可重用工作流时,默认会checkout调用方仓库的代码,所以工作流里的相对路径会指向调用方仓库,而非可重用工作流所在的仓库。
解决步骤
手动checkout可重用工作流仓库
在可重用工作流的ci.yml中,先保留调用方仓库的checkout步骤(若需要调用方的代码),再添加步骤checkout自身仓库到独立目录:jobs: your-job-name: runs-on: ubuntu-latest steps: # 保留调用方仓库的checkout(如果不需要可以省略) - name: Checkout caller repository uses: actions/checkout@v4 # Checkout可重用工作流所在仓库到指定目录 - name: Checkout reusable workflow repository uses: actions/checkout@v4 with: repository: {你的用户名}/{可重用工作流仓库名} ref: main # 替换为你使用的分支或标签 path: reusable-repo使用绝对路径访问目标文件
之后在工作流中引用文件时,使用checkout后的绝对路径:- Dockerfile路径:
./reusable-repo/.github/docker/Dockerfile - main.tf路径:
./reusable-repo/.github/terraform/main.tf
- Dockerfile路径:
优化技巧
利用GitHub上下文变量自动获取可重用工作流的仓库信息,避免硬编码:
- name: Checkout reusable workflow repository uses: actions/checkout@v4 with: repository: ${{ github.repository }} ref: ${{ github.ref_name }} path: reusable-repo
这个方式会自动读取当前可重用工作流所在的仓库和分支,适合同一仓库内管理工作流与通用文件的场景。
内容的提问来源于stack exchange,提问作者Sandy
相关产品推荐
相关产品推荐

