Skip to content

Commit 72f562e

Browse files
committed
Refactor cache pruning
1 parent ca46b53 commit 72f562e

File tree

4 files changed

+47
-14
lines changed

4 files changed

+47
-14
lines changed

.github/workflows/prune-actions-caches.yml

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ on:
66
pull_request:
77
types:
88
- closed
9+
workflow_dispatch:
10+
inputs:
11+
date_interval:
12+
description: How old should be flushed?
13+
required: true
14+
default: 'P2D'
15+
16+
env:
17+
GH_TOKEN: ${{ secrets.ACCESS_TOKEN }}
18+
REPO: ${{ github.repository }}
919

1020
jobs:
1121
prune:
@@ -15,11 +25,21 @@ jobs:
1525
- name: Checkout
1626
uses: actions/checkout@v4
1727

18-
- name: Run prune-cache script
28+
- name: Run prune-cache on pull requests
29+
if: github.event_name == 'pull_request'
30+
run: |
31+
bin/prune-cache ${{ env.REPO }} --pr-branch ${{ env.PR_BRANCH }}
32+
env:
33+
PR_BRANCH: ${{ github.event.pull_request.number }}
34+
35+
- name: Run prune-cache on schedule
36+
if: github.event_name == 'schedule'
37+
run: |
38+
bin/prune-cache ${{ env.REPO }} --schedule
39+
40+
- name: Run prune-cache on workflow dispatch
41+
if: github.event_name == 'workflow_dispatch'
1942
run: |
20-
bin/prune-cache ${{ env.REPO }} ${{ env.PR_BRANCH }} ${{ env.SCHEDULE }}
43+
bin/prune-cache ${{ env.REPO }} --schedule
2144
env:
22-
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
23-
REPO: ${{ github.repository }}
24-
PR_BRANCH: ${{ github.event_name == 'pull_request' && format('--pr-branch {0}', github.event.pull_request.number) || '' }}
25-
SCHEDULE: ${{ github.event_name == 'schedule' && '--schedule' || '' }}
45+
DATE_INTERVAL: ${{ inputs.date_interval || 'P2D' }}

.github/workflows/static-code-analysis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ jobs:
4646
uses: actions/cache@v4
4747
with:
4848
path: ${{ steps.cache-dir.outputs.COMPOSER_CACHE_DIR }}
49-
key: ${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ hashFiles('**/composer.json') }}-${{ github.run_id }}
49+
key: ${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ hashFiles('**/composer.json') }}
5050
restore-keys: |
51-
${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ hashFiles('**/composer.json') }}-
5251
${{ github.workflow }}-PHP_${{ matrix.php-version }}-
5352
5453
- name: Install dependencies

.github/workflows/unit-tests.yml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,8 @@ jobs:
5555
uses: actions/cache@v4
5656
with:
5757
path: ${{ steps.cache-dir.outputs.COMPOSER_CACHE_DIR }}
58-
key: ${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ matrix.os }}-${{ hashFiles('**/composer.json') }}-${{ github.run_id }}
58+
key: ${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ matrix.os }}-${{ hashFiles('**/composer.json') }}
5959
restore-keys: |
60-
${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ matrix.os }}-${{ hashFiles('**/composer.json') }}-
6160
${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ matrix.os }}-
6261
${{ github.workflow }}-PHP_${{ matrix.php-version }}-
6362
@@ -90,7 +89,7 @@ jobs:
9089
env:
9190
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
9291
COVERALLS_PARALLEL: true
93-
COVERALLS_FLAG_NAME: ${{ format('PHP {0}-{1}', matrix.php-version, matrix.os) }}
92+
COVERALLS_FLAG_NAME: ${{ format('PHP_{0}-{1}', matrix.php-version, matrix.os) }}
9493

9594
coveralls-finish:
9695
needs: tests

tools/prune-cache.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
'-H "Accept: application/vnd.github+json"',
7878
'-H "X-GitHub-Api-Version: 2022-11-28"',
7979
sprintf('/repos/%s/actions/cache/usage', $repository),
80-
'2>/dev/null',
80+
'2> /dev/null',
8181
];
8282
$cacheUsageOutput = (array) json_decode((string) shell_exec(implode(' ', $activeCacheUsageCommand)), true, flags: JSON_THROW_ON_ERROR);
8383

@@ -88,7 +88,7 @@
8888
&& '200' !== $cacheUsageOutput['status']
8989
) {
9090
echo sprintf(
91-
"\033[97;41m[ERROR]\033[0m %s (HTTP %d)\n",
91+
"\033[31mFAIL\033[0m %s (HTTP %d)\n",
9292
$cacheUsageOutput['message'],
9393
$cacheUsageOutput['status'],
9494
);
@@ -174,10 +174,25 @@
174174
}
175175

176176
foreach ($caches['actions_caches'] as $cache) {
177-
if (preg_match('#refs/pull/\d+/merge#', $cache['ref']) !== 1) {
177+
// if pruning PRs, select matching refs
178+
if (preg_match('#refs/pull/\d+/merge#', $cache['ref']) !== 1 && ! $onSchedule) {
178179
continue;
179180
}
180181

182+
// if pruning other branches, select older than value set by DATE_INTERVAL env
183+
if (preg_match('#refs/pull/\d+/merge#', $cache['ref']) !== 1 && $onSchedule) {
184+
$dateInterval = getenv('DATE_INTERVAL') !== false ? getenv('DATE_INTERVAL') : 'P2D';
185+
$dateInterval = new DateInterval($dateInterval);
186+
$dateTimezone = new DateTimeZone('UTC');
187+
$timeNow = time();
188+
$createdAt = (new DateTimeImmutable($cache['created_at'], $dateTimezone))->add($dateInterval)->getTimestamp();
189+
$lastAccessedAt = (new DateTimeImmutable($cache['last_accessed_at'], $dateTimezone))->add($dateInterval)->getTimestamp();
190+
191+
if ($createdAt > $timeNow && $lastAccessedAt > $timeNow) {
192+
continue;
193+
}
194+
}
195+
181196
$exitCode = 0;
182197
$result = [];
183198
$message = sprintf(

0 commit comments

Comments
 (0)