Repository navigation
packaging: template winget manifests and add release publishing #500
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
inqrphl
wants to merge
8
commits into
main
Choose a base branch
from
winget-templates
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
75e1eac
packaging: template winget manifests and add release publishing
inqrphl e843401
buildtools: make winget shell scripts portable
inqrphl 93f174f
buildtools: satisfy shellcheck
inqrphl 4b21c5d
ci: do not require a current winget-pkgs fork
inqrphl 1815c85
ci: use separate fork and PR tokens
inqrphl 87814e8
ci: rename fork token to WINGET_FORK_TOKEN
inqrphl 4da71e9
Merge branch 'main' into winget-templates
inqrphl fb7d13f
clarify winget documentation a bit
inqrphl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| name: Publish to WinGet | ||
|
|
||
| # This workflow publishes the generated WinGet manifests to microsoft/winget-pkgs. | ||
| # It does not generate the manifests. | ||
| # The release workflow attaches the manifest zip to the release. | ||
| # WINGET_FORK_TOKEN is a fine-grained PAT for the fork. | ||
| # WINGET_FORK_TOKEN needs Contents: Read and write on WINGET_FORK_OWNER/winget-pkgs. | ||
| # WINGET_PR_TOKEN is a classic PAT with public_repo. | ||
| # WINGET_PR_TOKEN opens the PR in microsoft/winget-pkgs. | ||
| # The fork does not need to be current. | ||
| # The PR adds only the new version directory. | ||
| # Set WINGET_FORK_OWNER to the fork owner. | ||
| # Set WINGET_AUTOPUBLISH to true after the first submission. | ||
|
|
||
| on: | ||
| release: | ||
| types: [published] | ||
| workflow_dispatch: | ||
| inputs: | ||
| release_tag: | ||
| description: "Existing release tag to submit (for example v1.02)" | ||
| required: true | ||
| type: string | ||
| dry_run: | ||
| description: "Print the manifests and do not create a branch or PR" | ||
| required: false | ||
| default: true | ||
| type: boolean | ||
|
|
||
| permissions: | ||
| contents: read | ||
|
|
||
| concurrency: | ||
| group: winget-${{ github.event.release.tag_name || inputs.release_tag }} | ||
| cancel-in-progress: false | ||
|
|
||
| jobs: | ||
| publish: | ||
| if: github.event_name == 'workflow_dispatch' || (github.event_name == 'release' && !github.event.release.prerelease && vars.WINGET_AUTOPUBLISH == 'true') | ||
| runs-on: windows-2025 | ||
| env: | ||
| GH_REPO: ${{ github.repository }} | ||
| RELEASE_TAG: ${{ github.event.release.tag_name || inputs.release_tag }} | ||
| WINGET_FORK_OWNER: ${{ vars.WINGET_FORK_OWNER }} | ||
| steps: | ||
| - name: "Download release manifests" | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| shell: pwsh | ||
| run: | | ||
| $ErrorActionPreference = 'Stop' | ||
| $tag = $env:RELEASE_TAG | ||
| if (-not $tag) { throw 'No release tag was provided.' } | ||
| $tagVersion = $tag -replace '^v', '' | ||
| $downloadDir = Join-Path $env:RUNNER_TEMP 'winget-download' | ||
| $manifestDir = Join-Path $env:RUNNER_TEMP 'winget-manifests' | ||
| New-Item -ItemType Directory -Force -Path $downloadDir | Out-Null | ||
| New-Item -ItemType Directory -Force -Path $manifestDir | Out-Null | ||
| & gh release download $tag --repo $env:GH_REPO --pattern "snclient-$tagVersion-winget-manifests.zip" --dir $downloadDir | ||
| if ($LASTEXITCODE -ne 0) { throw "Could not download the manifest asset for $tag." } | ||
| $zip = Get-ChildItem -Path $downloadDir -Filter '*.zip' | Select-Object -First 1 | ||
| if (-not $zip) { throw 'The manifest zip is missing.' } | ||
| Expand-Archive -Path $zip.FullName -DestinationPath $manifestDir -Force | ||
| Get-ChildItem -Path $manifestDir -Filter '*.yaml' | Select-Object Name, Length | ||
|
|
||
| - name: "Dry run" | ||
| if: ${{ github.event_name == 'workflow_dispatch' && inputs.dry_run == true }} | ||
| shell: pwsh | ||
| run: | | ||
| $manifestDir = Join-Path $env:RUNNER_TEMP 'winget-manifests' | ||
| Get-ChildItem -Path $manifestDir -Filter '*.yaml' | ForEach-Object { | ||
| Write-Output "===== $($_.Name) =====" | ||
| Get-Content -LiteralPath $_.FullName | ||
| } | ||
|
|
||
| - name: "Create fork branch" | ||
| id: create_branch | ||
| if: ${{ github.event_name != 'workflow_dispatch' || inputs.dry_run != true }} | ||
| env: | ||
| GH_TOKEN: ${{ secrets.WINGET_FORK_TOKEN }} | ||
| shell: pwsh | ||
| run: | | ||
| $ErrorActionPreference = 'Stop' | ||
| $manifestDir = Join-Path $env:RUNNER_TEMP 'winget-manifests' | ||
| $forkOwner = $env:WINGET_FORK_OWNER | ||
| if (-not $forkOwner) { throw 'Set the WINGET_FORK_OWNER repository variable.' } | ||
| $repo = 'microsoft/winget-pkgs' | ||
| $forkRepo = "$forkOwner/winget-pkgs" | ||
|
|
||
| function Set-Output { | ||
| param([string]$Name, [string]$Value) | ||
| [IO.File]::AppendAllText($env:GITHUB_OUTPUT, "$Name=$Value`n", [Text.UTF8Encoding]::new($false)) | ||
| } | ||
|
|
||
| function Invoke-GhJson { | ||
| param([string[]]$Arguments) | ||
| $output = & gh @Arguments | ||
| if ($LASTEXITCODE -ne 0) { | ||
| throw "gh api failed: $($Arguments -join ' ')" | ||
| } | ||
| if (-not $output) { return $null } | ||
| return (($output -join "`n") | ConvertFrom-Json) | ||
| } | ||
|
|
||
| $versionFile = Join-Path $manifestDir 'ConSol.SNClient.yaml' | ||
| if (-not (Test-Path -LiteralPath $versionFile)) { throw 'ConSol.SNClient.yaml is missing.' } | ||
| $versionMatch = Select-String -Path $versionFile -Pattern '^PackageVersion:\s*(.+)$' | ||
| if (-not $versionMatch) { throw 'PackageVersion is missing.' } | ||
| $packageVersion = $versionMatch.Matches[0].Groups[1].Value.Trim() | ||
| $identifierMatch = Select-String -Path $versionFile -Pattern '^PackageIdentifier:\s*(.+)$' | ||
| if (-not $identifierMatch) { throw 'PackageIdentifier is missing.' } | ||
| $packageIdentifier = $identifierMatch.Matches[0].Groups[1].Value.Trim() | ||
| if ($packageIdentifier -ne 'ConSol.SNClient') { throw "Unexpected package identifier: $packageIdentifier" } | ||
|
|
||
| & gh api "repos/$repo/contents/manifests/c/ConSol/SNClient/$packageVersion" 2>$null | Out-Null | ||
| if ($LASTEXITCODE -eq 0) { | ||
| Set-Output 'skip' 'true' | ||
| Write-Output "Version $packageVersion already exists in winget-pkgs." | ||
| exit 0 | ||
| } | ||
|
|
||
| & gh api "repos/$repo/contents/manifests/c/ConSol/SNClient" 2>$null | Out-Null | ||
| $isNewPackage = $LASTEXITCODE -ne 0 | ||
| $title = if ($isNewPackage) { "New package: $packageIdentifier $packageVersion" } else { "Update $packageIdentifier to $packageVersion" } | ||
| $message = if ($isNewPackage) { "New package: $packageIdentifier $packageVersion" } else { "Update $packageIdentifier to $packageVersion" } | ||
|
|
||
| # The fork master can be behind upstream. | ||
| # The PR adds only the new version directory, so the fork does not need to be current. | ||
| $forkRef = Invoke-GhJson -Arguments @('api', "repos/$forkRepo/git/ref/heads/master") | ||
| $parentSha = $forkRef.object.sha | ||
| $forkCommit = Invoke-GhJson -Arguments @('api', "repos/$forkRepo/git/commits/$parentSha") | ||
| $baseTree = $forkCommit.tree.sha | ||
|
|
||
| $tempDir = Join-Path $env:RUNNER_TEMP 'winget-pr' | ||
| New-Item -ItemType Directory -Force -Path $tempDir | Out-Null | ||
| $utf8 = New-Object System.Text.UTF8Encoding($false) | ||
| $entries = @() | ||
| Get-ChildItem -Path $manifestDir -Filter '*.yaml' | ForEach-Object { | ||
| $content = [Convert]::ToBase64String([IO.File]::ReadAllBytes($_.FullName)) | ||
| $blobPayload = @{ content = $content; encoding = 'base64' } | ConvertTo-Json -Compress | ||
| $blobFile = Join-Path $tempDir ("blob-" + $_.Name + ".json") | ||
| [IO.File]::WriteAllText($blobFile, $blobPayload, $utf8) | ||
| $blob = Invoke-GhJson -Arguments @('api', '--method', 'POST', "repos/$forkRepo/git/blobs", '--input', $blobFile) | ||
| $entries += @{ | ||
| path = "manifests/c/ConSol/SNClient/$packageVersion/$($_.Name)" | ||
| mode = '100644' | ||
| type = 'blob' | ||
| sha = $blob.sha | ||
| } | ||
| } | ||
|
|
||
| $treePayload = @{ base_tree = $baseTree; tree = $entries } | ConvertTo-Json -Depth 6 | ||
| $treeFile = Join-Path $tempDir 'tree.json' | ||
| [IO.File]::WriteAllText($treeFile, $treePayload, $utf8) | ||
| $tree = Invoke-GhJson -Arguments @('api', '--method', 'POST', "repos/$forkRepo/git/trees", '--input', $treeFile) | ||
|
|
||
| $commitPayload = @{ | ||
| message = $message | ||
| tree = $tree.sha | ||
| parents = @($parentSha) | ||
| } | ConvertTo-Json -Depth 4 | ||
| $commitFile = Join-Path $tempDir 'commit.json' | ||
| [IO.File]::WriteAllText($commitFile, $commitPayload, $utf8) | ||
| $commit = Invoke-GhJson -Arguments @('api', '--method', 'POST', "repos/$forkRepo/git/commits", '--input', $commitFile) | ||
|
|
||
| $branch = "consol-snclient-$packageVersion-$env:GITHUB_RUN_ID" | ||
| $refPayload = @{ ref = "refs/heads/$branch"; sha = $commit.sha } | ConvertTo-Json | ||
| $refFile = Join-Path $tempDir 'ref.json' | ||
| [IO.File]::WriteAllText($refFile, $refPayload, $utf8) | ||
| Invoke-GhJson -Arguments @('api', '--method', 'POST', "repos/$forkRepo/git/refs", '--input', $refFile) | Out-Null | ||
|
|
||
| $body = @" | ||
| $title | ||
|
|
||
| SNClient is a general-purpose monitoring agent. | ||
| The installer is a per-machine WiX MSI. | ||
| It installs the snclient Windows service and a firewall rule. | ||
| ElevationRequirement is elevatesSelf because elevationRequired triggers microsoft/winget-cli#2231. | ||
| InstallationMetadata runs snclient.exe --version for executable discovery. | ||
| "@ | ||
| $bodyPath = Join-Path $env:RUNNER_TEMP 'winget-pr-body.md' | ||
| [IO.File]::WriteAllText($bodyPath, $body, $utf8) | ||
|
|
||
| Set-Output 'skip' 'false' | ||
| Set-Output 'package_version' $packageVersion | ||
| Set-Output 'branch' $branch | ||
| Set-Output 'title' $title | ||
| Write-Output "Created fork branch $branch" | ||
|
|
||
| - name: "Open pull request" | ||
| if: ${{ (github.event_name != 'workflow_dispatch' || inputs.dry_run != true) && steps.create_branch.outputs.skip != 'true' }} | ||
| env: | ||
| GH_TOKEN: ${{ secrets.WINGET_PR_TOKEN }} | ||
| BRANCH: ${{ steps.create_branch.outputs.branch }} | ||
| TITLE: ${{ steps.create_branch.outputs.title }} | ||
| shell: pwsh | ||
| run: | | ||
| $ErrorActionPreference = 'Stop' | ||
| $bodyPath = Join-Path $env:RUNNER_TEMP 'winget-pr-body.md' | ||
| if (-not (Test-Path -LiteralPath $bodyPath)) { throw 'The PR body file is missing.' } | ||
| $body = Get-Content -LiteralPath $bodyPath -Raw | ||
| $forkOwner = $env:WINGET_FORK_OWNER | ||
| if (-not $forkOwner) { throw 'Set the WINGET_FORK_OWNER repository variable.' } | ||
| $prPayload = @{ | ||
| title = $env:TITLE | ||
| head = "${forkOwner}:$($env:BRANCH)" | ||
| base = 'master' | ||
| body = $body | ||
| } | ConvertTo-Json -Depth 4 | ||
| $prFile = Join-Path $env:RUNNER_TEMP 'winget-pr.json' | ||
| [IO.File]::WriteAllText($prFile, $prPayload, [Text.UTF8Encoding]::new($false)) | ||
| $prOutput = & gh api --method POST "repos/microsoft/winget-pkgs/pulls" --input $prFile | ||
| if ($LASTEXITCODE -ne 0) { throw 'Could not create the pull request.' } | ||
| $pr = ($prOutput -join "`n") | ConvertFrom-Json | ||
| Write-Output "Created PR $($pr.html_url)" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| #!/bin/sh | ||
|
|
||
| # Print the WinGet package version. | ||
| # This version comes from the MSI ProductVersion and ARP DisplayVersion. | ||
| # The WinGet PackageVersion must match the installed MSI version. | ||
|
|
||
| set -eu | ||
|
|
||
| SCRIPT_DIR=$(CDPATH='' cd "$(dirname "$0")" && pwd) | ||
| ROOT=$(dirname "$SCRIPT_DIR") | ||
| VERSION=$(cd "$ROOT" && ./buildtools/get_version) | ||
| PARTS=$(printf '%s' "$VERSION" | awk -F. '{ print NF }') | ||
|
|
||
| if [ "$PARTS" -eq 2 ]; then | ||
| VERSION="${VERSION}.0" | ||
| elif [ "$PARTS" -ne 3 ]; then | ||
| echo "unsupported version: $VERSION" >&2 | ||
| exit 1 | ||
| fi | ||
|
|
||
| echo "$VERSION" |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the most complicated part of the PR, and the hardest script section starts here.
uses the github HTTP API to create a new branch, upload files as blobs? , generates a commit JSON, uploads it etc.
Did not look into the references for every API call here.