diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 668693bb..7cb1278b 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,5 +1,6 @@ -# If this file is renamed, the incrementing run attempt number will be reset. If the workflow's -# `name` changes, the `workflow_run` trigger in publish.yml must be updated. +# Builds, tests and packages (on Windows and Linux) the project on merges or PRs to 'dev' or 'main'. +# Saves artifacts (from Windows job) on merges to main, or when the workflow is manually triggered with 'Publish a GitHub release and packages'. +# The Linux job publishes a docker image on every build. name: CI @@ -11,7 +12,7 @@ on: workflow_dispatch: inputs: publish: - description: 'Publish a GitHub release' + description: 'Publish a GitHub release and packages' type: boolean required: true default: true @@ -26,9 +27,11 @@ jobs: name: Build (Windows) runs-on: windows-latest + # A manual run with the `publish` input unchecked does nothing. + if: github.event_name != 'workflow_dispatch' || inputs.publish + permissions: - # Required for creating GitHub releases. - contents: write + contents: read steps: - uses: actions/checkout@v6 @@ -36,16 +39,15 @@ jobs: uses: actions/setup-dotnet@v5 with: dotnet-version: 10.0.x - - name: Build and Publish + - name: Build and Package env: DOTNET_CLI_TELEMETRY_OPTOUT: true - GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} shell: pwsh run: | ./build/Build.Windows.ps1 - name: Upload packages - # Consumed by the publish-nuget job in publish.yml - if: github.event_name != 'pull_request' + # Consumed by the publish-nuget and publish-github jobs in publish.yml + if: env.CI_PUBLISH == 'true' uses: actions/upload-artifact@v7 with: name: packages @@ -53,8 +55,8 @@ jobs: if-no-files-found: error retention-days: 1 - name: Upload archives - # Consumed by the publish-npm job in publish.yml - if: github.event_name != 'pull_request' + # Consumed by the publish-npm and publish-github jobs in publish.yml + if: env.CI_PUBLISH == 'true' uses: actions/upload-artifact@v7 with: name: archives @@ -63,13 +65,25 @@ jobs: artifacts/seqcli-*-*.tar.gz if-no-files-found: error retention-days: 1 + - name: Upload docs + # Consumed by the publish-github job in publish.yml + if: env.CI_PUBLISH == 'true' + uses: actions/upload-artifact@v7 + with: + name: docs + path: artifacts/seqcli-*.md + if-no-files-found: error + retention-days: 1 build-linux: name: Build (Linux) runs-on: ubuntu-24.04 + # A manual run with the `publish` input unchecked does nothing. + if: github.event_name != 'workflow_dispatch' || inputs.publish + permissions: - contents: write + contents: read steps: - uses: actions/checkout@v6 diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 1e9b6548..7adc69b4 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,4 +1,5 @@ -# Publishes the packages built by a successful CI run to NuGet.org and npm +# Publishes the packages built by a successful CI run: creates a draft GitHub release, pushes the +# packages to NuGet.org and npm, then publishes the release once both registries have succeeded. name: Publish @@ -16,14 +17,54 @@ env: CI_TARGET_BRANCH: ${{ github.event.workflow_run.head_branch }} jobs: - publish-nuget: - name: Publish (NuGet) + draft-release: + name: Draft GitHub release runs-on: ubuntu-24.04 if: >- github.event.workflow_run.conclusion == 'success' && - (github.event.workflow_run.event == 'push' || github.event.workflow_run.event == 'workflow_dispatch') && - github.event.workflow_run.head_repository.full_name == github.repository + github.event.workflow_run.head_repository.full_name == github.repository && + (github.event.workflow_run.event == 'workflow_dispatch' || + (github.event.workflow_run.event == 'push' && github.event.workflow_run.head_branch == 'main')) + + permissions: + # Required to download artifacts from the triggering CI run. + actions: read + # Required to create the release. + contents: write + + outputs: + tag: ${{ steps.draft.outputs.tag }} + + steps: + # Build.Common.ps1 and baseversion, at the commit CI built, to reproduce its version. + - uses: actions/checkout@v6 + with: + ref: ${{ github.event.workflow_run.head_sha }} + persist-credentials: false + - name: Download release assets + # Every artifact of the CI run: `packages`, `archives` and `docs`. + uses: actions/download-artifact@v8 + with: + path: release-assets + merge-multiple: true + run-id: ${{ github.event.workflow_run.id }} + github-token: ${{ github.token }} + - name: Create draft release + # Writes the release tag to the step's `tag` output. + id: draft + env: + GH_TOKEN: ${{ github.token }} + HEAD_SHA: ${{ github.event.workflow_run.head_sha }} + shell: pwsh + run: | + ./build/Build.Release.ps1 -AssetDir ./release-assets -Target $env:HEAD_SHA + + publish-nuget: + name: Publish (NuGet) + runs-on: ubuntu-24.04 + + needs: draft-release permissions: # Required to download artifacts from the triggering CI run. @@ -57,7 +98,7 @@ jobs: $packages = @(Get-ChildItem ./packages/*.nupkg) if ($packages.Count -eq 0) { throw "No packages found in ./packages" } foreach ($nupkg in $packages) { - dotnet nuget push "$nupkg" --api-key "$env:NUGET_API_KEY" --source https://api.nuget.org/v3/index.json + dotnet nuget push "$nupkg" --api-key "$env:NUGET_API_KEY" --source https://api.nuget.org/v3/index.json --skip-duplicate if ($LASTEXITCODE -ne 0) { throw "Publishing failed" } } @@ -65,10 +106,7 @@ jobs: name: Publish (npm) runs-on: ubuntu-24.04 - if: >- - github.event.workflow_run.conclusion == 'success' && - (github.event.workflow_run.event == 'push' || github.event.workflow_run.event == 'workflow_dispatch') && - github.event.workflow_run.head_repository.full_name == github.repository + needs: draft-release permissions: # Required to download artifacts from the triggering CI run. @@ -78,8 +116,8 @@ jobs: id-token: write steps: - # Build.Npm.ps1 and the package templates under ./npm, at the commit CI built. The checks - # above guarantee this is a commit pushed to this repository, not pull request code. + # Build.Npm.ps1 and the package templates under ./npm, at the commit CI built. The checks on + # the draft-release job guarantee this is a commit pushed to this repository, not pull request code. - uses: actions/checkout@v6 with: ref: ${{ github.event.workflow_run.head_sha }} @@ -104,3 +142,22 @@ jobs: shell: pwsh run: | ./build/Build.Npm.ps1 -ArchiveDir ./npm-archives + + publish-github: + name: Publish GitHub release + runs-on: ubuntu-24.04 + + # Only publish the release once the packages are on NuGet.org and npm. + needs: [ draft-release, publish-nuget, publish-npm ] + + permissions: + # Required to publish the release and create its tag. + contents: write + + steps: + - name: Publish release + env: + GH_TOKEN: ${{ github.token }} + TAG: ${{ needs.draft-release.outputs.tag }} + run: | + gh release edit "$TAG" --draft=false --repo "$GITHUB_REPOSITORY" diff --git a/build/Build.Release.ps1 b/build/Build.Release.ps1 new file mode 100644 index 00000000..96a5dc14 --- /dev/null +++ b/build/Build.Release.ps1 @@ -0,0 +1,59 @@ +# Creates (or refreshes) the draft GitHub release for a seqcli build, with every file in -AssetDir +# attached. The tag isn't created until the release is published, at the commit given by -Target. +# +# In CI (see draft-release in .github/workflows/publish.yml) the assets are the artifacts of the CI +# run, and the version is reproduced by Get-SemVer from CI_BUILD_NUMBER_BASE and CI_TARGET_BRANCH. +# Builds from branches other than `main` are marked as prereleases. If a draft for the tag already +# exists (left by an earlier attempt of the same run), its assets are replaced instead. +# +# The release tag is written to $env:GITHUB_OUTPUT as `tag` when that variable is set. +# +# Usage: +# ./build/Build.Release.ps1 -AssetDir ./release-assets -Target +param( + # Directory containing the files to attach to the release. + [Parameter(Mandatory)] + [string] $AssetDir, + + # Commit the release's tag is created at when the release is published. + [Parameter(Mandatory)] + [string] $Target, + + # GitHub repository to create the release in. + [string] $Repo = $env:GITHUB_REPOSITORY +) + +$ErrorActionPreference = 'Stop' + +if (-not $Repo) { throw "No repository given; pass -Repo or set GITHUB_REPOSITORY" } + +$assets = @(Get-ChildItem $AssetDir -File | ForEach-Object { $_.FullName }) +if ($assets.Count -eq 0) { throw "No release assets found in $AssetDir" } + +Push-Location $PSScriptRoot/../ +try { + . ./build/Build.Common.ps1 + $tag = "v$(Get-SemVer)" +} finally { + Pop-Location +} + +if ($env:GITHUB_OUTPUT) { + "tag=$tag" >> $env:GITHUB_OUTPUT +} + +$isDraft = gh release view $tag --repo $Repo --json isDraft --jq .isDraft 2>$null +if ($LASTEXITCODE -eq 0) { + # A draft left by an earlier attempt of this run; refresh its assets. + if ($isDraft -ne 'true') { throw "Release $tag has already been published" } + Write-Host "Updating existing draft release $tag" + & gh release upload $tag --repo $Repo --clobber @assets + if ($LASTEXITCODE -ne 0) { throw "Uploading release assets failed" } +} else { + Write-Host "Creating draft release $tag" + $arguments = @('release', 'create', $tag, '--draft', '--title', $tag, '--target', $Target, '--generate-notes', '--repo', $Repo) + # Get-SemVer adds a prerelease suffix for every branch except `main`. + if ($tag.Contains('-')) { $arguments += '--prerelease' } + & gh @arguments @assets + if ($LASTEXITCODE -ne 0) { throw "Creating the draft release failed" } +} diff --git a/build/Build.Windows.ps1 b/build/Build.Windows.ps1 index 8c22b65c..6b2f33fe 100644 --- a/build/Build.Windows.ps1 +++ b/build/Build.Windows.ps1 @@ -48,7 +48,7 @@ function Create-ArtifactDir mkdir ./artifacts } -function Publish-Archives($version) +function Create-Archives($version) { $rids = $([xml](Get-Content .\src\SeqCli\SeqCli.csproj)).Project.PropertyGroup.RuntimeIdentifiers.Split(';') foreach ($rid in $rids) { @@ -78,7 +78,7 @@ function Publish-Archives($version) } } -function Publish-DotNetTool($version) +function Package-DotNetTool($version) { Write-Output "Building dotnet tool" @@ -95,18 +95,6 @@ function Publish-Docs($version) if($LASTEXITCODE -ne 0) { throw "Build failed" } } -function Upload-GitHubRelease($version) -{ - Write-Output "Creating release for version $version" - - $prerelease = ""; - if ($env:CI_TARGET_BRANCH -ne "main") { - $prerelease = "--prerelease "; - } - - iex "gh release create v$version --title v$version $prerelease--generate-notes $(get-item ./artifacts/*)" -} - function Remove-GlobalJson { if(Test-Path ./global.json) { rm ./global.json } @@ -127,15 +115,11 @@ Clean-Output Create-ArtifactDir Create-GlobalJson Restore-Packages -Publish-Archives($version) -Publish-DotNetTool($version) +Create-Archives($version) +Package-DotNetTool($version) Execute-Tests($version) Publish-Docs($version) -if ($env:CI_PUBLISH -eq "True") { - Upload-GitHubRelease($version) -} - Remove-GlobalJson Pop-Location diff --git a/build/README.md b/build/README.md index b6edd53a..bd2c4025 100644 --- a/build/README.md +++ b/build/README.md @@ -5,9 +5,10 @@ This directory holds the PowerShell scripts that build, test, package, and publi | Script | Runs on | Produces | |---|---|---| | `Build.Common.ps1` | (dot-sourced by the others) | Version number helpers | -| `Build.Windows.ps1` | Windows | Release archives for every platform, the `seqcli` .NET tool package, generated docs, a GitHub release | +| `Build.Windows.ps1` | Windows | Release archives for every platform, the `seqcli` .NET tool package, generated docs | | `Build.Linux.ps1` | Linux | `datalust/seqcli` Docker images for `linux/amd64` and `linux/arm64` | | `Build.Npm.ps1` | Linux (any OS locally) | `@datalust/seqcli` and `@datalust/seqcli-` npm packages | +| `Build.Release.ps1` | Linux (any OS locally) | Draft GitHub release `v` with the CI artifacts attached | `7-zip/` contains a vendored copy of `7za.exe`, used by `Build.Windows.ps1` to produce `.zip` and `.tar.gz` archives with consistent contents on Windows. @@ -20,7 +21,7 @@ Every artifact from one CI run shares a single version, computed by `Get-SemVer` ``` * `` is read from [`baseversion`](../baseversion) in the repository root (e.g. `2026.1`). Bump it there when starting a new release line. -* `` is `CI_BUILD_NUMBER_BASE + 2300`, zero-padded to five digits (e.g. `02616`). `CI_BUILD_NUMBER_BASE` is the GitHub Actions `run_number`; the fixed offset keeps build numbers increasing across the move from the previous CI system. Note the comment at the top of `ci.yml`: renaming the workflow file resets `run_number`, which would produce lower version numbers than already-published releases. Locally, where the variable is unset, the build number is the literal string `local`. +* `` is `CI_BUILD_NUMBER_BASE + 2300`, zero-padded to five digits (e.g. `02616`). `CI_BUILD_NUMBER_BASE` is the GitHub Actions `run_number`; the fixed offset keeps build numbers increasing across the move from the previous CI system. Renaming the workflow file resets `run_number`, which would produce lower version numbers than already-published releases. Locally, where the variable is unset, the build number is the literal string `local`. * The prerelease suffix is added for every branch except `main`. It is the first ten characters of the branch name, stripped of anything other than letters, digits and hyphens, followed by the build number. A `dev` build is therefore `2026.1.02700-dev-02700`; a `main` build is `2026.1.02616`. `CI_TARGET_BRANCH` overrides the branch detected from git. In CI it is set from `github.head_ref` (for pull requests) or `github.ref_name`. @@ -33,7 +34,7 @@ npm enforces strict semver, which forbids leading zeros in numeric components, s The workflow runs on pushes and pull requests to `dev` and `main`, and can be triggered manually with `workflow_dispatch`. Two jobs run: -1. **Build (Windows)** runs `Build.Windows.ps1`. On non-PR builds, it then uploads the tool package as a workflow artifact named `packages`, and the release archives as one named `archives`, for the publish workflow to consume. +1. **Build (Windows)** runs `Build.Windows.ps1`. When `CI_PUBLISH` is set (pushes to `main`, and manual runs with the `publish` input checked), it then uploads the tool package as a workflow artifact named `packages`, the release archives as one named `archives`, and the markdown command reference as one named `docs`, for the publish workflow to consume. 2. **Build (Linux)** runs `Build.Linux.ps1`, after configuring `binfmt` so that ARM64 images can be built on the x64 runner. It runs in parallel with the Windows job. Environment variables control what gets published: @@ -42,8 +43,7 @@ Environment variables control what gets published: |---|---|---| | `CI_BUILD_NUMBER_BASE` | `github.run_number` | Build number component of the version | | `CI_TARGET_BRANCH` | `github.head_ref` or `github.ref_name` | Branch component of the version | -| `CI_PUBLISH` | `True` for pushes to `main`, or when the manual `publish` input is set | Whether `Build.Windows.ps1` creates a GitHub release | -| `GH_TOKEN` | `secrets.GITHUB_TOKEN` | Used by `gh release create` | +| `CI_PUBLISH` | `True` for pushes to `main`, or when the manual `publish` input is set | Whether CI uploads the artifacts the publish workflow publishes to NuGet, npm and a GitHub release | | `DOCKER_USER`, `DOCKER_TOKEN` | Docker Hub secrets | When `DOCKER_TOKEN` is non-empty, `Build.Linux.ps1` pushes images | | `SEQ_DOCKER_TAG` | e.g. `2026.1` | Seq image tag used for the Linux end-to-end tests | @@ -51,10 +51,14 @@ GitHub supplies no secrets or OIDC tokens to pull requests from forks, and PR bu ## The publish workflow -`publish.yml` runs via `workflow_run` when the CI workflow completes. It only publishes when that CI run succeeded, and was started by a push or manual dispatch on `dev` or `main` in this repository; it does nothing for pull requests. Two jobs run, independently: +`publish.yml` runs via `workflow_run` when the CI workflow completes. It only publishes when that CI run succeeded in this repository, and was either a push to `main` or a manual dispatch on `dev` or `main`; it does nothing for pull requests or pushes to `dev`. Four jobs run: -1. **Publish (NuGet)** downloads the `packages` artifact from the CI run and pushes it to NuGet.org. -2. **Publish (npm)** checks out the commit CI built, downloads the `archives` artifact, and runs `Build.Npm.ps1`. +1. **Draft GitHub release** downloads all three artifacts and runs `Build.Release.ps1`, which creates a draft release `v` targeting the commit CI built, with them attached; builds from branches other than `main` are marked as prereleases. Drafts are visible only to people with write access, and the tag isn't created until the release is published. +2. **Publish (NuGet)** downloads the `packages` artifact from the CI run and pushes it to NuGet.org. +3. **Publish (npm)** checks out the commit CI built, downloads the `archives` artifact, and runs `Build.Npm.ps1`. +4. **Publish GitHub release** runs only after both registries succeed, and publishes the draft, which creates the tag. + +The NuGet and npm jobs run in parallel, after the draft is created. If either fails, the draft is left in place; re-running the failed jobs publishes it once they succeed. Re-running the draft job updates the existing draft's assets rather than creating another. Both registries use [trusted publishing](https://learn.microsoft.com/nuget/nuget-org/trusted-publishing). @@ -63,12 +67,14 @@ Both registries use [trusted publishing](https://learn.microsoft.com/nuget/nuget | Trigger | GitHub release | NuGet | Docker Hub | npm | |---|---|---|---|---| | Pull request | no | no | no | no | -| Push to `dev` | no (unless manual `publish`) | prerelease version | `datalust/seqcli-ci:` | prerelease, dist-tag `dev` | +| Push to `dev` | no | no | `datalust/seqcli-ci:` | no | | Push to `main` | yes, `v` | release version | `datalust/seqcli-ci:` | release, dist-tag `latest` | +| Manual run, `publish` checked | yes (prerelease unless on `main`) | prerelease version unless on `main` | `datalust/seqcli-ci:` | prerelease (dist-tag `dev`) unless on `main` | +| Manual run, `publish` unchecked | no (CI skips its jobs) | no | no | no | Note that `Build.Linux.ps1` always pushes to the `datalust/seqcli-ci` repository (the image name with a `-ci` suffix), never directly to `datalust/seqcli`. Promoting an image to the public `datalust/seqcli` repository, and tagging it `latest`, is a separate step outside this repository. -To publish a release from `dev` (for example a preview build), run the workflow manually from the Actions tab with the **Publish a GitHub release** input checked. The release is marked as a prerelease because the branch is not `main`. +To publish a release from `dev` (for example a preview build), run the workflow manually from the Actions tab with the **Publish a GitHub release and packages** input checked; this also publishes to NuGet and npm. The release is marked as a prerelease because the branch is not `main`. ## Running the builds locally