Skip to content

Commit 1e96caa

Browse files
🩹 [Patch] Workflow improvements (#12)
This release introduces several improvements to the GitHub Actions workflows and the main PowerShell script for the repository. The main focus is to optimize CI/CD runs by detecting whether a pull request includes changes to important files (such as source code, examples, documentation, or workflow configurations). Build, test, and publish stages are now conditionally executed based on this detection, which helps save CI resources and provides clear feedback to contributors. Additionally, the workflows and dependencies have been updated and refactored for clarity and maintainability. **Workflow optimization and conditional execution:** * Added logic in `src/main.ps1` to detect if a PR changes important files (`src/**`, `examples/**`, `README.md`, `.github/workflows/Process-PSModule.yml`). If not, build/test stages are skipped, and a comment is added to the PR explaining why. This is also reflected in the new `HasImportantChanges` property in both the script and `Settings.schema.json`. * Updated the calculation of release and prerelease conditions to require important file changes for these actions, preventing unnecessary releases for trivial PRs. * Refined job run conditions throughout the workflow to only run build, test, and publish steps if important files have changed, further optimizing CI usage. **Workflow and dependency updates:** * Updated `actions/checkout` and `PSModule/GitHub-Script` actions to their latest versions in all workflow files and `action.yml` for improved security and features. * Changed the script path in `action.yml` to use `src/main.ps1` instead of the old `scripts/main.ps1` location. * Renamed and refactored the release workflow from `Auto-Release` to `Release`, updated its triggering conditions, and switched to the new `PSModule/Release-GHRepository` action for releases. **Cleanup and configuration changes:** * Removed the `.github/linters/.jscpd.json` configuration file, possibly as part of linter or duplication check cleanup. * Renamed and relocated schema and script files for clarity and better organization (e.g., `scripts/Settings.schema.json` to `src/Settings.schema.json`). These changes collectively make the CI/CD workflows more efficient, transparent, and easier to maintain.
1 parent 405b128 commit 1e96caa

File tree

7 files changed

+138
-38
lines changed

7 files changed

+138
-38
lines changed

.github/linters/.jscpd.json

Lines changed: 0 additions & 10 deletions
This file was deleted.

.github/workflows/Action-Test.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ jobs:
2323
steps:
2424
# Need to check out as part of the test, as its a local action
2525
- name: Checkout repo
26-
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
26+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
2727
with:
2828
persist-credentials: false
2929

@@ -39,7 +39,7 @@ jobs:
3939
steps:
4040
# Need to check out as part of the test, as its a local action
4141
- name: Checkout repo
42-
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
42+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
4343
with:
4444
persist-credentials: false
4545

.github/workflows/Linter.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
runs-on: ubuntu-latest
2020
steps:
2121
- name: Checkout repo
22-
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
22+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
2323
with:
2424
fetch-depth: 0
2525
persist-credentials: false
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
name: Auto-Release
1+
name: Release
22

3-
run-name: "Auto-Release - [${{ github.event.pull_request.title }} #${{ github.event.pull_request.number }}] by @${{ github.actor }}"
3+
run-name: "Release - [${{ github.event.pull_request.title }} #${{ github.event.pull_request.number }}] by @${{ github.actor }}"
44

55
on:
6-
pull_request_target:
6+
pull_request:
77
branches:
88
- main
99
types:
@@ -12,6 +12,9 @@ on:
1212
- reopened
1313
- synchronize
1414
- labeled
15+
paths:
16+
- 'action.yml'
17+
- 'src/**'
1518

1619
concurrency:
1720
group: ${{ github.workflow }}-${{ github.ref }}
@@ -22,13 +25,13 @@ permissions:
2225
pull-requests: write # Required to create comments on the PRs
2326

2427
jobs:
25-
Auto-Release:
28+
Release:
2629
runs-on: ubuntu-latest
2730
steps:
2831
- name: Checkout Code
29-
uses: actions/checkout@8e8c483db84b4bee98b60c0593521ed34d9990e8 # v6.0.1
32+
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
3033
with:
3134
persist-credentials: false
3235

33-
- name: Auto-Release
34-
uses: PSModule/Auto-Release@eabd533035e2cb9822160f26f2eda584bd012356 # v1
36+
- name: Release
37+
uses: PSModule/Release-GHRepository@88c70461c8f16cc09682005bcf3b7fca4dd8dc1a # v2.0.1

action.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ runs:
4141
using: composite
4242
steps:
4343
- name: Get-PSModuleSettings
44-
uses: PSModule/GitHub-Script@2010983167dc7a41bcd84cb88e698ec18eccb7ca # v1.7.8
44+
uses: PSModule/GitHub-Script@0097f3bbe3f413f3b577b9bcc600727b0ca3201a # v1.7.10
4545
id: Get-PSModuleSettings
4646
env:
4747
PSMODULE_GET_SETTINGS_INPUT_Name: ${{ inputs.Name }}
@@ -60,4 +60,4 @@ runs:
6060
Verbose: ${{ inputs.Verbose }}
6161
Version: ${{ inputs.Version }}
6262
WorkingDirectory: ${{ inputs.WorkingDirectory }}
63-
Script: ${{ github.action_path }}/scripts/main.ps1
63+
Script: ${{ github.action_path }}/src/main.ps1
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -293,6 +293,10 @@
293293
"type": "string",
294294
"description": "Working directory path"
295295
},
296+
"HasImportantChanges": {
297+
"type": "boolean",
298+
"description": "Indicates if important files have changed in the PR (src/**, examples/**, README.md, .github/workflows/Process-PSModule.yml)"
299+
},
296300
"Run": {
297301
"type": "object",
298302
"description": "Runtime execution flags",

scripts/main.ps1 renamed to src/main.ps1

Lines changed: 119 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ LogGroup 'Calculate Job Run Conditions:' {
219219
} | Format-List | Out-String
220220

221221
$isPR = $env:GITHUB_EVENT_NAME -eq 'pull_request'
222-
$isOpenOrUpdatedPR = $isPR -and $pullRequestAction -in @('opened', 'reopened', 'synchronize')
222+
$isOpenOrUpdatedPR = $isPR -and $pullRequestAction -in @('opened', 'reopened', 'synchronize', 'labeled', 'unlabeled')
223223
$isAbandonedPR = $isPR -and $pullRequestAction -eq 'closed' -and $pullRequestIsMerged -ne $true
224224
$isMergedPR = $isPR -and $pullRequestAction -eq 'closed' -and $pullRequestIsMerged -eq $true
225225
$isNotAbandonedPR = -not $isAbandonedPR
@@ -229,11 +229,101 @@ LogGroup 'Calculate Job Run Conditions:' {
229229
$prLabels = @($pullRequest.labels.name)
230230
$hasPrereleaseLabel = ($prLabels | Where-Object { $prereleaseLabels -contains $_ }).Count -gt 0
231231
$isOpenOrLabeledPR = $isPR -and $pullRequestAction -in @('opened', 'reopened', 'synchronize', 'labeled')
232-
$shouldPrerelease = $isOpenOrLabeledPR -and $hasPrereleaseLabel
232+
233+
# Check if important files have changed in the PR
234+
# Important files for module and docs publish:
235+
# - .github/workflows/Process-PSModule.yml
236+
# - src/**
237+
# - examples/**
238+
# - README.md
239+
$hasImportantChanges = $false
240+
if ($isPR -and $pullRequest.Number) {
241+
LogGroup 'Check for Important File Changes' {
242+
$owner = $env:GITHUB_REPOSITORY_OWNER
243+
$repo = $env:GITHUB_REPOSITORY_NAME
244+
$prNumber = $pullRequest.Number
245+
246+
Write-Host "Fetching changed files for PR #$prNumber..."
247+
$changedFiles = Invoke-GitHubAPI -ApiEndpoint "/repos/$owner/$repo/pulls/$prNumber/files" -Method GET |
248+
Select-Object -ExpandProperty Response |
249+
Select-Object -ExpandProperty filename
250+
251+
Write-Host "Changed files ($($changedFiles.Count)):"
252+
$changedFiles | ForEach-Object { Write-Host " - $_" }
253+
254+
# Define important file patterns
255+
$importantPatterns = @(
256+
'^\.github/workflows/Process-PSModule\.yml$'
257+
'^src/'
258+
'^examples/'
259+
'^README\.md$'
260+
)
261+
262+
# Check if any changed file matches an important pattern
263+
foreach ($file in $changedFiles) {
264+
foreach ($pattern in $importantPatterns) {
265+
if ($file -match $pattern) {
266+
$hasImportantChanges = $true
267+
Write-Host "Important file changed: [$file] (matches pattern: $pattern)"
268+
break
269+
}
270+
}
271+
if ($hasImportantChanges) { break }
272+
}
273+
274+
if ($hasImportantChanges) {
275+
Write-Host '✓ Important files have changed - build/test stages will run'
276+
} else {
277+
Write-Host '✗ No important files changed - build/test stages will be skipped'
278+
279+
# Add a comment to open PRs explaining why build/test is skipped (best-effort, may fail if permissions not granted)
280+
if ($isOpenOrUpdatedPR) {
281+
$commentBody = @"
282+
### No Significant Changes Detected
283+
284+
This PR does not contain changes to files that would trigger a new release:
285+
286+
| Path | Description |
287+
| :--- | :---------- |
288+
| ``src/**`` | Module source code |
289+
| ``examples/**`` | Example scripts |
290+
| ``README.md`` | Documentation |
291+
| ``.github/workflows/Process-PSModule.yml`` | Workflow configuration |
292+
293+
**Build, test, and publish stages will be skipped** for this PR.
294+
295+
If you believe this is incorrect, please verify that your changes are in the correct locations.
296+
"@
297+
try {
298+
Write-Host 'Adding comment to PR about skipped stages...'
299+
$apiParams = @{
300+
Method = 'POST'
301+
ApiEndpoint = "/repos/$owner/$repo/issues/$prNumber/comments"
302+
Body = @{ body = $commentBody } | ConvertTo-Json
303+
}
304+
$null = Invoke-GitHubAPI @apiParams
305+
Write-Host '✓ Comment added successfully'
306+
} catch {
307+
Write-Warning "Could not add PR comment (may need 'issues: write' permission): $_"
308+
}
309+
}
310+
}
311+
}
312+
} else {
313+
# Not a PR event or no PR number - consider as having important changes (e.g., workflow_dispatch, schedule)
314+
$hasImportantChanges = $true
315+
Write-Host 'Not a PR event or missing PR number - treating as having important changes'
316+
}
317+
318+
# Prerelease requires both: prerelease label AND important file changes
319+
# No point creating a prerelease if only non-module files changed
320+
$shouldPrerelease = $isOpenOrLabeledPR -and $hasPrereleaseLabel -and $hasImportantChanges
233321

234322
# Determine ReleaseType - what type of release to create
235323
# Values: 'Release', 'Prerelease', 'None'
236-
$releaseType = if ($isMergedPR -and $isTargetDefaultBranch) {
324+
# Release only happens when important files changed (actual module code/docs)
325+
# Merged PRs without important changes should only trigger cleanup, not a new release
326+
$releaseType = if ($isMergedPR -and $isTargetDefaultBranch -and $hasImportantChanges) {
237327
'Release'
238328
} elseif ($shouldPrerelease) {
239329
'Prerelease'
@@ -252,6 +342,7 @@ LogGroup 'Calculate Job Run Conditions:' {
252342
hasPrereleaseLabel = $hasPrereleaseLabel
253343
shouldPrerelease = $shouldPrerelease
254344
ReleaseType = $releaseType
345+
HasImportantChanges = $hasImportantChanges
255346
} | Format-List | Out-String
256347
}
257348

@@ -426,35 +517,47 @@ if ($settings.Test.Skip) {
426517
# Calculate job-specific conditions and add to settings
427518
LogGroup 'Calculate Job Run Conditions:' {
428519
# Calculate if prereleases should be cleaned up:
429-
# True if (Release or Abandoned PR) AND user has AutoCleanup enabled (defaults to true)
430-
$shouldAutoCleanup = (($releaseType -eq 'Release') -or $isAbandonedPR) -and ($settings.Publish.Module.AutoCleanup -eq $true)
520+
# True if (Release, merged PR to default branch, or Abandoned PR) AND user has AutoCleanup enabled (defaults to true)
521+
# Even if no important files changed, we still want to cleanup prereleases when merging to default branch
522+
$isReleaseOrMergedOrAbandoned = (
523+
($releaseType -eq 'Release') -or
524+
($isMergedPR -and $isTargetDefaultBranch) -or
525+
$isAbandonedPR
526+
)
527+
$shouldAutoCleanup = $isReleaseOrMergedOrAbandoned -and ($settings.Publish.Module.AutoCleanup -eq $true)
431528

432529
# Update Publish.Module with computed release values
433530
$settings.Publish.Module | Add-Member -MemberType NoteProperty -Name ReleaseType -Value $releaseType -Force
434531
$settings.Publish.Module.AutoCleanup = $shouldAutoCleanup
435532

533+
# For open PRs, we only want to run build/test stages if important files changed.
534+
# For merged PRs, workflow_dispatch, schedule - $hasImportantChanges is already true.
535+
# Note: $shouldPrerelease already requires $hasImportantChanges, so no separate check needed.
536+
$shouldRunBuildTest = $isNotAbandonedPR -and $hasImportantChanges
537+
436538
# Create Run object with all job-specific conditions
437539
$run = [pscustomobject]@{
438540
LintRepository = $isOpenOrUpdatedPR -and (-not $settings.Linter.Skip)
439-
BuildModule = $isNotAbandonedPR -and (-not $settings.Build.Module.Skip)
440-
TestSourceCode = $isNotAbandonedPR -and ($null -ne $settings.TestSuites.SourceCode)
441-
LintSourceCode = $isNotAbandonedPR -and ($null -ne $settings.TestSuites.SourceCode)
442-
TestModule = $isNotAbandonedPR -and ($null -ne $settings.TestSuites.PSModule)
443-
BeforeAllModuleLocal = $isNotAbandonedPR -and ($null -ne $settings.TestSuites.Module)
444-
TestModuleLocal = $isNotAbandonedPR -and ($null -ne $settings.TestSuites.Module)
541+
BuildModule = $shouldRunBuildTest -and (-not $settings.Build.Module.Skip)
542+
TestSourceCode = $shouldRunBuildTest -and ($null -ne $settings.TestSuites.SourceCode)
543+
LintSourceCode = $shouldRunBuildTest -and ($null -ne $settings.TestSuites.SourceCode)
544+
TestModule = $shouldRunBuildTest -and ($null -ne $settings.TestSuites.PSModule)
545+
BeforeAllModuleLocal = $shouldRunBuildTest -and ($null -ne $settings.TestSuites.Module)
546+
TestModuleLocal = $shouldRunBuildTest -and ($null -ne $settings.TestSuites.Module)
445547
AfterAllModuleLocal = $true # Always runs if Test-ModuleLocal was not skipped
446-
GetTestResults = $isNotAbandonedPR -and (-not $settings.Test.TestResults.Skip) -and (
548+
GetTestResults = $shouldRunBuildTest -and (-not $settings.Test.TestResults.Skip) -and (
447549
($null -ne $settings.TestSuites.SourceCode) -or ($null -ne $settings.TestSuites.PSModule) -or ($null -ne $settings.TestSuites.Module)
448550
)
449-
GetCodeCoverage = $isNotAbandonedPR -and (-not $settings.Test.CodeCoverage.Skip) -and (
551+
GetCodeCoverage = $shouldRunBuildTest -and (-not $settings.Test.CodeCoverage.Skip) -and (
450552
($null -ne $settings.TestSuites.PSModule) -or ($null -ne $settings.TestSuites.Module)
451553
)
452554
PublishModule = ($releaseType -ne 'None') -or $shouldAutoCleanup
453-
BuildDocs = $isNotAbandonedPR -and (-not $settings.Build.Docs.Skip)
454-
BuildSite = $isNotAbandonedPR -and (-not $settings.Build.Site.Skip)
455-
PublishSite = $isMergedPR -and $isTargetDefaultBranch
555+
BuildDocs = $shouldRunBuildTest -and (-not $settings.Build.Docs.Skip)
556+
BuildSite = $shouldRunBuildTest -and (-not $settings.Build.Site.Skip)
557+
PublishSite = $isMergedPR -and $isTargetDefaultBranch -and $hasImportantChanges
456558
}
457559
$settings | Add-Member -MemberType NoteProperty -Name Run -Value $run
560+
$settings | Add-Member -MemberType NoteProperty -Name HasImportantChanges -Value $hasImportantChanges
458561

459562
Write-Host 'Job Run Conditions:'
460563
$run | Format-List | Out-String

0 commit comments

Comments
 (0)