-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsnapshot-github-repositories-popularity-score.yml
More file actions
146 lines (124 loc) 路 6.22 KB
/
Copy pathsnapshot-github-repositories-popularity-score.yml
File metadata and controls
146 lines (124 loc) 路 6.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
name: 3-snapshot-github-repositories-popularity-score
# Save a snapshot of the referenced GitHub repositories popularity score (number of stars + number of watchers) to a dedicated JSON file
# Workflow triggered when the README workflow completes successfully on main - but can also be triggered manually without input parameters
on:
workflow_dispatch:
workflow_run:
workflows:
- 2-update-readme-with-github-repositories-details
types:
- completed
permissions:
contents: write
# Concurrency configuration for the current workflow - Keep only the latest workflow queued for the considered group
concurrency:
group: snapshot-github-repositories-popularity-score
cancel-in-progress: true
jobs:
snapshot-github-repositories-popularity-score:
runs-on: ubuntu-latest
if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch == 'main') }}
concurrency:
group: main-repository-writers
cancel-in-progress: false
env:
RUNNER_DEBUG: 1
DATA_SNAPSHOT_METADATA_FILE_PATH: Data/GitHubRepositoriesPopularityScoresSnapshotMetadata.json
steps:
# Action used to checkout the main branch in the current repository
# Community action: https://github.com/actions/checkout
- name: Checkout
uses: actions/checkout@v7
with:
ref: main
fetch-depth: 0
# Set a current date environment variable in the following format: YYYYMMDD
- name: Set current date as env variable
id: current_date
run: echo "NOW=$(date +'%Y%m%d')" >> $env:GITHUB_OUTPUT
shell: pwsh
# Set the name of the current day in the week as a variable
- name: Set the name of the current day in the week as env variable
id: current_day_in_the_week
run: echo "DAY=$(date +'%A')" >> $env:GITHUB_OUTPUT
shell: pwsh
# If the current day in the week is Monday, take a snapshot of the popularity score of the referenced GitHub repositories - For the tests we will configure the step for the next day (Thursday)
- name: Take a snapshot of the popularity score of the referenced GitHub repositories
if: ${{ steps.current_day_in_the_week.outputs.DAY == 'Monday' }}
run: |
Write-Host "/********************************************************************************/"
Write-Host "Install required modules"
Import-Module .\Scripts\Export-GitHubRepositoriesPopularityScore.ps1 -Force
Write-Host "/********************************************************************************/"
Write-Host "Take a snapshot of the popularity score of the referenced GitHub repositories"
$results = Export-GitHubRepositoriesPopularityScore `
-InputFilePath "${{ vars.DATA_FILE_PATH }}" `
-OutputFilePath "${{ vars.DATA_SNAPSHOT_FILE_PATH }}" `
-MetadataOutputFilePath ".\$env:DATA_SNAPSHOT_METADATA_FILE_PATH"
shell: pwsh
# Push the changes in the current repository
- name: Push changes
run: |
git config --global user.name 'action@github.com'
git config --global user.email 'GitHub Action'
git add -- "${{ vars.DATA_SNAPSHOT_FILE_PATH }}" "$env:DATA_SNAPSHOT_METADATA_FILE_PATH"
git diff --cached --quiet
$gitDiffExitCode = $LASTEXITCODE
if ($gitDiffExitCode -eq 0) {
Write-Host "No popularity snapshot changes to commit."
if ($env:GITHUB_STEP_SUMMARY) {
@(
"### Push changes"
""
"- Snapshot files were unchanged, so commit and push were skipped."
) | Add-Content -Path $env:GITHUB_STEP_SUMMARY
}
exit 0
}
if ($gitDiffExitCode -ne 1) {
throw "Unable to check staged snapshot changes. git diff exited with code $gitDiffExitCode."
}
git commit -m "GitHub repositories popularity score updated - ${{ steps.current_date.outputs.NOW }}.${{ github.run_number }}"
$pushSucceeded = $false
$usedRetry = $false
for ($attempt = 1; $attempt -le 2; $attempt++) {
$pushOutput = & git -c http.extraheader="AUTHORIZATION: Bearer ${{ secrets.GITHUB_TOKEN }}" push origin HEAD:main 2>&1
$pushExitCode = $LASTEXITCODE
$pushOutput | ForEach-Object { Write-Host $_ }
if ($pushExitCode -eq 0) {
$pushSucceeded = $true
break
}
$isFastForwardConflict = ($pushOutput | Out-String) -match 'fetch first|non-fast-forward|failed to push some refs'
if ($attempt -eq 2 -or -not $isFastForwardConflict) {
throw "Unable to push snapshot changes. git push exited with code $pushExitCode."
}
$usedRetry = $true
Write-Warning "Remote main moved during push attempt $attempt. Fetching origin/main, rebasing, and retrying once."
if ($env:GITHUB_STEP_SUMMARY) {
@(
"### Push retry"
""
"- Push attempt $attempt was rejected because `main` moved."
"- Fetching `origin/main`, rebasing the snapshot commit, and retrying once."
) | Add-Content -Path $env:GITHUB_STEP_SUMMARY
}
& git fetch origin main 2>&1 | ForEach-Object { Write-Host $_ }
if ($LASTEXITCODE -ne 0) {
throw "Unable to fetch origin/main before retrying the snapshot push."
}
& git rebase origin/main 2>&1 | ForEach-Object { Write-Host $_ }
if ($LASTEXITCODE -ne 0) {
& git rebase --abort 2>&1 | ForEach-Object { Write-Host $_ }
throw "Unable to rebase snapshot changes onto origin/main before retrying the push."
}
}
if (-not $pushSucceeded) {
throw "Unable to push snapshot changes after retrying."
}
if ($usedRetry -and $env:GITHUB_STEP_SUMMARY) {
@(
"- The retry path completed successfully and the rebased snapshot commit was pushed to `main`."
) | Add-Content -Path $env:GITHUB_STEP_SUMMARY
}
shell: pwsh