Skip to content

Commit

Permalink
Refactor cache pruning
Browse files Browse the repository at this point in the history
  • Loading branch information
paulbalandan committed Aug 5, 2024
1 parent ca46b53 commit 72f562e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 14 deletions.
32 changes: 26 additions & 6 deletions .github/workflows/prune-actions-caches.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ on:
pull_request:
types:
- closed
workflow_dispatch:
inputs:
date_interval:
description: How old should be flushed?
required: true
default: 'P2D'

env:
GH_TOKEN: ${{ secrets.ACCESS_TOKEN }}
REPO: ${{ github.repository }}

jobs:
prune:
Expand All @@ -15,11 +25,21 @@ jobs:
- name: Checkout
uses: actions/checkout@v4

- name: Run prune-cache script
- name: Run prune-cache on pull requests
if: github.event_name == 'pull_request'
run: |
bin/prune-cache ${{ env.REPO }} --pr-branch ${{ env.PR_BRANCH }}
env:
PR_BRANCH: ${{ github.event.pull_request.number }}

- name: Run prune-cache on schedule
if: github.event_name == 'schedule'
run: |
bin/prune-cache ${{ env.REPO }} --schedule
- name: Run prune-cache on workflow dispatch
if: github.event_name == 'workflow_dispatch'
run: |
bin/prune-cache ${{ env.REPO }} ${{ env.PR_BRANCH }} ${{ env.SCHEDULE }}
bin/prune-cache ${{ env.REPO }} --schedule
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPO: ${{ github.repository }}
PR_BRANCH: ${{ github.event_name == 'pull_request' && format('--pr-branch {0}', github.event.pull_request.number) || '' }}
SCHEDULE: ${{ github.event_name == 'schedule' && '--schedule' || '' }}
DATE_INTERVAL: ${{ inputs.date_interval || 'P2D' }}
3 changes: 1 addition & 2 deletions .github/workflows/static-code-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ jobs:
uses: actions/cache@v4
with:
path: ${{ steps.cache-dir.outputs.COMPOSER_CACHE_DIR }}
key: ${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ hashFiles('**/composer.json') }}-${{ github.run_id }}
key: ${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ hashFiles('**/composer.json') }}
restore-keys: |
${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ hashFiles('**/composer.json') }}-
${{ github.workflow }}-PHP_${{ matrix.php-version }}-
- name: Install dependencies
Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,8 @@ jobs:
uses: actions/cache@v4
with:
path: ${{ steps.cache-dir.outputs.COMPOSER_CACHE_DIR }}
key: ${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ matrix.os }}-${{ hashFiles('**/composer.json') }}-${{ github.run_id }}
key: ${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ matrix.os }}-${{ hashFiles('**/composer.json') }}
restore-keys: |
${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ matrix.os }}-${{ hashFiles('**/composer.json') }}-
${{ github.workflow }}-PHP_${{ matrix.php-version }}-${{ matrix.os }}-
${{ github.workflow }}-PHP_${{ matrix.php-version }}-
Expand Down Expand Up @@ -90,7 +89,7 @@ jobs:
env:
COVERALLS_REPO_TOKEN: ${{ secrets.GITHUB_TOKEN }}
COVERALLS_PARALLEL: true
COVERALLS_FLAG_NAME: ${{ format('PHP {0}-{1}', matrix.php-version, matrix.os) }}
COVERALLS_FLAG_NAME: ${{ format('PHP_{0}-{1}', matrix.php-version, matrix.os) }}

coveralls-finish:
needs: tests
Expand Down
21 changes: 18 additions & 3 deletions tools/prune-cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@
'-H "Accept: application/vnd.github+json"',
'-H "X-GitHub-Api-Version: 2022-11-28"',
sprintf('/repos/%s/actions/cache/usage', $repository),
'2>/dev/null',
'2> /dev/null',
];
$cacheUsageOutput = (array) json_decode((string) shell_exec(implode(' ', $activeCacheUsageCommand)), true, flags: JSON_THROW_ON_ERROR);

Expand All @@ -88,7 +88,7 @@
&& '200' !== $cacheUsageOutput['status']
) {
echo sprintf(
"\033[97;41m[ERROR]\033[0m %s (HTTP %d)\n",
"\033[31mFAIL\033[0m %s (HTTP %d)\n",
$cacheUsageOutput['message'],
$cacheUsageOutput['status'],
);
Expand Down Expand Up @@ -174,10 +174,25 @@
}

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

// if pruning other branches, select older than value set by DATE_INTERVAL env
if (preg_match('#refs/pull/\d+/merge#', $cache['ref']) !== 1 && $onSchedule) {
$dateInterval = getenv('DATE_INTERVAL') !== false ? getenv('DATE_INTERVAL') : 'P2D';
$dateInterval = new DateInterval($dateInterval);
$dateTimezone = new DateTimeZone('UTC');
$timeNow = time();
$createdAt = (new DateTimeImmutable($cache['created_at'], $dateTimezone))->add($dateInterval)->getTimestamp();
$lastAccessedAt = (new DateTimeImmutable($cache['last_accessed_at'], $dateTimezone))->add($dateInterval)->getTimestamp();

if ($createdAt > $timeNow && $lastAccessedAt > $timeNow) {
continue;
}
}

$exitCode = 0;
$result = [];
$message = sprintf(
Expand Down

0 comments on commit 72f562e

Please sign in to comment.