改善 GitHub Actions PowerShell 中的搜索和存档时间

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

下面是我在 PowerShell 上的工作流程,搜索以逗号分隔列表形式提供的文件和文件夹到

itemsToInclude
:

      $zipFileName = "${{ github.workspace }}\package-$env:GITHUB_RUN_NUMBER.zip"

      cd "${{ github.workspace }}"

      $itemsToInclude = $env:INPUTS_FILESINZIP

      Write-Host "itemsToInclude is- $itemsToInclude"

 

      if (-not (Test-Path $zipFileName)) {

        $null = New-Item $zipFileName -ItemType File

      }

      $workspace = "${{ github.workspace }}"

      # Define the directories to exclude

      $excludeDirectories = @('DevOps')
      $excludeExtensions = @('.java', '.class')


      # Include specific files and folders as per the comma-separated list

      Write-Host 'Include specific files and folders as per the comma-separated list'

      $itemsToIncludeList = $itemsToInclude -split ','
      $filesToInclude = Get-ChildItem $workspace -Recurse -File  -Exclude $excludeDirectories | Where-Object {
      $itemName = $_.Name

      Write-Host "Checking file: $itemName"

      $itemsToIncludeList -contains $itemName

      }

 
      $filesToInclude | ForEach-Object {

        $newZipEntrySplat = @{

          EntryPath   = $_.FullName.Substring($workspace.Length)
          SourcePath  = $_.FullName
          Destination = $zipFileName

          }

          Write-Host "Adding file: $($_.FullName)"
          New-ZipEntry @newZipEntrySplat

        }

      Write-Host "Zip file created: $zipFileName"

    env:
      INPUTS_FILESINZIP: ${{ inputs.filesinzip }}

搜索所需文件并将其包含在 ZIP 中所需的时间是可以接受的。

因此,我希望排除文件夹

DevOps
以及所有具有扩展名
.java
.class
的文件,以便减少此步骤所需的时间。

不幸的是,

-Exclude
选项不起作用,我可以看到
Checking file:

的输出中列出的AreDevOps文件夹内的所有文件

你能推荐一下吗?

powershell github-actions get-childitem
1个回答
0
投票

您要寻找的是从递归Get-ChildItem调用

的枚举中排除整个目录
子树

不幸的是,

直接在Windows PowerShell中受支持,而且从PowerShell(核心)7.4起仍然不支持:

  • -Include

    -Exclude
    参数仅作用于项目(文件或目录)
    名称(不适用于路径)。

  • 他们只对匹配的项目

    自己进行操作。也就是说,如果目录名称匹配,其子树仍会递归到

GitHub 问题 #15159 是一个功能请求,还支持排除匹配子目录的整个子树


解决方法:
如果您要排除的子树的子目录都是

顶级,即目标目录的直接子项,您可以使用两步方法:

$filesToInclude = Get-ChildItem $workspace -Exclude $excludeDirectories | Get-ChildItem -Recurse -File -Exclude $excludeExtensions

  • 第一个

    Get-ChildItem

     调用仅返回与名称不匹配的顶级项目,从而排除感兴趣的目录。

  • 第二次调用仅使用文件扩展名排除对未排除的项目进行递归。

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