.github/workflows/ci-cd.yml

325 lines · 11477 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@v4
 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          $directCommitLines = @()
222          $contributorSet = @{}
223
224          foreach ($entry in $rawCommits) {
225            $parts = $entry -split "`t", 3
226            if ($parts.Count -lt 3) {
227              continue
228            }
229
230            $sha = $parts[0].Trim()
231            $subject = $parts[1].Trim()
232            $authorName = $parts[2].Trim()
233
234            if ([string]::IsNullOrWhiteSpace($sha) -or [string]::IsNullOrWhiteSpace($subject)) {
235              continue
236            }
237
238            if ($subject -match '^\s*release:') {
239              continue
240            }
241
242            $pullsUri = "https://api.github.com/repos/$repo/commits/$sha/pulls"
243            $pulls = @()
244            try {
245              $pulls = @(Invoke-RestMethod -Uri $pullsUri -Headers $headers -Method Get)
246            } catch {
247              $pulls = @()
248            }
249
250            # Skip commits that are already represented by pull requests in generated notes.
251            if ($pulls.Count -gt 0) {
252              continue
253            }
254
255            $commitUri = "https://api.github.com/repos/$repo/commits/$sha"
256            $login = $null
257            try {
258              $commitData = Invoke-RestMethod -Uri $commitUri -Headers $headers -Method Get
259              if ($null -ne $commitData.author -and $null -ne $commitData.author.login) {
260                $login = $commitData.author.login
261              }
262            } catch {
263              $login = $null
264            }
265
266            $shortSha = $sha.Substring(0, 7)
267            if (-not [string]::IsNullOrWhiteSpace($login)) {
268              $authorDisplay = "[@$login](https://github.com/$login)"
269            } else {
270              $authorDisplay = $authorName
271            }
272
273            $contributorSet[$authorDisplay] = $true
274            $directCommitLines += "- $subject ([$shortSha](https://github.com/$repo/commit/$sha)) by $authorDisplay"
275          }
276
277          $notesPath = "Build/release-notes-extra.md"
278
279          $body = @()
280          $body += "## Direct Commits"
281          if ($directCommitLines.Count -eq 0) {
282            $body += "- No direct commits in this release range."
283          } else {
284            $body += $directCommitLines
285          }
286
287          $body += ""
288          $body += "## Direct Commit Contributors"
289          $contributors = @($contributorSet.Keys | Sort-Object)
290          if ($contributors.Count -eq 0) {
291            $body += "- None"
292          } else {
293            foreach ($contributor in $contributors) {
294              $body += "- $contributor"
295            }
296          }
297
298          if ($previousTag) {
299            $body += ""
300            $body += "_Range: $previousTag..HEAD_"
301          }
302
303          $body | Set-Content -Path $notesPath -Encoding utf8
304        shell: pwsh
305
306      - name: Create Release
307        id: create_release
308        uses: softprops/action-gh-release@v1
309        with:
310          tag_name: "v${{ env.CURD_VERSION }}"
311          name: "Curd v${{ env.CURD_VERSION }}"
312          draft: false
313          prerelease: false
314          generate_release_notes: true
315          body_path: Build/release-notes-extra.md
316          files: |
317            releases/curd-${{ env.CURD_VERSION }}/linux/curd-linux-x86_64
318            releases/curd-${{ env.CURD_VERSION }}/linux/curd-linux-arm64
319            releases/curd-${{ env.CURD_VERSION }}/macos/curd-macos-x86_64
320            releases/curd-${{ env.CURD_VERSION }}/macos/curd-macos-arm64
321            releases/curd-${{ env.CURD_VERSION }}/macos/curd-macos-universal
322            releases/curd-${{ env.CURD_VERSION }}/windows/curd-windows-x86_64.exe
323            Build/Output/curd-windows-installer.exe
324        env:
325          GITHUB_TOKEN: ${{ secrets.ACTIONS_PAT }}