|
| 1 | +# ==================================================================================================================== # |
| 2 | +# Authors: # |
| 3 | +# Patrick Lehmann # |
| 4 | +# # |
| 5 | +# ==================================================================================================================== # |
| 6 | +# Copyright 2020-2026 The pyTooling Authors # |
| 7 | +# # |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); # |
| 9 | +# you may not use this file except in compliance with the License. # |
| 10 | +# You may obtain a copy of the License at # |
| 11 | +# # |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 # |
| 13 | +# # |
| 14 | +# Unless required by applicable law or agreed to in writing, software # |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, # |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # |
| 17 | +# See the License for the specific language governing permissions and # |
| 18 | +# limitations under the License. # |
| 19 | +# # |
| 20 | +# SPDX-License-Identifier: Apache-2.0 # |
| 21 | +# ==================================================================================================================== # |
| 22 | +name: 🗑️ Cleanup |
| 23 | + |
| 24 | +on: |
| 25 | + workflow_call: |
| 26 | + inputs: |
| 27 | + ubuntu_image_version: |
| 28 | + description: 'Ubuntu image version.' |
| 29 | + required: false |
| 30 | + default: '24.04' |
| 31 | + type: string |
| 32 | + json: |
| 33 | + description: 'JSON string of artifact names' |
| 34 | + required: false |
| 35 | + default: '{}' |
| 36 | + type: string |
| 37 | + artifact-json-ids: |
| 38 | + description: 'Artifacts to be removed by JSON name.' |
| 39 | + required: false |
| 40 | + default: '' |
| 41 | + type: string |
| 42 | + json2: |
| 43 | + description: 'Second JSON string of artifact names' |
| 44 | + required: false |
| 45 | + default: '{}' |
| 46 | + type: string |
| 47 | + artifact-json-ids2: |
| 48 | + description: 'Second set of artifacts to be removed by JSON name.' |
| 49 | + required: false |
| 50 | + default: '' |
| 51 | + type: string |
| 52 | + others: |
| 53 | + description: 'Other artifacts to be removed.' |
| 54 | + required: false |
| 55 | + default: '' |
| 56 | + type: string |
| 57 | + |
| 58 | +jobs: |
| 59 | + CleanUp: |
| 60 | + name: 🗑️ Artifact Cleanup |
| 61 | + runs-on: "ubuntu-${{ inputs.ubuntu_image_version }}" |
| 62 | + steps: |
| 63 | + - name: 🧮 Compute artifact names |
| 64 | + id: compute |
| 65 | + if: inputs.json != '' && inputs.json != '{}' |
| 66 | + shell: python |
| 67 | + run: | |
| 68 | + from json import loads, dumps |
| 69 | + from os import getenv |
| 70 | + from pathlib import Path |
| 71 | +
|
| 72 | + artifactNames = loads("""${{ inputs.json }}""") |
| 73 | + artifactsToDelect = [k for k in "${{ inputs.artifact-json-ids }}".split(" ") if k != ""] |
| 74 | +
|
| 75 | + artifacts = [] |
| 76 | + for name in artifactsToDelect: |
| 77 | + if name.startswith('#'): |
| 78 | + continue |
| 79 | + match name.split(":"): |
| 80 | + case [key] if key in artifactNames: |
| 81 | + artifacts.append(artifactNames[key]) |
| 82 | + case [key, postfix] if key in artifactNames: |
| 83 | + artifacts.append(f"{artifactNames[key]}{postfix}") |
| 84 | + case [prefix, key] if key in artifactNames: |
| 85 | + artifacts.append(f"{prefix}{artifactNames[key]}") |
| 86 | + case [prefix, key, postfix] if key in artifactNames: |
| 87 | + artifacts.append(f"{prefix}{artifactNames[key]}{postfix}") |
| 88 | + case _: |
| 89 | + printf(f"Name '{name}' not found in JSON dictionary.") |
| 90 | +
|
| 91 | + print("Artifact to delete:") |
| 92 | + for name in artifacts: |
| 93 | + print(f" {name}") |
| 94 | +
|
| 95 | + # Write requirements path to special file |
| 96 | + github_output = Path(getenv("GITHUB_OUTPUT")) |
| 97 | + print(f"GITHUB_OUTPUT: {github_output}") |
| 98 | + with github_output.open("a+") as f: |
| 99 | + f.write(f"artifacts={dumps(artifacts)}\n") |
| 100 | +
|
| 101 | + - name: 🗑️ Delete artifacts |
| 102 | + uses: geekyeggo/delete-artifact@v5 |
| 103 | + if: inputs.artifact-json-ids != '' && steps.compute.outputs.artifacts != '' |
| 104 | + continue-on-error: true |
| 105 | + with: |
| 106 | + name: | |
| 107 | + ${{ join(fromJSON(steps.compute.outputs.artifacts).*, ' |
| 108 | + ') }} |
| 109 | +
|
| 110 | + - name: 🧮 Compute artifact names for second JSON dictionary |
| 111 | + id: compute2 |
| 112 | + if: inputs.json2 != '' && inputs.json2 != '{}' |
| 113 | + shell: python |
| 114 | + run: | |
| 115 | + from json import loads, dumps |
| 116 | + from os import getenv |
| 117 | + from pathlib import Path |
| 118 | +
|
| 119 | + artifactNames = loads("""${{ inputs.json2 }}""") |
| 120 | + artifactsToDelect = [k for k in "${{ inputs.artifact-json-ids2 }}".split(" ") if k != ""] |
| 121 | +
|
| 122 | + artifacts = [] |
| 123 | + for name in artifactsToDelect: |
| 124 | + if name.startswith('#'): |
| 125 | + continue |
| 126 | + match name.split(":"): |
| 127 | + case [key] if key in artifactNames: |
| 128 | + artifacts.append(artifactNames[key]) |
| 129 | + case [key, postfix] if key in artifactNames: |
| 130 | + artifacts.append(f"{artifactNames[key]}{postfix}") |
| 131 | + case [prefix, key] if key in artifactNames: |
| 132 | + artifacts.append(f"{prefix}{artifactNames[key]}") |
| 133 | + case [prefix, key, postfix] if key in artifactNames: |
| 134 | + artifacts.append(f"{prefix}{artifactNames[key]}{postfix}") |
| 135 | + case _: |
| 136 | + printf(f"Name '{name}' not found in JSON dictionary.") |
| 137 | +
|
| 138 | + print("Artifact to delete:") |
| 139 | + for name in artifacts: |
| 140 | + print(f" {name}") |
| 141 | +
|
| 142 | + # Write requirements path to special file |
| 143 | + github_output = Path(getenv("GITHUB_OUTPUT")) |
| 144 | + print(f"GITHUB_OUTPUT: {github_output}") |
| 145 | + with github_output.open("a+") as f: |
| 146 | + f.write(f"artifacts={dumps(artifacts)}\n") |
| 147 | +
|
| 148 | + - name: 🗑️ Delete artifacts |
| 149 | + uses: geekyeggo/delete-artifact@v5 |
| 150 | + if: inputs.artifact-json-ids2 != '' && steps.compute2.outputs.artifacts != '' |
| 151 | + continue-on-error: true |
| 152 | + with: |
| 153 | + name: | |
| 154 | + ${{ join(fromJSON(steps.compute.outputs.artifacts).*, ' |
| 155 | + ') }} |
| 156 | +
|
| 157 | + - name: 🗑️ Delete other Artifacts |
| 158 | + uses: geekyeggo/delete-artifact@v5 |
| 159 | + if: ${{ inputs.others != '' }} |
| 160 | + with: |
| 161 | + name: ${{ inputs.others }} |
0 commit comments