diff --git a/.github/actions/gomodfs-mount/action.yml b/.github/actions/gomodfs-mount/action.yml new file mode 100644 index 000000000..23964e122 --- /dev/null +++ b/.github/actions/gomodfs-mount/action.yml @@ -0,0 +1,58 @@ +name: gomodfs-mount +description: Mount the Go module cache and Go toolchain from a gomodfs remote store (Windows only) + +inputs: + store-url: + description: URL of the gomodfs store API server. If empty, a built-in default is used. + drive: + description: Drive letter to mount gomodfs on + default: "M:" + +outputs: + success: + description: Whether gomodfs is mounted and GOMODCACHE points at it + value: ${{ steps.use.outputs.success == 'true' }} + log: + description: Path to gomodfs's log file + value: ${{ steps.start.outputs.log }} + +runs: + using: composite + steps: + # The version must match the server's gomodfs version, so the remote + # store protocol matches. Keep it in sync with GOMODFS_VERSION below. + - name: Restore gomodfs.exe + uses: actions/cache@668228422ae6a00e4ad889ee87cd7109ec5666a7 # zizmor: ignore[cache-poisoning] v5.0.4 + with: + path: ${{ runner.temp }}/gomodfs-bin + key: gomodfs-exe-windows-amd64-954c541eeafc + + - name: Start gomodfs + id: start + shell: pwsh + env: + GOMODFS_VERSION: 954c541eeafc + # An empty input (e.g. an unset repo variable) overrides an input + # default, so the default URL is here instead. + STORE_URL: ${{ inputs.store-url || 'http://ci-gocached-azure-1:8090' }} + DRIVE: ${{ inputs.drive }} + BIN_DIR: ${{ runner.temp }}/gomodfs-bin + run: '& "$env:GITHUB_ACTION_PATH\start.ps1"' + + # This is a separate step so it runs after the runner has cleaned up + # the previous step's processes. Only then is it safe to point + # GOMODCACHE and the toolchain at the mount. + - name: Use gomodfs + id: use + if: steps.start.outputs.started == 'true' + shell: pwsh + env: + DRIVE: ${{ inputs.drive }} + LOG: ${{ steps.start.outputs.log }} + run: '& "$env:GITHUB_ACTION_PATH\use.ps1"' + + - name: Report fallback + if: steps.use.outputs.success != 'true' + shell: pwsh + run: | + "::warning::gomodfs is not in use; Go will download modules and the toolchain directly" diff --git a/.github/actions/gomodfs-mount/check.ps1 b/.github/actions/gomodfs-mount/check.ps1 new file mode 100644 index 000000000..e859c3769 --- /dev/null +++ b/.github/actions/gomodfs-mount/check.ps1 @@ -0,0 +1,21 @@ +# Copyright (c) Tailscale Inc & contributors +# SPDX-License-Identifier: BSD-3-Clause + +# check.ps1 fails if the gomodfs mounted by this action has exited, +# printing the tail of its log. Run it at the end of a job that used the +# action, with $env:LOG set to the action's log output and $env:JOB_STATUS +# set to job.status. + +$p = Get-Process gomodfs -ErrorAction SilentlyContinue +if ($p) { + "gomodfs is running (pid $($p.Id))" + if ($env:JOB_STATUS -ne 'success') { + "job failed; last lines of gomodfs's log:" + Get-Content -LiteralPath $env:LOG -Tail 50 -ErrorAction SilentlyContinue + } + exit 0 +} +"::error::gomodfs.exe is no longer running, so the job's Go module cache and toolchain were gone" +"last lines of gomodfs's log:" +Get-Content -LiteralPath $env:LOG -Tail 50 -ErrorAction SilentlyContinue +exit 1 diff --git a/.github/actions/gomodfs-mount/start.ps1 b/.github/actions/gomodfs-mount/start.ps1 new file mode 100644 index 000000000..9007fd62d --- /dev/null +++ b/.github/actions/gomodfs-mount/start.ps1 @@ -0,0 +1,98 @@ +# Copyright (c) Tailscale Inc & contributors +# SPDX-License-Identifier: BSD-3-Clause + +# start.ps1 installs WinFsp, gets gomodfs.exe, and starts it in the +# background, mounted on $env:DRIVE and backed by the remote store at +# $env:STORE_URL. On success it sets the step outputs started=true and +# log=. It never fails the step; on any error it +# prints a warning so the job falls back to downloading modules directly. + +$ErrorActionPreference = 'Stop' +$ProgressPreference = 'SilentlyContinue' + +$winfspURL = 'https://github.com/winfsp/winfsp/releases/download/v2.1/winfsp-2.1.25156.msi' +$winfspSHA256 = '073a70e00f77423e34bed98b86e600def93393ba5822204fac57a29324db9f7a' + +$log = Join-Path $env:RUNNER_TEMP 'gomodfs.log' +"log=$log" >> $env:GITHUB_OUTPUT + +function Time-Phase([string]$name, [scriptblock]$block) { + $sw = [System.Diagnostics.Stopwatch]::StartNew() + & $block + "${name}: $([math]::Round($sw.Elapsed.TotalSeconds, 1))s" +} + +try { + $url = $env:STORE_URL.TrimEnd('/') + # The URL ends up on a cmd.exe command line below. + if ($url -notmatch '^https?://[A-Za-z0-9.\-:\[\]]+$') { + throw "unexpected gomodfs store URL format: $url" + } + + Time-Phase 'checking gomodfs store' { + $code = & curl.exe -sS -m 10 -o NUL -w '%{http_code}' -H 'Accept-Encoding: lz4' "$url/api/v1/modmap/golang.org/x/sync/@v/v0.10.0" + if ($code -ne '200') { + throw "gomodfs store at $url is not healthy: HTTP status $code" + } + } + + Time-Phase 'installing WinFsp' { + $msi = Join-Path $env:RUNNER_TEMP 'winfsp.msi' + Invoke-WebRequest -Uri $winfspURL -OutFile $msi + $got = (Get-FileHash -Algorithm SHA256 -LiteralPath $msi).Hash.ToLowerInvariant() + if ($got -ne $winfspSHA256) { + throw "WinFsp MSI has SHA-256 $got; want $winfspSHA256" + } + $msiLog = Join-Path $env:RUNNER_TEMP 'winfsp-install.log' + $p = Start-Process -Wait -PassThru -FilePath msiexec.exe -ArgumentList '/i', "`"$msi`"", '/qn', '/norestart', '/l*', "`"$msiLog`"" + if ($p.ExitCode -ne 0) { + Get-Content -LiteralPath $msiLog -Tail 30 -ErrorAction SilentlyContinue + throw "WinFsp install failed with exit code $($p.ExitCode)" + } + } + + $exe = Join-Path $env:BIN_DIR 'gomodfs.exe' + if (Test-Path -LiteralPath $exe) { + "using cached $exe" + } else { + # Use the image's Go, not ./tool/go, which would download the + # toolchain that the mount provides. + Time-Phase 'building gomodfs' { + if (-not (Get-Command go -ErrorAction SilentlyContinue)) { + throw 'no go on PATH to build gomodfs with' + } + $tmp = Join-Path $env:RUNNER_TEMP 'gomodfs-build' + $env:GOMODCACHE = Join-Path $tmp 'mod' + $env:GOCACHE = Join-Path $tmp 'cache' + $env:GOBIN = $env:BIN_DIR + $env:GOFLAGS = '-modcacherw' + $env:GOTOOLCHAIN = 'auto' + $env:CGO_ENABLED = '0' + Remove-Item Env:GOCACHEPROG -ErrorAction SilentlyContinue + & go install "github.com/tailscale/gomodfs/cmd/gomodfs@$env:GOMODFS_VERSION" + if ($LASTEXITCODE -ne 0) { + throw "building gomodfs failed with exit code $LASTEXITCODE" + } + } + } + + Time-Phase 'mounting gomodfs' { + Remove-Item Env:PARENTDEATH_ADDR, Env:PARENTDEATH_SESSION -ErrorAction SilentlyContinue + # Start gomodfs via ShellExecute (no -NoNewWindow or -Redirect* + # flags) so it doesn't inherit this step's stdout and stderr pipes. + # If it did, the runner would kill it when this step ends. + $cmdArgs = "/d /s /c `"`"$exe`" --winfsp --mount=$env:DRIVE --remote-store=$url > `"$log`" 2>&1`"" + $p = Start-Process -PassThru -WindowStyle Hidden -FilePath $env:ComSpec -ArgumentList $cmdArgs + $deadline = (Get-Date).AddSeconds(30) + while (-not (Test-Path "$env:DRIVE\.gomodfs-status")) { + if ($p.HasExited -or (Get-Date) -gt $deadline) { + Get-Content -LiteralPath $log -Tail 20 -ErrorAction SilentlyContinue + throw "mounting gomodfs on $env:DRIVE failed" + } + Start-Sleep -Milliseconds 100 + } + } + "started=true" >> $env:GITHUB_OUTPUT +} catch { + "::warning::gomodfs setup failed: $_" +} diff --git a/.github/actions/gomodfs-mount/use.ps1 b/.github/actions/gomodfs-mount/use.ps1 new file mode 100644 index 000000000..e30cabce5 --- /dev/null +++ b/.github/actions/gomodfs-mount/use.ps1 @@ -0,0 +1,35 @@ +# Copyright (c) Tailscale Inc & contributors +# SPDX-License-Identifier: BSD-3-Clause + +# use.ps1 checks that the gomodfs started by start.ps1 survived into this +# step, then points GOMODCACHE and ~/.cache/tsgo at the mount for later +# steps and sets the step output success=true. It never fails the step. + +$ErrorActionPreference = 'Stop' + +try { + if (-not (Get-Process gomodfs -ErrorAction SilentlyContinue) -or + -not (Test-Path "$env:DRIVE\.gomodfs-status")) { + Get-Content -LiteralPath $env:LOG -Tail 20 -ErrorAction SilentlyContinue + throw 'gomodfs did not survive past the step that started it' + } + + $cache = Join-Path $env:USERPROFILE '.cache' + $tsgo = Join-Path $cache 'tsgo' + New-Item -ItemType Directory -Force $cache | Out-Null + $old = Get-Item -LiteralPath $tsgo -Force -ErrorAction SilentlyContinue + if ($old) { + if ($old.LinkType) { + $old.Delete() + } else { + Remove-Item -LiteralPath $tsgo -Recurse -Force + } + } + New-Item -ItemType SymbolicLink -Path $tsgo -Target "$env:DRIVE\tsgo-windows-amd64" | Out-Null + + "GOMODCACHE=$env:DRIVE\" >> $env:GITHUB_ENV + "success=true" >> $env:GITHUB_OUTPUT + "gomodfs is mounted on $env:DRIVE; GOMODCACHE and $tsgo point at it" +} catch { + "::warning::not using gomodfs: $_" +} diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f69474162..10821a8b4 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -218,7 +218,6 @@ jobs: # https://github.com/organizations/tailscale/settings/actions/github-hosted-runners/5 runs-on: ci-windows-github-1 timeout-minutes: 60 - needs: gomod-cache name: Windows (${{ matrix.name || matrix.shard}}) strategy: fail-fast: false # don't abort the entire matrix if one element fails @@ -236,12 +235,12 @@ jobs: with: path: ${{ github.workspace }}/src - - name: Restore Go module cache - uses: actions/cache/restore@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4 + # This must run before anything uses ./tool/go, as it also provides the Go toolchain. + - name: Mount gomodfs + id: gomodfs + uses: ./src/.github/actions/gomodfs-mount with: - path: gomodcache - key: ${{ needs.gomod-cache.outputs.cache-key }} - enableCrossOsArchive: true + store-url: ${{ vars.GOMODFS_AZURE_URL }} - name: Set up cigocacher id: cigocacher-setup @@ -288,6 +287,14 @@ jobs: run: | Invoke-Expression "$env:GOCACHEPROG --stats" | jq . + - name: Check gomodfs is still running + if: always() && steps.gomodfs.outputs.success == 'true' + shell: pwsh + env: + LOG: ${{ steps.gomodfs.outputs.log }} + JOB_STATUS: ${{ job.status }} + run: ./src/.github/actions/gomodfs-mount/check.ps1 + macos: runs-on: macos-latest timeout-minutes: 30 diff --git a/.github/workflows/windows-nightly.yml b/.github/workflows/windows-nightly.yml index 4defe69a1..8e14f48d0 100644 --- a/.github/workflows/windows-nightly.yml +++ b/.github/workflows/windows-nightly.yml @@ -51,17 +51,12 @@ jobs: path: ${{ github.workspace }}/src persist-credentials: false - - name: Compute Go module cache key - id: hash - shell: bash - run: echo "key=gomod-cross3-${{ hashFiles('src/go.mod', 'src/go.sum') }}" >> "$GITHUB_OUTPUT" - - - name: Restore Go module cache - uses: actions/cache/restore@668228422ae6a00e4ad889ee87cd7109ec5666a7 # v5.0.4 + # This must run before anything uses ./tool/go, as it also provides the Go toolchain. + - name: Mount gomodfs + id: gomodfs + uses: ./src/.github/actions/gomodfs-mount with: - path: gomodcache - key: ${{ steps.hash.outputs.key }} - enableCrossOsArchive: true + store-url: ${{ vars.GOMODFS_AZURE_URL }} - name: Set up cigocacher uses: ./src/.github/actions/go-cache @@ -93,6 +88,14 @@ jobs: if-no-files-found: ignore retention-days: 90 + - name: Check gomodfs is still running + if: always() && steps.gomodfs.outputs.success == 'true' + shell: pwsh + env: + LOG: ${{ steps.gomodfs.outputs.log }} + JOB_STATUS: ${{ job.status }} + run: ./src/.github/actions/gomodfs-mount/check.ps1 + notify: if: always() needs: