|
| 1 | +name: Publish Samples |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + paths: |
| 8 | + - 'samples/**' |
| 9 | + - '.github/workflows/samples.yml' |
| 10 | + pull_request: |
| 11 | + paths: |
| 12 | + - 'samples/**' |
| 13 | + - '.github/workflows/samples.yml' |
| 14 | + release: |
| 15 | + types: |
| 16 | + - published |
| 17 | + workflow_dispatch: |
| 18 | + inputs: |
| 19 | + version: |
| 20 | + description: 'SkiaSharp NuGet version to use (leave empty for latest from NuGet.org)' |
| 21 | + required: false |
| 22 | + default: '' |
| 23 | + sha: |
| 24 | + description: 'Git SHA of the commit to build samples from (leave empty to use the default branch HEAD)' |
| 25 | + required: false |
| 26 | + default: '' |
| 27 | + dry_run: |
| 28 | + description: 'Dry run — generate samples but do not push to the samples branch' |
| 29 | + required: false |
| 30 | + default: 'false' |
| 31 | + type: boolean |
| 32 | + |
| 33 | +permissions: |
| 34 | + contents: write |
| 35 | + |
| 36 | +jobs: |
| 37 | + publish: |
| 38 | + name: Generate and Publish Samples |
| 39 | + runs-on: ubuntu-latest |
| 40 | + steps: |
| 41 | + - name: Checkout |
| 42 | + uses: actions/checkout@v4 |
| 43 | + with: |
| 44 | + ref: ${{ inputs.sha || github.sha }} |
| 45 | + |
| 46 | + - name: Get latest SkiaSharp NuGet version |
| 47 | + id: nuget |
| 48 | + shell: pwsh |
| 49 | + run: | |
| 50 | + $inputVersion = '${{ inputs.version }}' |
| 51 | +
|
| 52 | + if ($inputVersion) { |
| 53 | + $version = $inputVersion |
| 54 | + } else { |
| 55 | + # Fetch all published versions of SkiaSharp from NuGet.org (includes pre-releases) |
| 56 | + try { |
| 57 | + $response = Invoke-RestMethod 'https://api.nuget.org/v3-flatcontainer/skiasharp/index.json' |
| 58 | + } catch { |
| 59 | + throw "Failed to query NuGet.org for SkiaSharp versions: $_" |
| 60 | + } |
| 61 | + if (-not $response.versions -or $response.versions.Count -eq 0) { |
| 62 | + throw "NuGet.org returned no versions for SkiaSharp" |
| 63 | + } |
| 64 | + # Versions are sorted ascending by semver; the last one is the highest version |
| 65 | + $version = $response.versions | Select-Object -Last 1 |
| 66 | + } |
| 67 | +
|
| 68 | + Write-Host "Using SkiaSharp version: $version" |
| 69 | +
|
| 70 | + # Parse base version and optional pre-release suffix |
| 71 | + if ($version -match '^(\d+\.\d+\.\d+)(?:-(.+))?$') { |
| 72 | + $baseVersion = $Matches[1] |
| 73 | + $previewSuffix = if ($Matches[2]) { $Matches[2] } else { '' } |
| 74 | + } else { |
| 75 | + throw "Unexpected version format: $version" |
| 76 | + } |
| 77 | +
|
| 78 | + Write-Host "Base version: $baseVersion" |
| 79 | + Write-Host "Preview suffix: $previewSuffix" |
| 80 | +
|
| 81 | + "version=$version" | Out-File -FilePath $env:GITHUB_OUTPUT -Append |
| 82 | + "base_version=$baseVersion" | Out-File -FilePath $env:GITHUB_OUTPUT -Append |
| 83 | + "preview_suffix=$previewSuffix" | Out-File -FilePath $env:GITHUB_OUTPUT -Append |
| 84 | +
|
| 85 | + - name: Get latest HarfBuzzSharp NuGet version |
| 86 | + id: hb-nuget |
| 87 | + shell: pwsh |
| 88 | + run: | |
| 89 | + # Fetch all published versions of HarfBuzzSharp from NuGet.org |
| 90 | + try { |
| 91 | + $response = Invoke-RestMethod 'https://api.nuget.org/v3-flatcontainer/harfbuzzsharp/index.json' |
| 92 | + } catch { |
| 93 | + throw "Failed to query NuGet.org for HarfBuzzSharp versions: $_" |
| 94 | + } |
| 95 | + if (-not $response.versions -or $response.versions.Count -eq 0) { |
| 96 | + throw "NuGet.org returned no versions for HarfBuzzSharp" |
| 97 | + } |
| 98 | + # Use the latest stable version for HarfBuzzSharp |
| 99 | + $version = $response.versions | Where-Object { $_ -notmatch '-' } | Select-Object -Last 1 |
| 100 | + if (-not $version) { |
| 101 | + throw "No stable HarfBuzzSharp version found on NuGet.org" |
| 102 | + } |
| 103 | + Write-Host "Latest stable HarfBuzzSharp version: $version" |
| 104 | + "version=$version" | Out-File -FilePath $env:GITHUB_OUTPUT -Append |
| 105 | +
|
| 106 | + - name: Update NuGet versions in VERSIONS.txt |
| 107 | + shell: pwsh |
| 108 | + run: | |
| 109 | + $skiaVersion = '${{ steps.nuget.outputs.base_version }}' |
| 110 | + $hbVersion = '${{ steps.hb-nuget.outputs.version }}' |
| 111 | + $versionsFile = 'scripts/VERSIONS.txt' |
| 112 | +
|
| 113 | + $content = Get-Content $versionsFile -Raw |
| 114 | +
|
| 115 | + # Update all SkiaSharp package nuget version entries |
| 116 | + $content = [Regex]::Replace( |
| 117 | + $content, |
| 118 | + '(?m)^(SkiaSharp\S*\s+nuget\s+)[\d.]+', |
| 119 | + "`${1}$skiaVersion") |
| 120 | +
|
| 121 | + # Update all HarfBuzzSharp package nuget version entries |
| 122 | + if ($hbVersion) { |
| 123 | + $content = [Regex]::Replace( |
| 124 | + $content, |
| 125 | + '(?m)^(HarfBuzzSharp\S*\s+nuget\s+)[\d.]+', |
| 126 | + "`${1}$hbVersion") |
| 127 | + } |
| 128 | +
|
| 129 | + Set-Content $versionsFile $content -NoNewline |
| 130 | + Write-Host "Updated $versionsFile with SkiaSharp=$skiaVersion, HarfBuzzSharp=$hbVersion" |
| 131 | +
|
| 132 | + - name: Setup .NET |
| 133 | + uses: actions/setup-dotnet@v4 |
| 134 | + with: |
| 135 | + global-json-file: global.json |
| 136 | + |
| 137 | + - name: Restore tools |
| 138 | + run: dotnet tool restore |
| 139 | + |
| 140 | + - name: Generate samples |
| 141 | + env: |
| 142 | + PREVIEW_LABEL: ${{ steps.nuget.outputs.preview_suffix }} |
| 143 | + BUILD_NUMBER: '' |
| 144 | + run: dotnet cake --target=samples-generate |
| 145 | + |
| 146 | + - name: Publish samples to branch |
| 147 | + shell: bash |
| 148 | + env: |
| 149 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 150 | + run: | |
| 151 | + VERSION="${{ steps.nuget.outputs.version }}" |
| 152 | + PREVIEW_SUFFIX="${{ steps.nuget.outputs.preview_suffix }}" |
| 153 | + BRANCH_NAME="docs-samples" |
| 154 | + REMOTE_URL="https://x-access-token:${GITHUB_TOKEN}@github.com/${{ github.repository }}.git" |
| 155 | +
|
| 156 | + # Dry run: triggered by a pull_request event, or the dry_run input is set to true |
| 157 | + DRY_RUN="false" |
| 158 | + if [ "${{ github.event_name }}" = "pull_request" ] || [ "${{ inputs.dry_run }}" = "true" ]; then |
| 159 | + DRY_RUN="true" |
| 160 | + fi |
| 161 | +
|
| 162 | + # Choose the appropriate output directory based on whether it is a preview release |
| 163 | + if [ -n "$PREVIEW_SUFFIX" ]; then |
| 164 | + SAMPLES_DIR="output/samples-preview" |
| 165 | + else |
| 166 | + SAMPLES_DIR="output/samples" |
| 167 | + fi |
| 168 | +
|
| 169 | + echo "Publishing $SAMPLES_DIR to branch: $BRANCH_NAME (dry_run=$DRY_RUN)" |
| 170 | +
|
| 171 | + # Work in a temp directory so we can clone the existing branch (if any) and add a new commit |
| 172 | + WORK_DIR=$(mktemp -d) |
| 173 | + trap 'rm -rf "$WORK_DIR"' EXIT |
| 174 | +
|
| 175 | + if git ls-remote --exit-code "$REMOTE_URL" "refs/heads/${BRANCH_NAME}" > /dev/null; then |
| 176 | + # Branch already exists — clone it so we preserve the history |
| 177 | + git clone --depth=1 --branch "${BRANCH_NAME}" "$REMOTE_URL" "$WORK_DIR" |
| 178 | + else |
| 179 | + # Branch does not exist yet — initialise an empty repo |
| 180 | + git init "$WORK_DIR" |
| 181 | + git -C "$WORK_DIR" checkout -b "${BRANCH_NAME}" |
| 182 | + fi |
| 183 | +
|
| 184 | + # Replace contents with the freshly generated samples (delete files no longer present) |
| 185 | + rsync -a --delete --exclude='.git' "${SAMPLES_DIR}/" "$WORK_DIR/" |
| 186 | +
|
| 187 | + git -C "$WORK_DIR" config user.name "github-actions[bot]" |
| 188 | + git -C "$WORK_DIR" config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 189 | + git -C "$WORK_DIR" add . |
| 190 | + # --allow-empty handles the case where the generated output is identical to the previous commit |
| 191 | + git -C "$WORK_DIR" commit --allow-empty -m "Samples for SkiaSharp $VERSION" |
| 192 | +
|
| 193 | + if [ "$DRY_RUN" = "true" ]; then |
| 194 | + echo "Dry run — skipping push. Would publish $SAMPLES_DIR to branch: $BRANCH_NAME" |
| 195 | + git -C "$WORK_DIR" show --stat HEAD |
| 196 | + else |
| 197 | + git -C "$WORK_DIR" push "$REMOTE_URL" "HEAD:refs/heads/${BRANCH_NAME}" |
| 198 | + echo "Successfully published samples to branch: $BRANCH_NAME" |
| 199 | + fi |
0 commit comments