Skip to content

Commit 3aa409a

Browse files
committed
ci: Count revision separately for each branch.
1 parent 51e0ccd commit 3aa409a

File tree

1 file changed

+41
-10
lines changed

1 file changed

+41
-10
lines changed

.github/workflows/build.yml

Lines changed: 41 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ jobs:
1515
matrix:
1616
platform: [x86, x64]
1717
configuration: [Release]
18+
concurrency:
19+
group: ${{ github.workflow }}-${{ github.ref }}
20+
cancel-in-progress: false
1821
env:
1922
NUGET_PACKAGES: ${{ github.workspace }}/.nuget/packages
2023
steps:
@@ -54,9 +57,36 @@ jobs:
5457
- name: Restore DLLExport dependency
5558
run: .\DllExport -action Restore -sln-file DmdExtensions.sln
5659

60+
- name: Compute per-branch build number
61+
id: branch_build
62+
env:
63+
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
64+
shell: pwsh
65+
run: |
66+
$ownerRepo = "${{ github.repository }}"
67+
$branch = if ($env:GITHUB_HEAD_REF) { $env:GITHUB_HEAD_REF } else { $env:GITHUB_REF_NAME }
68+
69+
# Parse the workflow file name from workflow_ref: owner/repo/.github/workflows/<file>@<ref>
70+
$wfRef = "${{ github.workflow_ref }}"
71+
if ($wfRef -match "\.github/workflows/(?<file>[^@]+)@") { $wfFile = $matches.file } else { throw "Couldn't parse workflow file from workflow_ref" }
72+
73+
$uri = "https://api.github.com/repos/$ownerRepo/actions/workflows/$wfFile/runs?branch=$branch&per_page=1"
74+
$headers = @{
75+
Authorization = "Bearer $env:GH_TOKEN"
76+
"X-GitHub-Api-Version" = "2022-11-28"
77+
"User-Agent" = "branch-counter"
78+
}
79+
80+
$resp = Invoke-RestMethod -Uri $uri -Headers $headers -Method GET
81+
# total_count includes this current run, which is what we want (first run => 1)
82+
$count = [int]$resp.total_count
83+
"number=$count" >> $env:GITHUB_OUTPUT
84+
5785
- name: Update version info
5886
id: set_version
5987
shell: pwsh
88+
env:
89+
BRANCH_BUILD: ${{ steps.branch_build.outputs.number }}
6090
run: |
6191
function replaceNumericVersion($name, $fileContent) {
6292
$RegularExpression = [regex] "^[^/]*\[assembly:\s*$name\(\""(\d+)\.(\d+)\.(\d+)(\.(\d+))?\""\)"
@@ -66,11 +96,11 @@ jobs:
6696
$major=$match.groups[1].value -as [int]
6797
$minor=$match.groups[2].value -as [int]
6898
$patch=$match.groups[3].value -as [int]
69-
return $fileContent -replace "^[^/]*\[assembly:\s*$name\(\""[^""]*\""\)", "[assembly: $name(""$major.$minor.$patch.$env:GITHUB_RUN_NUMBER"")]".TrimEnd("]") # remove trailing ]
99+
return $fileContent -replace "^[^/]*\[assembly:\s*$name\(\""[^""]*\""\)", "[assembly: $name(""$major.$minor.$patch.$env:BRANCH_BUILD"")]".TrimEnd("]")
70100
}
71101
}
72102
}
73-
103+
74104
function replaceFullVersion($name, $fileContent) {
75105
$RegularExpression = [regex] "[^/]*\[assembly:\s*$name\(\""(\d)\.(\d)\.(\d)(-([\w\W]+))?\""\)"
76106
foreach($content in $fileContent) {
@@ -80,21 +110,21 @@ jobs:
80110
$minor=$match.groups[2].value -as [int]
81111
$patch=$match.groups[3].value -as [int]
82112
$tag=$match.groups[5].value
83-
113+
84114
$branch = if ($env:GITHUB_HEAD_REF) { $env:GITHUB_HEAD_REF } else { $env:GITHUB_REF_NAME }
85115
if ($branch -eq "master" -or $branch -eq "main") {
86-
if ($tag) { $version = "$major.$minor.$patch-$tag-r$env:GITHUB_RUN_NUMBER" }
87-
else { $version = "$major.$minor.$patch-r$env:GITHUB_RUN_NUMBER" }
116+
if ($tag) { $version = "$major.$minor.$patch-$tag-r$env:BRANCH_BUILD" }
117+
else { $version = "$major.$minor.$patch-r$env:BRANCH_BUILD" }
88118
} else {
89-
$version = "$major.$minor.$patch-$($branch.ToUpper())-r$env:GITHUB_RUN_NUMBER"
119+
$version = "$major.$minor.$patch-$($branch.ToUpper())-r$env:BRANCH_BUILD"
90120
}
91-
121+
92122
"version=$version" >> $env:GITHUB_OUTPUT
93123
return $fileContent -replace "^[^/]*\[assembly:\s*$name\(\""[^""]*\""\)", "[assembly: $name(""$version"")]".TrimEnd("]")
94124
}
95125
}
96126
}
97-
127+
98128
function replaceAny($name, $replaceWith, $fileContent) {
99129
$RegularExpression = [regex] "^[^/]*\[assembly:\s*$name\(\""[^""]*\""\)"
100130
foreach($content in $fileContent) {
@@ -104,7 +134,7 @@ jobs:
104134
}
105135
}
106136
}
107-
137+
108138
$assemblyFile = "VersionAssemblyInfo.cs"
109139
$fileContent = Get-Content $assemblyFile
110140
$fileContent = replaceNumericVersion 'AssemblyVersion' $fileContent
@@ -115,6 +145,7 @@ jobs:
115145
}
116146
$fileContent | Set-Content "$assemblyFile"
117147
148+
118149
- name: Build
119150
run: msbuild -t:rebuild /p:Platform=${{ matrix.platform }} /p:Configuration=${{ matrix.configuration }} DmdExtensions.sln
120151

@@ -189,4 +220,4 @@ jobs:
189220
name: dmdext-v${{ steps.set_version.outputs.version }}-${{ matrix.platform }}-msi
190221
path: ${{ github.workspace }}\Installer\Builds\dmdext-v${{ steps.set_version.outputs.version }}-${{ matrix.platform }}.msi
191222
retention-days: 90
192-
if-no-files-found: error
223+
if-no-files-found: error

0 commit comments

Comments
 (0)