|
| 1 | +name: Build Windows executable |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + types: [labeled] |
| 6 | + |
| 7 | +jobs: |
| 8 | + staging_build: |
| 9 | + if: ${{ github.event.label.name == 'release-ready' }} |
| 10 | + runs-on: windows-latest |
| 11 | + |
| 12 | + steps: |
| 13 | + - name: Checkout repository |
| 14 | + uses: actions/checkout@v4 |
| 15 | + with: |
| 16 | + ref: ${{ github.head_ref }} |
| 17 | + |
| 18 | + # Branch name must be in the format "release-vX.XX" |
| 19 | + - name: Extract version from branch name |
| 20 | + id: get_version |
| 21 | + shell: pwsh |
| 22 | + run: | |
| 23 | + $branch = "${{ github.head_ref }}" |
| 24 | + if (-not $branch) { $branch = "${{ github.ref }}" } |
| 25 | + if ($branch -match 'release-v(?<ver>[0-9]+\.[0-9]+)') { |
| 26 | + $ver = $Matches["ver"] |
| 27 | + echo "VERSION_NUM=$ver" >> $env:GITHUB_OUTPUT |
| 28 | + echo "DISPLAY_VERSION=v$ver" >> $env:GITHUB_OUTPUT |
| 29 | + Write-Host "Extracted version: $ver" |
| 30 | + } |
| 31 | + else { |
| 32 | + Write-Error "Branch name does not match expected format (release-vX.XX)." |
| 33 | + } |
| 34 | +
|
| 35 | + - name: Update version in cloc files |
| 36 | + shell: pwsh |
| 37 | + run: | |
| 38 | + $ver = "${{ steps.get_version.outputs.VERSION_NUM }}" |
| 39 | + Write-Host "Updating version in cloc and Unix/cloc to '$ver'" |
| 40 | + $replaceVer = 'my $VERSION = "' + $ver + '";' |
| 41 | + (Get-Content cloc) -replace 'my \$VERSION = ".*";', $replaceVer | Set-Content cloc |
| 42 | + (Get-Content Unix/cloc) -replace 'my \$VERSION = ".*";', $replaceVer | Set-Content Unix/cloc |
| 43 | +
|
| 44 | + - name: Update README.md with version and date |
| 45 | + shell: pwsh |
| 46 | + run: | |
| 47 | + $ver = "${{ steps.get_version.outputs.VERSION_NUM }}" |
| 48 | + $disp = "${{ steps.get_version.outputs.DISPLAY_VERSION }}" |
| 49 | + $currentDate = (Get-Date).ToString("MMM. d, yyyy") |
| 50 | + Write-Host "Updating README.md with version $disp and date $currentDate" |
| 51 | + (Get-Content README.md) ` |
| 52 | + -replace 'Latest release:\s+v\d+\.\d+\s+\(.*\)', "Latest release: $disp ($currentDate)" ` |
| 53 | + -replace 'badge/version-\d+\.\d+', "badge/version-$ver" ` |
| 54 | + -replace 'cloc-\d+\.\d+\.pl', "cloc-$ver.pl" ` |
| 55 | + -replace 'cloc-\d+\.\d+\.exe(?!:)', "cloc-$ver.exe" ` |
| 56 | + -replace 'pp -M Win32::LongPath -M Encode::Unicode -M Digest::MD5 -c -x -o cloc-\d+\.\d+\.exe', "pp -M Win32::LongPath -M Encode::Unicode -M Digest::MD5 -c -x -o cloc-$ver.exe" ` |
| 57 | + -replace '<tt>cloc-\d+\.\d+\.exe</tt>', "<tt>cloc-$ver.exe</tt>" | Set-Content README.md |
| 58 | + |
| 59 | + - name: Install or upgrade Strawberry Perl |
| 60 | + shell: pwsh |
| 61 | + run: | |
| 62 | + if (Get-Command perl -ErrorAction SilentlyContinue) { |
| 63 | + # see https://github.com/StrawberryPerl/Perl-Dist-Strawberry/issues/19#issuecomment-2401043349 |
| 64 | + choco uninstall strawberryperl -y |
| 65 | + } |
| 66 | + choco install strawberryperl --no-progress -y |
| 67 | + perl -V |
| 68 | + cpan -v |
| 69 | +
|
| 70 | + - name: Install CPAN dependencies |
| 71 | + shell: pwsh |
| 72 | + run: | |
| 73 | + cpan -i App::cpanminus |
| 74 | + cpanm Digest::MD5 |
| 75 | + cpanm Regexp::Common |
| 76 | + cpanm Algorithm::Diff |
| 77 | + cpanm PAR::Packer |
| 78 | + cpanm Win32::LongPath || cpanm -n Win32::LongPath |
| 79 | + |
| 80 | + - name: Build executable |
| 81 | + shell: pwsh |
| 82 | + run: | |
| 83 | + $ver = "${{ steps.get_version.outputs.VERSION_NUM }}" |
| 84 | + pp -M Win32::LongPath -M Encode::Unicode -M Digest::MD5 -c -x -o "cloc-$ver.exe" cloc |
| 85 | +
|
| 86 | + - name: Upload executable to VirusTotal and get analysis URL |
| 87 | + id: vt_upload |
| 88 | + shell: pwsh |
| 89 | + run: | |
| 90 | + $ver = "${{ steps.get_version.outputs.VERSION_NUM }}" |
| 91 | + $apiKey = "${{ secrets.VIRUSTOTAL_API_KEY }}" |
| 92 | + $exeFile = "cloc-$ver.exe" |
| 93 | + $absoluteFilePath = (Get-Location).Path + "\" + $exeFile |
| 94 | + Write-Host "Uploading $absoluteFilePath to VirusTotal..." |
| 95 | + $headers=@{} |
| 96 | + $headers.Add("accept", "application/json") |
| 97 | + $headers.Add("content-type", "multipart/form-data") |
| 98 | + $headers.Add("x-apikey", $apiKey) |
| 99 | + $response = Invoke-RestMethod -Uri "https://www.virustotal.com/api/v3/files" ` |
| 100 | + -Method POST ` |
| 101 | + -Headers $headers ` |
| 102 | + -Form @{ file = Get-Item -Path $absoluteFilePath } |
| 103 | + $vtAnalysisId = $response.data.id |
| 104 | + $response2 = Invoke-WebRequest -Uri "https://www.virustotal.com/api/v3/analyses/$vtAnalysisId" -Method GET -Headers $headers |
| 105 | + $vtId = ($response2.Content | ConvertFrom-Json).data.links.item.Split("/")[-1] |
| 106 | + $vtUrl = "https://www.virustotal.com/gui/file/$vtId" |
| 107 | + echo "VT_URL=$vtUrl" >> $env:GITHUB_OUTPUT |
| 108 | + Write-Host "VirusTotal analysis available at $vtUrl" |
| 109 | + |
| 110 | + - name: Upload artifact |
| 111 | + uses: actions/upload-artifact@v4 |
| 112 | + id: upload-artifact |
| 113 | + with: |
| 114 | + name: cloc-${{ steps.get_version.outputs.VERSION_NUM }}-executable |
| 115 | + path: cloc-${{ steps.get_version.outputs.VERSION_NUM }}.exe |
| 116 | + |
| 117 | + - name: Post comment with artifact and VirusTotal URL |
| 118 | + uses: peter-evans/create-or-update-comment@v4 |
| 119 | + with: |
| 120 | + issue-number: ${{ github.event.pull_request.number }} |
| 121 | + body: | |
| 122 | + **Built executable artifact:** |
| 123 | + [Download the executable artifact from this workflow run](${{ steps.upload-artifact.outputs.artifact-url }}) |
| 124 | +
|
| 125 | + **VirusTotal Analysis:** ${{ steps.vt_upload.outputs.VT_URL }} |
| 126 | +
|
| 127 | + - name: Clean up local exe |
| 128 | + shell: pwsh |
| 129 | + run: | |
| 130 | + $ver = "${{ steps.get_version.outputs.VERSION_NUM }}" |
| 131 | + Remove-Item "cloc-$ver.exe" |
| 132 | +
|
| 133 | + - name: Update README recent versions entry |
| 134 | + shell: pwsh |
| 135 | + run: | |
| 136 | + $ver = "${{ steps.get_version.outputs.VERSION_NUM }}" |
| 137 | + $vtUrl = "${{ steps.vt_upload.outputs.VT_URL }}" |
| 138 | + $readme = Get-Content README.md -Raw |
| 139 | + $pattern = 'The entries for recent versions are:\s*\r?\n\s*\r?\n' |
| 140 | + $replacement = "The entries for recent versions are:`n`ncloc-$ver.exe:`n$vtUrl`n`n" |
| 141 | + $newReadme = [regex]::Replace($readme, $pattern, $replacement) |
| 142 | + Set-Content -Path README.md -Value $newReadme |
| 143 | + (Get-Content README.md -Raw) -replace '\r?\n\s*\r?\n$', "" | Set-Content README.md |
| 144 | +
|
| 145 | + - name: Update Unix/NEWS with release notes |
| 146 | + shell: pwsh |
| 147 | + run: | |
| 148 | + $ver = "${{ steps.get_version.outputs.VERSION_NUM }}" |
| 149 | + $currentDate = (Get-Date).ToString("MMM. d, yyyy") |
| 150 | + $event = Get-Content $env:GITHUB_EVENT_PATH -Raw | ConvertFrom-Json |
| 151 | + $prBody = $event.pull_request.body -replace '"', '`"' |
| 152 | + $processedNotes = $prBody -replace '- ', ' o ' -replace '[\*\`_]', '' |
| 153 | + $newsContent = Get-Content Unix/NEWS -Raw -Encoding UTF8 |
| 154 | + $header = @( |
| 155 | + " Release Notes for cloc version $ver", |
| 156 | + " https://github.com/AlDanial/cloc", |
| 157 | + " $currentDate" |
| 158 | + ) |
| 159 | + $newNews = $header + "" + $processedNotes + "" + ("=" * 76) + $newsContent |
| 160 | + Set-Content -Path Unix/NEWS -Value $newNews -Encoding UTF8 |
| 161 | + (Get-Content Unix/NEWS -Raw) -replace '\r?\n\s*\r?\n$', "" | Set-Content Unix/NEWS -Encoding UTF8 |
| 162 | +
|
| 163 | + - name: Commit changes |
| 164 | + shell: pwsh |
| 165 | + run: | |
| 166 | + $ver = "${{ steps.get_version.outputs.VERSION_NUM }}" |
| 167 | + git config user.name "github-actions" |
| 168 | + git config user.email "actions@github.com" |
| 169 | + git add cloc README.md Unix/NEWS Unix/cloc |
| 170 | + git commit -m "chore: prepare release version $ver" |
| 171 | + git push |
0 commit comments