下面是我在 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文件夹内的所有文件
你能推荐一下吗?
您要寻找的是从递归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
调用仅返回与名称不匹配的顶级项目,从而排除感兴趣的目录。