GitHub Actions如何仅发布同一仓库中发生变更的Function Apps项目
单仓库多Azure Function App增量发布实现方案
以下是无需拆分代码仓库,仅发布代码存在变更的Function App的可落地实现方案:
核心逻辑
通过GitHub Actions的路径变更检测能力,结合矩阵构建能力,只触发对应变更目录下的Function App的发布流程,未变更的应用自动跳过执行。
实现步骤
1. 规范仓库目录结构
首先确保每个Function App的所有代码都存放在独立的子目录中,示例结构如下:
仓库根目录/ ├── .github/ │ └── workflows/ │ └── deploy-function-apps.yml # 后续新建的发布工作流文件 ├── FuncApp1/ # 第一个Function App的全部代码 ├── FuncApp2/ # 第二个Function App的全部代码 ├── FuncApp3/ ├── FuncApp4/ ├── FuncApp5/ ├── Shared/ # 可选,存放多个应用共享的公共类库/工具代码 └── 解决方案文件.sln
2. 编写GitHub Actions发布工作流
在.github/workflows目录下新建deploy-function-apps.yml文件,内容如下:
name: 按需发布Azure Function App on: push: branches: [ main ] # 替换为你需要触发发布的分支名 env: DOTNET_VERSION: '6.0.x' # 替换为你使用的运行时版本,非.NET应用可删除或修改为对应环境变量 jobs: # 第一步:检测各应用目录的代码变更情况 check-changes: runs-on: ubuntu-latest outputs: func1_changed: ${{ steps.filter.outputs.FuncApp1 }} func2_changed: ${{ steps.filter.outputs.FuncApp2 }} func3_changed: ${{ steps.filter.outputs.FuncApp3 }} func4_changed: ${{ steps.filter.outputs.FuncApp4 }} func5_changed: ${{ steps.filter.outputs.FuncApp5 }} steps: - uses: actions/checkout@v4 - name: 检测变更目录 uses: dorny/paths-filter@v3 id: filter with: filters: | FuncApp1: - 'FuncApp1/**' - 'Shared/**' # 如果该应用依赖公共目录,就加上这行 FuncApp2: - 'FuncApp2/**' - 'Shared/**' FuncApp3: - 'FuncApp3/**' - 'Shared/**' FuncApp4: - 'FuncApp4/**' - 'Shared/**' FuncApp5: - 'FuncApp5/**' - 'Shared/**' # 第二步:仅发布发生变更的应用 deploy-changed-apps: needs: check-changes runs-on: windows-latest # 替换为你Function App对应的运行系统,可选ubuntu-latest、macos-latest strategy: fail-fast: false matrix: include: - app_name: FuncApp1 publish_profile: AZURE_FUNC1_PUBLISH_PROFILE is_changed: ${{ needs.check-changes.outputs.func1_changed == 'true' }} - app_name: FuncApp2 publish_profile: AZURE_FUNC2_PUBLISH_PROFILE is_changed: ${{ needs.check-changes.outputs.func2_changed == 'true' }} - app_name: FuncApp3 publish_profile: AZURE_FUNC3_PUBLISH_PROFILE is_changed: ${{ needs.check-changes.outputs.func3_changed == 'true' }} - app_name: FuncApp4 publish_profile: AZURE_FUNC4_PUBLISH_PROFILE is_changed: ${{ needs.check-changes.outputs.func4_changed == 'true' }} - app_name: FuncApp5 publish_profile: AZURE_FUNC5_PUBLISH_PROFILE is_changed: ${{ needs.check-changes.outputs.func5_changed == 'true' }} # 仅当应用发生变更时才执行当前矩阵任务 if: matrix.is_changed == true steps: - uses: actions/checkout@v4 # 以下为.NET应用的构建步骤,非.NET应用替换为对应语言的构建打包逻辑即可 - name: 配置.NET环境 uses: actions/setup-dotnet@v4 with: dotnet-version: ${{ env.DOTNET_VERSION }} - name: 构建项目 run: dotnet build ./${{ matrix.app_name }} --configuration Release - name: 打包发布文件 run: dotnet publish ./${{ matrix.app_name }} -c Release -o ./publish_output - name: 发布到Azure Function App uses: Azure/functions-action@v1 with: app-name: ${{ matrix.app_name }} slot-name: 'Production' publish-profile: ${{ secrets[matrix.publish_profile] }} package: ./publish_output
3. 完成前置配置
- 提前将5个Function App的发布配置文件(Publish Profile)分别存入GitHub仓库的Secrets中,命名和工作流中
publish_profile字段对应即可 - 若使用非.NET的语言开发Function App,仅需替换工作流中对应的构建打包步骤,路径检测和增量触发的逻辑完全通用
- 如果不需要在公共代码变更时触发所有关联应用发布,删除对应应用过滤规则里的公共目录路径即可
效果验证
配置完成后,每次推送代码时,工作流会自动跳过所有未发生代码变更的Function App的发布流程,仅执行变更应用的构建发布,可大幅减少发布耗时。
内容的提问来源于stack exchange,提问作者Niels Brinch
相关产品推荐
相关产品推荐

