GitHub Action跨仓库工作流调用及DLL文件共享问题
解决跨仓库GitHub Action调用与DLL共享问题
一、正确调用跨仓库GitHub Action工作流
核心问题是被调用的工作流默认拉取调用方仓库代码,需在被调用工作流中明确拉取自身仓库代码,并配置正确权限。
配置dlls仓库的工作流(workflowStep.yml)
添加workflow_call触发规则,确保工作流允许被外部调用,同时在步骤中明确拉取当前仓库代码:name: Build DLLs on: workflow_call: inputs: target_branch: type: string default: main permissions: contents: read jobs: build-dlls: runs-on: ubuntu-latest steps: - name: Checkout dlls repository uses: actions/checkout@v4 with: repository: my-company/dlls ref: ${{ inputs.target_branch }} persist-credentials: false token: ${{ secrets.GITHUB_TOKEN }} - name: Build DLLs run: dotnet build MyDlls.sln --configuration Release在my-api仓库的buildApi.yml中调用工作流
通过uses指定被调用工作流的仓库路径与分支,确保依赖顺序:name: Build API on: [push, pull_request] jobs: call-dll-workflow: uses: my-company/dlls/.github/workflows/workflowStep.yml@main with: target_branch: main secrets: inherit build-api: needs: call-dll-workflow runs-on: ubuntu-latest steps: - name: Checkout my-api repository uses: actions/checkout@v4 # 后续API构建步骤
二、仓库间共享DLL文件
提供两种实用方案,按需选择:
方案1:通过GitHub Artifacts传递临时DLL
适合短周期构建依赖场景:
dlls仓库上传DLL作为Artifact
在workflowStep.yml的构建步骤后添加上传逻辑:- name: Upload DLL artifacts uses: actions/upload-artifact@v4 with: name: my-dlls path: ./bin/Release/**/*.dll # 替换为实际DLL输出路径 retention-days: 7my-api仓库下载并放置DLL
在build-api job中添加下载步骤,将DLL放到API依赖的指定路径:- name: Download DLL artifacts uses: actions/download-artifact@v4 with: name: my-dlls path: ./lib # 替换为MyApi.sln依赖的DLL存放目录 - name: Build API run: dotnet build MyApi.sln --configuration Release
方案2:发布NuGet包到GitHub Packages(推荐)
适合长期依赖、版本化管理场景:
dlls仓库打包并发布NuGet包
修改workflowStep.yml,添加打包与发布步骤:- name: Pack NuGet package run: dotnet pack MyDlls.sln --configuration Release --output ./nupkg - name: Publish to GitHub Packages run: dotnet nuget push ./nupkg/*.nupkg --source https://nuget.pkg.github.com/my-company/index.json --api-key ${{ secrets.GITHUB_TOKEN }}注意:需在dlls仓库的Packages设置中,将包权限设为组织内可见或允许my-api仓库访问。
my-api仓库引用NuGet包
编辑API项目文件,添加NuGet源与包引用:<ItemGroup> <PackageReference Include="MyDlls" Version="1.0.0" /> </ItemGroup> <ItemGroup> <PackageSource Include="GitHubPackages"> <Url>https://nuget.pkg.github.com/my-company/index.json</Url> <Username>${{ secrets.GITHUB_ACTOR }}</Username> <Password>${{ secrets.GITHUB_TOKEN }}</Password> </PackageSource> </ItemGroup>构建时自动拉取包:
- name: Build API run: dotnet build MyApi.sln --configuration Release
内容的提问来源于stack exchange,提问作者M Kenyon II
相关产品推荐
相关产品推荐

