.github/workflows/ci-cd.yml

358 lines · 12566 bytes

  1name: CI/CD Pipeline
  2
  3on:
  4  push:
  5    branches:
  6      - main # Trigger on pushes to the main branch
  7  pull_request:
  8    branches:
  9      - main # Trigger on pull requests targeting the main branch
 10  workflow_dispatch:
 11    inputs:
 12      pr_number:
 13        description: "Pull Request Number"
 14        required: false
 15        type: string
 16
 17jobs:
 18  build:
 19    runs-on: ubuntu-latest
 20
 21    steps:
 22      - name: Checkout code
 23        uses: actions/checkout@v4
 24        with:
 25          fetch-depth: 0 # Fetch all history and tags
 26          ref: ${{ github.event.pull_request.head.sha || (github.event.inputs.pr_number && format('refs/pull/{0}/head', github.event.inputs.pr_number)) || github.sha }}
 27
 28      - name: Set up Go
 29        uses: actions/setup-go@v5
 30        with:
 31          go-version: "1.21"
 32
 33      - name: Install Dependencies
 34        run: go mod tidy
 35
 36      - name: Build Linux binary
 37        run: |
 38          ./Build/buildlinux
 39          mkdir -p release/linux
 40          mv curd-linux-x86_64 release/linux/
 41          mv curd-linux-arm64 release/linux/
 42        env:
 43          GITHUB_EVENT_NAME: ${{ github.event_name }}
 44          GITHUB_REF: ${{ github.ref }}
 45          GITHUB_EVENT_HEAD_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
 46          COMPRESS: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && contains(github.event.head_commit.message, 'release:') }}
 47
 48      - name: Build macOS x86_64 binary
 49        run: |
 50          ./Build/buildmac-x86_64
 51
 52      - name: Build macOS ARM64 binary
 53        run: |
 54          ./Build/buildmac-arm64
 55
 56      - name: Install LLVM
 57        run: |
 58          sudo apt-get update && sudo apt-get install -y llvm
 59          # Find the llvm-lipo command
 60          LIPO_CMD=$(find /usr/bin -name "llvm-lipo*" | head -n 1)
 61          echo "Found lipo command: $LIPO_CMD"
 62          echo "LIPO_CMD=$LIPO_CMD" >> $GITHUB_ENV
 63
 64      - name: Create Universal macOS binary
 65        run: |
 66          ${{ env.LIPO_CMD }} -create "curd-macos-x86_64" "curd-macos-arm64" -output "curd-macos-universal"
 67          mkdir -p release/macos
 68          mv curd-macos-x86_64 release/macos
 69          mv curd-macos-arm64 release/macos
 70          mv curd-macos-universal release/macos
 71
 72      - name: Build Windows binary (cross-compile)
 73        run: |
 74          ./Build/buildwindows
 75          mkdir -p release/windows
 76          mv curd-windows-x86_64.exe release/windows/curd-windows-x86_64.exe
 77        env:
 78          GITHUB_EVENT_NAME: ${{ github.event_name }}
 79          GITHUB_REF: ${{ github.ref }}
 80          GITHUB_EVENT_HEAD_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
 81          COMPRESS: ${{ github.event_name == 'push' && github.ref == 'refs/heads/main' && contains(github.event.head_commit.message, 'release:') }}
 82
 83      - name: Upload Artifacts
 84        uses: actions/upload-artifact@v4
 85        with:
 86          name: curd-binaries
 87          path: |
 88            release/linux/curd-linux-x86_64
 89            release/linux/curd-linux-arm64
 90            release/macos/curd-macos-x86_64
 91            release/macos/curd-macos-arm64
 92            release/macos/curd-macos-universal
 93            release/windows/curd-windows-x86_64.exe
 94          retention-days: 1
 95          if-no-files-found: error
 96
 97  release:
 98    runs-on: windows-latest
 99    needs: build
100    permissions:
101      contents: write
102      actions: read
103    if: |
104      github.ref == 'refs/heads/main' && 
105      github.event_name == 'push' && 
106      (contains(github.event.head_commit.message, 'release:'))
107
108    steps:
109      - name: Checkout code
110        uses: actions/checkout@v4
111        with:
112          fetch-depth: 0
113
114      - name: Checkout MPV binaries
115        uses: actions/checkout@v4
116        with:
117          ref: mpv-binaries
118          path: mpv-temp
119          sparse-checkout: |
120            Build/mpv.exe.gz
121            Build/mpv.zip
122
123      - name: Setup MPV binary
124        run: |
125          New-Item -ItemType Directory -Force -Path "Build\mpv"
126          if (Test-Path "mpv-temp/Build/mpv.exe.gz") {
127            Copy-Item "mpv-temp/Build/mpv.exe.gz" "Build\mpv\mpv.exe.gz"
128            7z x "Build\mpv\mpv.exe.gz" -o"Build\mpv"
129            Remove-Item "Build\mpv\mpv.exe.gz"
130          } else {
131            Write-Host "Warning: MPV binary not found in mpv-binaries branch, downloading from URL..."
132            Invoke-WebRequest -Uri "https://raw.githubusercontent.com/${{ github.repository }}/main/Build/mpv.exe.gz" -OutFile "Build\mpv\mpv.exe.gz"
133            7z x "Build\mpv\mpv.exe.gz" -o"Build\mpv"
134            Remove-Item "Build\mpv\mpv.exe.gz"
135          }
136          Remove-Item -Recurse -Force "mpv-temp" -ErrorAction SilentlyContinue
137        shell: pwsh
138
139      - name: Download Artifacts
140        uses: actions/download-artifact@v4
141        with:
142          name: curd-binaries
143          path: Build
144
145      - name: Organize Release Files
146        run: |
147          $version = Get-Content VERSION.txt
148          echo "CURD_VERSION=$version" >> $env:GITHUB_ENV
149          $release_dir = "releases/curd-$version"
150
151          # Create directory structure
152          New-Item -ItemType Directory -Force -Path "$release_dir/windows"
153          New-Item -ItemType Directory -Force -Path "$release_dir/macos"
154          New-Item -ItemType Directory -Force -Path "$release_dir/linux"
155
156          # List contents of Build directory to debug
157          Write-Host "Contents of Build directory:"
158          Get-ChildItem -Path "Build" -Recurse
159
160          # Move files to their respective directories
161          Move-Item "Build/windows/curd-windows-x86_64.exe" "$release_dir/windows/"
162          Move-Item "Build/macos/curd-macos-universal" "$release_dir/macos/"
163          Move-Item "Build/macos/curd-macos-x86_64" "$release_dir/macos/"
164          Move-Item "Build/macos/curd-macos-arm64" "$release_dir/macos/"
165          Move-Item "Build/linux/curd-linux-x86_64" "$release_dir/linux/"
166          Move-Item "Build/linux/curd-linux-arm64" "$release_dir/linux/"
167          Copy-Item "Build/mpv/mpv.exe" "$release_dir/windows/"
168        shell: pwsh
169
170      - name: Update Inno Setup Script with New Version
171        run: |
172          $iss_path = "Build/curd-windows-build.iss"
173          $content = Get-Content $iss_path
174          $content = $content -replace '^AppVersion=.*', "AppVersion=$env:CURD_VERSION"
175          Set-Content $iss_path $content
176        shell: pwsh
177
178      - name: Create Windows Installer
179        run: |
180          & "C:\Program Files (x86)\Inno Setup 6\ISCC.exe" "Build\curd-windows-build.iss"
181        shell: pwsh
182
183      - name: Build direct commit release notes
184        run: |
185          $ErrorActionPreference = 'Stop'
186
187          $repo = "${{ github.repository }}"
188          $token = "${{ secrets.ACTIONS_PAT }}"
189          $headers = @{
190            Authorization = "Bearer $token"
191            Accept = "application/vnd.github+json"
192            "X-GitHub-Api-Version" = "2022-11-28"
193          }
194
195          $currentTag = "v$env:CURD_VERSION"
196          $tags = @(git tag --sort=-version:refname "v*")
197          $previousTag = $null
198          foreach ($tag in $tags) {
199            if ($tag -ne $currentTag) {
200              $previousTag = $tag
201              break
202            }
203          }
204
205          if ($previousTag) {
206            $range = "$previousTag..HEAD"
207          } else {
208            $rootCommit = (git rev-list --max-parents=0 HEAD | Select-Object -First 1)
209            if ([string]::IsNullOrWhiteSpace($rootCommit)) {
210              $range = "HEAD"
211            } else {
212              $range = "$rootCommit..HEAD"
213            }
214          }
215
216          $rawCommits = @(git log --no-merges --pretty=format:'%H%x09%s%x09%an' $range)
217          if ($rawCommits.Count -eq 1 -and [string]::IsNullOrWhiteSpace($rawCommits[0])) {
218            $rawCommits = @()
219          }
220
221          $feats = @()
222          $fixes = @()
223          $chores = @()
224          $others = @()
225          $contributorSet = @{}
226
227          foreach ($entry in $rawCommits) {
228            $parts = $entry -split "`t", 3
229            if ($parts.Count -lt 3) {
230              continue
231            }
232
233            $sha = $parts[0].Trim()
234            $subject = $parts[1].Trim()
235            $authorName = $parts[2].Trim()
236
237            if ([string]::IsNullOrWhiteSpace($sha) -or [string]::IsNullOrWhiteSpace($subject)) {
238              continue
239            }
240
241            if ($subject -match '^\s*release:') {
242              continue
243            }
244
245            $pullsUri = "https://api.github.com/repos/$repo/commits/$sha/pulls"
246            $pulls = @()
247            try {
248              $pulls = @(Invoke-RestMethod -Uri $pullsUri -Headers $headers -Method Get)
249            } catch {
250              $pulls = @()
251            }
252
253            # Skip commits that are already represented by pull requests in generated notes.
254            if ($pulls.Count -gt 0) {
255              continue
256            }
257
258            $commitUri = "https://api.github.com/repos/$repo/commits/$sha"
259            $login = $null
260            try {
261              $commitData = Invoke-RestMethod -Uri $commitUri -Headers $headers -Method Get
262              if ($null -ne $commitData.author -and $null -ne $commitData.author.login) {
263                $login = $commitData.author.login
264              }
265            } catch {
266              $login = $null
267            }
268
269            $shortSha = $sha.Substring(0, 7)
270            if (-not [string]::IsNullOrWhiteSpace($login)) {
271              $authorDisplay = "[@$login](https://github.com/$login)"
272            } else {
273              $authorDisplay = $authorName
274            }
275
276            $contributorSet[$authorDisplay] = $true
277            $commitLine = "- $subject ([$shortSha](https://github.com/$repo/commit/$sha)) by $authorDisplay"
278
279            if ($subject -match '^(?i)(feat|add)') {
280              $feats += $commitLine
281            } elseif ($subject -match '^(?i)(fix|bug|hotfix)') {
282              $fixes += $commitLine
283            } elseif ($subject -match '^(?i)(chore|clean|update|refactor|docs|style|test|ver)') {
284              $chores += $commitLine
285            } else {
286              $others += $commitLine
287            }
288          }
289
290          $notesPath = "Build/release-notes-extra.md"
291
292          $body = @()
293          $body += "## 📝 Changelog"
294          $body += ""
295
296          if ($feats.Count -gt 0) {
297            $body += "### 🚀 Features"
298            $body += $feats
299            $body += ""
300          }
301          if ($fixes.Count -gt 0) {
302            $body += "### 🐛 Bug Fixes"
303            $body += $fixes
304            $body += ""
305          }
306          if ($chores.Count -gt 0) {
307            $body += "### 🔧 Maintenance & Improvements"
308            $body += $chores
309            $body += ""
310          }
311          if ($others.Count -gt 0) {
312            $body += "### 📝 Other Changes"
313            $body += $others
314            $body += ""
315          }
316
317          if ($feats.Count -eq 0 -and $fixes.Count -eq 0 -and $chores.Count -eq 0 -and $others.Count -eq 0) {
318            $body += "- No direct commits in this release range."
319            $body += ""
320          }
321
322          $body += "## 🤝 Contributors"
323          $contributors = @($contributorSet.Keys | Sort-Object)
324          if ($contributors.Count -eq 0) {
325            $body += "- None"
326          } else {
327            $body += "- " + ($contributors -join ", ")
328          }
329
330          if ($previousTag) {
331            $body += ""
332            $body += "---"
333            $body += "_Diff range: [$previousTag..v$env:CURD_VERSION](https://github.com/$repo/compare/$previousTag...v$env:CURD_VERSION)_"
334          }
335
336          $body | Set-Content -Path $notesPath -Encoding utf8
337        shell: pwsh
338
339      - name: Create Release
340        id: create_release
341        uses: softprops/action-gh-release@v2
342        with:
343          tag_name: "v${{ env.CURD_VERSION }}"
344          name: "Curd v${{ env.CURD_VERSION }}"
345          draft: false
346          prerelease: false
347          generate_release_notes: true
348          body_path: Build/release-notes-extra.md
349          files: |
350            releases/curd-${{ env.CURD_VERSION }}/linux/curd-linux-x86_64
351            releases/curd-${{ env.CURD_VERSION }}/linux/curd-linux-arm64
352            releases/curd-${{ env.CURD_VERSION }}/macos/curd-macos-x86_64
353            releases/curd-${{ env.CURD_VERSION }}/macos/curd-macos-arm64
354            releases/curd-${{ env.CURD_VERSION }}/macos/curd-macos-universal
355            releases/curd-${{ env.CURD_VERSION }}/windows/curd-windows-x86_64.exe
356            Build/Output/curd-windows-installer.exe
357        env:
358          GITHUB_TOKEN: ${{ secrets.ACTIONS_PAT }}