Skip to content

Commit f375c69

Browse files
committed
ci: Make build counter per workflow and not per build.
1 parent 3aa409a commit f375c69

File tree

1 file changed

+49
-27
lines changed

1 file changed

+49
-27
lines changed

.github/workflows/build.yml

Lines changed: 49 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,38 @@ on: [push]
55
permissions:
66
contents: write
77

8+
concurrency:
9+
group: ${{ github.workflow }}-${{ github.ref_name }}
10+
cancel-in-progress: false
11+
812
jobs:
13+
14+
compute-revision:
15+
runs-on: windows-latest
16+
outputs:
17+
rev: ${{ steps.branch_build.outputs.number }}
18+
steps:
19+
- name: Compute per-branch build number
20+
id: branch_build
21+
env:
22+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23+
shell: pwsh
24+
run: |
25+
$ownerRepo = "${{ github.repository }}"
26+
$branch = if ($env:GITHUB_HEAD_REF) { $env:GITHUB_HEAD_REF } else { $env:GITHUB_REF_NAME }
27+
$wfRef = "${{ github.workflow_ref }}"
28+
if ($wfRef -match "\.github/workflows/(?<file>[^@]+)@") { $wfFile = $matches.file } else { throw "Couldn't parse workflow file from workflow_ref" }
29+
$uri = "https://api.github.com/repos/$ownerRepo/actions/workflows/$wfFile/runs?branch=$branch&per_page=1"
30+
$headers = @{
31+
Authorization = "Bearer $env:GH_TOKEN"
32+
"X-GitHub-Api-Version" = "2022-11-28"
33+
"User-Agent" = "branch-counter"
34+
}
35+
$resp = Invoke-RestMethod -Uri $uri -Headers $headers -Method GET
36+
"number=$([int]$resp.total_count)" >> $env:GITHUB_OUTPUT
37+
938
build-windows:
39+
needs: compute-revision
1040
runs-on: windows-latest
1141
permissions:
1242
packages: write
@@ -15,9 +45,6 @@ jobs:
1545
matrix:
1646
platform: [x86, x64]
1747
configuration: [Release]
18-
concurrency:
19-
group: ${{ github.workflow }}-${{ github.ref }}
20-
cancel-in-progress: false
2148
env:
2249
NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages
2350
steps:
@@ -85,52 +112,48 @@ jobs:
85112
- name: Update version info
86113
id: set_version
87114
shell: pwsh
88-
env:
89-
BRANCH_BUILD: ${{ steps.branch_build.outputs.number }}
90115
run: |
91116
function replaceNumericVersion($name, $fileContent) {
92-
$RegularExpression = [regex] "^[^/]*\[assembly:\s*$name\(\""(\d+)\.(\d+)\.(\d+)(\.(\d+))?\""\)"
117+
$re = [regex] '^[^/]*\[assembly:\s*' + [regex]::Escape($name) + '\("(\d+)\.(\d+)\.(\d+)(\.(\d+))?"\)'
93118
foreach($content in $fileContent) {
94-
$match = [System.Text.RegularExpressions.Regex]::Match($content, $RegularExpression)
95-
if ($match.Success) {
96-
$major=$match.groups[1].value -as [int]
97-
$minor=$match.groups[2].value -as [int]
98-
$patch=$match.groups[3].value -as [int]
99-
return $fileContent -replace "^[^/]*\[assembly:\s*$name\(\""[^""]*\""\)", "[assembly: $name(""$major.$minor.$patch.$env:BRANCH_BUILD"")]".TrimEnd("]")
119+
$m = [System.Text.RegularExpressions.Regex]::Match($content, $re)
120+
if ($m.Success) {
121+
$major=$m.groups[1].value -as [int]
122+
$minor=$m.groups[2].value -as [int]
123+
$patch=$m.groups[3].value -as [int]
124+
return $fileContent -replace '^[^/]*\[assembly:\s*' + [regex]::Escape($name) + '\("[^"]*"\)', "[assembly: $name(""$major.$minor.$patch.$env:BRANCH_BUILD"")]".TrimEnd("]")
100125
}
101126
}
102127
}
103128
104129
function replaceFullVersion($name, $fileContent) {
105-
$RegularExpression = [regex] "[^/]*\[assembly:\s*$name\(\""(\d)\.(\d)\.(\d)(-([\w\W]+))?\""\)"
130+
$re = [regex] '[^/]*\[assembly:\s*' + [regex]::Escape($name) + '\("(\d)\.(\d)\.(\d)(-([\w\W]+))?"\)'
106131
foreach($content in $fileContent) {
107-
$match = [System.Text.RegularExpressions.Regex]::Match($content, $RegularExpression)
108-
if ($match.Success) {
109-
$major=$match.groups[1].value -as [int]
110-
$minor=$match.groups[2].value -as [int]
111-
$patch=$match.groups[3].value -as [int]
112-
$tag=$match.groups[5].value
113-
132+
$m = [System.Text.RegularExpressions.Regex]::Match($content, $re)
133+
if ($m.Success) {
134+
$major=$m.groups[1].value -as [int]
135+
$minor=$m.groups[2].value -as [int]
136+
$patch=$m.groups[3].value -as [int]
137+
$tag=$m.groups[5].value
114138
$branch = if ($env:GITHUB_HEAD_REF) { $env:GITHUB_HEAD_REF } else { $env:GITHUB_REF_NAME }
115139
if ($branch -eq "master" -or $branch -eq "main") {
116140
if ($tag) { $version = "$major.$minor.$patch-$tag-r$env:BRANCH_BUILD" }
117141
else { $version = "$major.$minor.$patch-r$env:BRANCH_BUILD" }
118142
} else {
119143
$version = "$major.$minor.$patch-$($branch.ToUpper())-r$env:BRANCH_BUILD"
120144
}
121-
122145
"version=$version" >> $env:GITHUB_OUTPUT
123-
return $fileContent -replace "^[^/]*\[assembly:\s*$name\(\""[^""]*\""\)", "[assembly: $name(""$version"")]".TrimEnd("]")
146+
return $fileContent -replace '^[^/]*\[assembly:\s*' + [regex]::Escape($name) + '\("[^"]*"\)', "[assembly: $name(""$version"")]".TrimEnd("]")
124147
}
125148
}
126149
}
127150
128151
function replaceAny($name, $replaceWith, $fileContent) {
129-
$RegularExpression = [regex] "^[^/]*\[assembly:\s*$name\(\""[^""]*\""\)"
152+
$re = [regex] '^[^/]*\[assembly:\s*' + [regex]::Escape($name) + '\("[^"]*"\)'
130153
foreach($content in $fileContent) {
131-
$match = [System.Text.RegularExpressions.Regex]::Match($content, $RegularExpression)
132-
if ($match.Success) {
133-
return $fileContent -replace "^[^/]*\[assembly:\s*$name\(\""[^""]*\""\)", "[assembly: $name(""$replaceWith"")]".TrimEnd("]")
154+
$m = [System.Text.RegularExpressions.Regex]::Match($content, $re)
155+
if ($m.Success) {
156+
return $fileContent -replace $re, "[assembly: $name(""$replaceWith"")]".TrimEnd("]")
134157
}
135158
}
136159
}
@@ -145,7 +168,6 @@ jobs:
145168
}
146169
$fileContent | Set-Content "$assemblyFile"
147170
148-
149171
- name: Build
150172
run: msbuild -t:rebuild /p:Platform=${{ matrix.platform }} /p:Configuration=${{ matrix.configuration }} DmdExtensions.sln
151173

0 commit comments

Comments
 (0)