FreeCAD

Форк
0
/
CI_cleanup.yml 
110 строк · 6.2 Кб
1
# SPDX-License-Identifier: LGPL-2.1-or-later
2
# ***************************************************************************
3
# *                                                                         *
4
# *   Copyright (c) 2023 0penBrain.                                         *
5
# *                                                                         *
6
# *   This file is part of FreeCAD.                                         *
7
# *                                                                         *
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.                       *
12
# *                                                                         *
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.                       *
17
# *                                                                         *
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/>.                                      *
21
# *                                                                         *
22
# ***************************************************************************
23

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.
28

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}
32
# with :
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
37

38
name: FreeCAD CI cleaner
39

40
on:
41
  workflow_run:
42
    workflows: [FreeCAD master CI]
43
    types:
44
      - completed
45

46
env:
47
  dryrun: false
48

49
concurrency:
50
  group: FC-CI-cleaner
51
  cancel-in-progress: false
52

53
jobs:
54

55
  CachesCleanup:
56
    runs-on: ubuntu-latest
57
    env:
58
      logdir: /tmp/log/
59
    steps:
60
      - name: Make needed directories
61
        run: |
62
          mkdir -p ${{ env.logdir }}
63
      - name: Get existing caches for the repo
64
        run: |
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
67
        run: |
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
71
        run: |
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)
75
          for id in $PRID
76
          do
77
            echo "Trying to delete pull request obsolete cache ID : $id"
78
            if [ ${{ env.dryrun }} == "false" ]
79
            then
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
81
            else
82
              echo "DRYRUN: executing : curl -X DELETE -H \"Accept: application/vnd.github+json\" $GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/caches/$id"
83
            fi
84
          done
85
      - name: Extract push caches
86
        run: |
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
90
        run: |
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)
94
          for id in $PSID
95
          do
96
            echo "Trying to delete push obsolete cache ID : $id"
97
            if [ ${{ env.dryrun }} == "false" ]
98
            then
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
100
            else
101
              echo "DRYRUN: executing : curl -X DELETE -H \"Accept: application/vnd.github+json\" $GITHUB_API_URL/repos/$GITHUB_REPOSITORY/actions/caches/$id"
102
            fi
103
          done
104
      - name: Upload logs
105
        if: always()
106
        uses: actions/upload-artifact@v4
107
        with:
108
          name: ${{ github.job }}-Logs
109
          path: |
110
            ${{ env.logdir }}
111

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.