GitHub Workflow - 获取触发拉取请求的路径

问题描述 投票:0回答:1

我正在处理一个包含多个项目的单一存储库,每个解决方案都在不同的文件夹中。我想要实现的是,如果对该文件夹中的代码进行了更改,则对该文件夹运行扫描操作。我想在 pull_request 路径触发器中设置每个解决方案的所有路径,然后根据触发工作流的路径对该文件夹运行扫描。

我在想做这样的事情:

name: scan

on:
  pull_request:
    paths:
      - 'path/to/folder/*'
      - 'path/to/anotherfolder/*'

jobs:
  output_path:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2
      - name: Determine triggering path
        id: determine_path
        run: |
          # Get the list of paths being monitored
          monitored_paths=$(echo "${{ github.event.pull_request.paths }}" | tr -d '[] ')

          # Loop through each path
          for path in $monitored_paths; do
            # Check if the modified files include this path
            if echo "${{ github.event.pull_request.changed_files }}" | grep -q "$path"; then
              # Set the output variables and exit the loop
              echo "::set-output name=triggering_path::$path"
              break
            fi
          done
      - name: Output path
        run: |
          echo "The following path triggered this job: ${{ steps.determine_path.outputs.triggering_path }}"```
github path github-actions pull-request
1个回答
0
投票

我最终通过以下方式解决了它:

name: scan
on:
  pull_request:
    branches:
      - main
    types:
      - opened
      - synchronize
    paths: ["path/to/folder/*", "path/to/anotherfolder/*"]

env:
  paths: 'path/to/folder/, path/to/anotherfolder/'

jobs:
  output_path:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Get changed files
        id: changes
        run: |
          echo "files=$(git diff --name-only --diff-filter=ACMRT ${{ github.event.pull_request.base.sha }} ${{ github.sha }} | xargs)" >> $GITHUB_OUTPUT

      - name: Determine triggering path and sonar project key
        id: determine_path
        shell: bash
        run: |
          # Show paths
          echo "Paths: ${{ env.paths }}"

          # Get the list of paths being monitored
          monitored_paths=$(echo "${{ env.paths }}" | tr "," " ")
          echo "Monitored paths: $monitored_paths"
          echo "Changed files: ${{ steps.changes.outputs.files }}"
          echo "If you see here files that you have not modified, please update your branch with changes from main."

          # Loop through each path
          for path in $monitored_paths
          do
            # Check if the modified files include this path
            if echo "${{ steps.changes.outputs.files }}" | grep -q "$path"; then
              # Set the output variable and exit the loop
              echo "triggering_path is $path"
              echo "triggering_path=$path" >> $GITHUB_OUTPUT
              break
            fi
          done

然后您可以使用表达式使用 determine_path 的输出

${{ steps.determine_path.outputs.triggering_path }}

© www.soinside.com 2019 - 2024. All rights reserved.