1
# SPDX-License-Identifier: LGPL-2.1-or-later
2
# ***************************************************************************
4
# * Copyright (c) 2023 0penBrain. *
6
# * This file is part of FreeCAD. *
8
# * FreeCAD is free software: you can redistribute it and/or modify it *
9
# * under the terms of the GNU Lesser General Public License as *
10
# * published by the Free Software Foundation, either version 2.1 of the *
11
# * License, or (at your option) any later version. *
13
# * FreeCAD is distributed in the hope that it will be useful, but *
14
# * WITHOUT ANY WARRANTY; without even the implied warranty of *
15
# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16
# * Lesser General Public License for more details. *
18
# * You should have received a copy of the GNU Lesser General Public *
19
# * License along with FreeCAD. If not, see *
20
# * <https://www.gnu.org/licenses/>. *
22
# ***************************************************************************
24
# This workflow is a complementary one to the master CI.
25
# It aims at doing cleanup operations after a CI workflow ran.
26
# Being triggered when the master workflow ends allows it to run with necessary privileges.
27
# Indeed it always run with push-like rights even for PR events.
29
# In order to work, this cleanup workflow imposes name formatting for caches
30
# Caches that have to be cleaned (typically compiler caches) shall be named as below :
31
# ${MARK}-${CONTEXT}-${REF}-${ID}
33
# ${MARK} => A mark identifying a cache to be cleaned, defined as being "FC" (without quotes)
34
# ${CONTEXT} => A string identifying cache saving context, typically OS name or compiler name
35
# ${REF} => The full reference of the branch owning the cache (starting with "/refs/pull/" or "/refs/heads/")
36
# ${ID} => A cache unique identifier, generally an ascending number, in no case containing a '-' (hyphen) sign
38
name: FreeCAD CI cleaner
42
workflows: [FreeCAD master CI]
51
cancel-in-progress: false
56
runs-on: ubuntu-latest
60
- name: Make needed directories
62
mkdir -p ${{ env.logdir }}
63
- name: Get existing caches for the repo
65
curl -H "Accept: application/vnd.github+json" -H "authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" $GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/caches > ${{ env.logdir }}caches.json
66
- name: Extract pull request caches
68
# Extract caches of which names starts with MARK and contains "/refs/pull/"
69
jq ".actions_caches | map(select(.key | startswith(\"FC-\"))) | map(select(.key | contains(\"refs/pull/\")))" ${{ env.logdir }}caches.json > ${{ env.logdir }}pulls.json
70
- name: Extract and delete pull request obsolete cache IDs
72
# Group the caches by MARK-CONTEXT-REF, sort by ascending last access datetime and keep all but the last as to be deleted
73
# As a consequence, for pull requests, only the most recent cache is kept (one for each context and for each PR)
74
PRID=$(jq "group_by(.key | .[:rindex(\"-\")]) | .[] | sort_by(.last_accessed_at) | .[:-1][].id" ${{ env.logdir }}pulls.json)
77
echo "Trying to delete pull request obsolete cache ID : $id"
78
if [ ${{ env.dryrun }} == "false" ]
80
curl -X DELETE -H "Accept: application/vnd.github+json" -H "authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" $GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/caches/$id
82
echo "DRYRUN: executing : curl -X DELETE -H \"Accept: application/vnd.github+json\" $GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/caches/$id"
85
- name: Extract push caches
87
# Extract caches of which names starts with MARK and contains "/refs/heads/"
88
jq ".actions_caches | map(select(.key | startswith(\"FC-\"))) | map(select(.key | contains(\"refs/heads/\")))" ${{ env.logdir }}caches.json > ${{ env.logdir }}pushes.json
89
- name: Extract and delete push obsolete cache IDs
91
# Group the caches by MARK-CONTEXT-REF, sort by ascending last access datetime, keep all but the last 2 and keep all accessed for more than 1 hour as to be deleted
92
# As a consequence, for pushes (repo branches), at least 2 caches (for each context and for each branch) are kept, others are deleted if they have been useless for more than 1 hour
93
PSID=$(jq "group_by(.key | .[:rindex(\"-\")]) | .[] | sort_by(.last_accessed_at) | .[:-2][] | select((.last_accessed_at | if contains(\".\") then .[:rindex(\".\")]+\"Z\" else . end | fromdateiso8601) < (now | floor - 3600)) | .id" ${{ env.logdir }}pushes.json)
96
echo "Trying to delete push obsolete cache ID : $id"
97
if [ ${{ env.dryrun }} == "false" ]
99
curl -X DELETE -H "Accept: application/vnd.github+json" -H "authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" $GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/caches/$id
101
echo "DRYRUN: executing : curl -X DELETE -H \"Accept: application/vnd.github+json\" $GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/caches/$id"
106
uses: actions/upload-artifact@v4
108
name: ${{ github.job }}-Logs