Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions .github/workflows/Release-Decision.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Release decision

on:
pull_request:
branches:
- main
types:
- opened
- reopened
- synchronize
- labeled
- unlabeled
- edited

concurrency:
group: ${{ github.workflow }}-${{ github.event.number }}
cancel-in-progress: true

permissions:
contents: read
pull-requests: read

jobs:
ReleaseDecision:
name: Release decision
runs-on: ubuntu-latest
steps:
- name: Check out the candidate merge
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
ref: ${{ github.sha }}
persist-credentials: false

- name: Verify the pull request snapshot
shell: pwsh
env:
GH_TOKEN: ${{ github.token }}
run: $null = & '.\src\Test-ReleaseDecisionContext.ps1'

- name: Calculate the release decision
id: calculate
uses: ./
env:
PSMODULE_AUTO_RELEASE_CHECK_PUBLISHER: 'true'
with:
Mode: calculate

- name: Recheck and report the decision
shell: pwsh
env:
GH_TOKEN: ${{ github.token }}
RELEASE_DECISION: ${{ steps.calculate.outputs.Decision }}
RELEASE_VERSION: ${{ steps.calculate.outputs.ReleaseVersion }}
RELEASE_BUMP_SOURCE: ${{ steps.calculate.outputs.BumpSource }}
run: |
$ErrorActionPreference = 'Stop'
$checked = & '.\src\Test-ReleaseDecisionContext.ps1'
$decision = $env:RELEASE_DECISION
$version = $env:RELEASE_VERSION
$source = $env:RELEASE_BUMP_SOURCE
$hasVersion = -not [string]::IsNullOrWhiteSpace($version)
if ($decision -cnotin @('release', 'skip') -or -not $source -or
($decision -ceq 'release') -ne $hasVersion) {
throw 'Calculate did not return a complete release decision.'
}
[ordered]@{
PullRequest = $checked.Number
BaseBranch = $checked.BaseRef
BaseSha = $checked.BaseSha
HeadSha = $checked.HeadSha
MergeSha = $checked.MergeSha
Decision = $decision
BumpSource = $source
Candidate = $version
} | ConvertTo-Json
17 changes: 14 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,19 @@ The action reads the current labels and latest stable release from the target re
If GitHub reports no latest stable release, the action uses `0.0.0` as the baseline.
A failed read, a stale pull request, or an invalid decision fails the run without outputs.

Use a separate read-only job for the [required release decision check][release-decision-check].
Include `labeled` and `unlabeled` to check label changes. Do not use a path filter to skip the check.
Apply `release:skip` when a change does not need a release.
### Check a pull request

This repository's `Release decision` job runs on pull requests to `main`, including forks and changes that affect only documentation.
It checks the candidate merge commit and uses the release settings and literal inputs from the local publisher in `.github/workflows/Release.yml`.
The publisher must check out this repository at the workspace root before it invokes the local action.
If the workflow route or its inputs cannot be resolved, the check fails. Do not use expressions for publisher inputs that this check must evaluate.
The default settings file is optional. If the publisher names a file, include it in the candidate checkout.

The job uses read-only permissions. It checks the current labels and revisions again before it reports its decision.
Apply `release:skip` when a change does not need a release. The job still runs and passes for a valid skip.
The job is separate from `Action-Test` and is advisory. It does not prevent merges, and a label rerun alone does not invalidate a previous pass.
See the [required check design][release-decision-check] for future enforcement.

If a fork contribution needs publication, follow [Prerelease a fork contribution][fork-prerelease].

### Publish a version from Calculate
Expand Down Expand Up @@ -281,6 +291,7 @@ These outputs are available only in Calculate mode. For example, read `steps.cal
| Name | Description |
| --- | --- |
| `Decision` | `release` if the version is valid. `skip` for a valid `release:skip` decision. |
| `BumpSource` | The release label or settings layer that supplied the decision. |
| `ReleaseVersion` | The calculated SemVer string without `VersionPrefix`. Empty for `skip`. |
| `CalculatedAt` | The ISO 8601 UTC instant used to calculate a date-dependent prerelease. Empty for other results. |

Expand Down
13 changes: 12 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ outputs:
Decision:
description: The Calculate decision, release or skip.
value: ${{ fromJSON(steps.release.outputs.result || '{}').Decision }}
BumpSource:
description: The release label or settings layer that supplied the Calculate decision.
value: ${{ fromJSON(steps.release.outputs.result || '{}').BumpSource }}
ReleaseVersion:
description: The unprefixed semantic version. Empty for skip.
value: ${{ fromJSON(steps.release.outputs.result || '{}').ReleaseVersion }}
Expand Down Expand Up @@ -171,7 +174,15 @@ runs:
PSMODULE_AUTO_RELEASE_INPUT_Version: ${{ inputs.Version }}
PSMODULE_AUTO_RELEASE_INPUT_Prerelease: ${{ inputs.Prerelease }}
run: |
$null = & (Join-Path -Path $env:PSMODULE_AUTO_RELEASE_ACTION_PATH -ChildPath 'src\Initialize-ReleaseConfiguration.ps1')
$configuration = @{}
if ($env:PSMODULE_AUTO_RELEASE_CHECK_PUBLISHER -ceq 'true') {
if ($env:PSMODULE_AUTO_RELEASE_INPUT_Mode -cne 'calculate') {
throw 'Publisher settings can only be evaluated in Calculate mode.'
}
$configuration.PublisherBranch = $env:GITHUB_BASE_REF
}
$bootstrap = Join-Path -Path $env:PSMODULE_AUTO_RELEASE_ACTION_PATH -ChildPath 'src\Initialize-ReleaseConfiguration.ps1'
$null = & $bootstrap @configuration

- name: Release-GHRepository
id: release
Expand Down
129 changes: 129 additions & 0 deletions src/Get-ReleaseWorkflowInputs.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
#Requires -Version 7.2
#Requires -Modules powershell-yaml

<#
.SYNOPSIS
Read the supported inputs from the local release workflow.

.DESCRIPTION
Select the publisher for the target branch. Return only literal input values.
Do not execute workflow expressions or guess unresolved values.

.OUTPUTS
System.Collections.IDictionary
#>
[OutputType([System.Collections.IDictionary])]
[CmdletBinding()]
param(
# The candidate publisher workflow file.
[Parameter(Mandatory)]
[string] $Path,

# The pull request target branch, without refs/heads/.
[Parameter(Mandatory)]
[string] $TargetBranch,

# The settings supported by the same action that publishes.
[Parameter(Mandatory)]
[string[]] $SettingNames
)

$ErrorActionPreference = 'Stop'
$workflow = ConvertFrom-Yaml -Yaml ([IO.File]::ReadAllText($Path)) -Ordered
$trigger = $workflow.on.pull_request
if ($trigger -isnot [System.Collections.IDictionary]) {
throw 'The release workflow must have a supported pull_request trigger.'
}
foreach ($filter in $trigger.GetEnumerator()) {
if ($filter.Key -cnotin @('branches', 'types')) {
throw "The release workflow has an unsupported pull_request filter [$($filter.Key)]."
}
}
if ($trigger.branches -isnot [System.Collections.IList] -or
@($trigger.branches | Where-Object { $_ -isnot [string] -or $_ -match '[!+*?\[\]\\]' }).Count -gt 0) {
throw 'The release workflow has an unsupported branch filter. List the target branch names literally.'
}
if ($trigger.branches -cnotcontains $TargetBranch) {
throw "The release workflow does not handle [$TargetBranch] with an explicit pull_request branch filter."
}
if ($trigger.types -isnot [System.Collections.IList] -or $trigger.types -cnotcontains 'closed') {
throw 'The release workflow must handle closed pull requests.'
}

$job = $workflow.jobs.Release
$publisher = @($job.steps | Where-Object { $_.uses -ceq './' })
if ($publisher.Count -ne 1) {
throw 'The release workflow must have exactly one local action invocation in the Release job.'
}
$sameRepository = 'github.event.pull_request.head.repo.full_name == github.repository'
if ($job.Contains('if') -and
($job.if -isnot [string] -or ($job.if.Trim() -replace '\s+', ' ') -cnotin @($sameRepository, ('${{ ' + $sameRepository + ' }}')))) {
throw 'The release check cannot resolve a conditional publisher job other than the same-repository guard.'
}
if ($publisher[0].Contains('if') -or $job.Contains('strategy') -or $job.Contains('needs')) {
throw 'The release check cannot resolve a conditional publisher step, publisher dependencies, or a matrix of publisher inputs.'
}

$checkouts = @(foreach ($step in $job.steps) {
if ([object]::ReferenceEquals($step, $publisher[0])) {
break
}
if ($step.uses -clike 'actions/checkout@*') {
$step
}
})
if ($checkouts.Count -ne 1 -or $checkouts[0].Contains('if')) {
throw 'The release check requires one unconditional checkout before the local publisher.'
}
if ($checkouts[0].Contains('with')) {
$checkoutInputs = $checkouts[0].with
if ($checkoutInputs -isnot [System.Collections.IDictionary]) {
throw 'The publisher checkout with block must contain a mapping.'
}
if ($checkoutInputs.Contains('repository') -and
($checkoutInputs.repository -isnot [string] -or $checkoutInputs.repository -cne '${{ github.repository }}')) {
throw 'The publisher checkout must use the current repository.'
}
if (($checkoutInputs.Contains('path') -and $checkoutInputs.path -cnotin @('.', './')) -or $checkoutInputs.Contains('sparse-checkout')) {
throw 'The publisher checkout must use the workspace root without a sparse checkout.'
}
$currentReleaseRef = @(
'${{ github.event.action == ''closed'' && github.event.pull_request.merged'
'&& github.event.pull_request.merge_commit_sha || github.event.pull_request.base.sha }}'
) -join ' '
$supportedRefs = @('${{ github.sha }}', '${{ github.event.pull_request.merge_commit_sha }}', $currentReleaseRef)
if ($checkoutInputs.Contains('ref') -and
($checkoutInputs.ref -isnot [string] -or ($checkoutInputs.ref.Trim() -replace '\s+', ' ') -cnotin $supportedRefs)) {
throw 'The publisher checkout ref must come from the pull request merge rather than another revision.'
}
}

$inputs = @{}
if (-not $publisher[0].Contains('with')) {
return $inputs
}

$overrides = $publisher[0].with
if ($overrides -isnot [System.Collections.IDictionary]) {
throw 'The publisher with block must contain a mapping of literal inputs.'
}
foreach ($override in $overrides.GetEnumerator()) {
$key = $override.Key
$value = $override.Value
$name = @($SettingNames + 'SettingsFilePath').Where({ $_ -eq $key }, 'First')[0]
if (-not $name) {
throw "The publisher has an unsupported workflow input [$key]."
}
if ($inputs.ContainsKey($name)) {
throw "The publisher specifies workflow input [$name] more than once."
}

if ($value -is [bool]) {
$inputs[$name] = $value.ToString().ToLowerInvariant()
} elseif ($value -is [string] -and -not $value.Contains('${{')) {
$inputs[$name] = $value
} else {
throw "Use a literal string or Boolean for publisher input [$name]."
}
}
$inputs
44 changes: 39 additions & 5 deletions src/Initialize-ReleaseConfiguration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ param(

# The runner's output file; local callers can omit it to return settings only.
[Parameter()]
[string] $OutputPath = [Environment]::GetEnvironmentVariable('GITHUB_OUTPUT')
[string] $OutputPath = [Environment]::GetEnvironmentVariable('GITHUB_OUTPUT'),

# Evaluate this branch's publisher inputs instead of the check's own inputs.
[Parameter()]
[string] $PublisherBranch
)

$ErrorActionPreference = 'Stop'
$checkPublisher = $PSBoundParameters.ContainsKey('PublisherBranch')

Import-Module -Name (Join-Path -Path $PSScriptRoot -ChildPath 'Release-GHRepository.Helpers.psm1') -Force
if ([string]::IsNullOrWhiteSpace($WorkspacePath) -or -not [IO.Path]::IsPathRooted($WorkspacePath) -or
Expand Down Expand Up @@ -67,9 +72,25 @@ try {
$actionDefinitionPath = [IO.Path]::GetFullPath((Join-Path -Path $PSScriptRoot -ChildPath '..\action.yml'))
$metadata = Get-ReleaseSettingSchema -Path $actionDefinitionPath
Write-Information "Reading action release standards [$actionDefinitionPath]" -InformationAction Continue
$inputs = @{}
foreach ($name in $metadata.Keys) {
$inputs[$name] = [Environment]::GetEnvironmentVariable("PSMODULE_AUTO_RELEASE_INPUT_$name")
if ($checkPublisher) {
$publisherParameters = @{
Path = Join-Path -Path $WorkspacePath -ChildPath '.github\workflows\Release.yml'
TargetBranch = $PublisherBranch
SettingNames = @($metadata.Keys)
}
$publisherReader = Join-Path -Path $PSScriptRoot -ChildPath 'Get-ReleaseWorkflowInputs.ps1'
$inputs = & $publisherReader @publisherParameters
$settingsFilePath = $inputs.SettingsFilePath
} else {
$inputs = @{}
foreach ($name in $metadata.Keys) {
$inputs[$name] = [Environment]::GetEnvironmentVariable("PSMODULE_AUTO_RELEASE_INPUT_$name")
}
$settingsFilePath = [Environment]::GetEnvironmentVariable('PSMODULE_AUTO_RELEASE_INPUT_SettingsFilePath')
}
$explicitPublisherFile = $checkPublisher -and $inputs.ContainsKey('SettingsFilePath')
if ($explicitPublisherFile) {
$inputs.Remove('SettingsFilePath')
}

$initialDirectory = $WorkspacePath
Expand All @@ -82,7 +103,6 @@ try {
}
$initialDirectory = ConvertTo-ReleaseSettingValue @initialParameters
}
$settingsFilePath = [Environment]::GetEnvironmentVariable('PSMODULE_AUTO_RELEASE_INPUT_SettingsFilePath')
if ($null -eq $settingsFilePath) {
$actionDefinition = ConvertFrom-Yaml -Yaml ([IO.File]::ReadAllText($actionDefinitionPath))
$settingsFilePath = $actionDefinition.inputs.SettingsFilePath.default
Expand All @@ -93,6 +113,12 @@ try {
BaseDirectory = $initialDirectory
}
$settingsFilePath = Resolve-ReleaseFileSystemPath @pathParameters
if ($checkPublisher) {
$relativePath = [IO.Path]::GetRelativePath($WorkspacePath, $settingsFilePath)
if ([IO.Path]::IsPathRooted($relativePath) -or $relativePath -match '\A\.\.(?:[\\/]|$)') {
throw 'The publisher SettingsFilePath must be inside the candidate checkout.'
}
}
if (Test-Path -LiteralPath $settingsFilePath -PathType Container) {
throw 'SettingsFilePath must select a file, not a directory.'
}
Expand All @@ -104,6 +130,8 @@ try {
if ($null -eq $configuration) {
throw "Release settings file [$loggedPath] must be a mapping of supported settings."
}
} elseif ($explicitPublisherFile) {
throw "The publisher settings file [$loggedPath] was not found in the candidate checkout."
} else {
Write-Information "Settings file not found at [$loggedPath]" -InformationAction Continue
}
Expand All @@ -115,6 +143,12 @@ try {
Configuration = $configuration
}
$settings = Resolve-ReleaseConfiguration @configurationParameters
if ($checkPublisher) {
if ($settings.Mode -cne 'publish') {
throw 'The effective publisher Mode must be publish.'
}
$settings.Mode = 'calculate'
}
$outputs = [ordered]@{}
$releaseSettings = [ordered]@{}
foreach ($name in $metadata.Keys) {
Expand Down
Loading
Loading