ci: add a delete artifact action (#2999)

This commit is contained in:
miampf 2024-03-25 13:36:09 +00:00 committed by GitHub
parent 4ca9db156b
commit febe8f0801
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 60 additions and 0 deletions

View File

@ -0,0 +1,17 @@
name: Delete artifact
description: Delete an artifact by name
inputs:
name:
description: 'The name of the artifact.'
required: true
workflowID:
description: 'The ID of the workflow.'
required: true
runs:
using: "composite"
steps:
- name: Delete artifact
shell: bash
run: ./.github/actions/artifact_delete/delete_artifact.sh ${{ inputs.workflowID }} ${{ inputs.name }}

View File

@ -0,0 +1,43 @@
#!/bin/bash
# get_artifact_id retrieves the artifact id of
# an artifact that was generated by a workflow.
# $1 should be the workflow run id. $2 should be the artifact name.
function get_artifact_id {
artifact_id="$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/edgelesssys/constellation/actions/runs/$1/artifacts" --jq ".artifacts |= map(select(.name==\"$2\")) | .artifacts[0].id" || exit 1)"
echo "$artifact_id"
}
# delete_artifact_by_id deletes an artifact by its artifact id.
# $1 should be the id of the artifact.
function delete_artifact_by_id {
gh api \
--method DELETE \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
"/repos/edgelesssys/constellation/actions/artifacts/$1" || exit 1
}
workflow_id="$1"
artifact_name="$2"
if [[ -z $workflow_id ]]; then
echo "[X] No workflow id provided."
echo "Usage: delete_artifact.sh <WORKFLOW_ID> <ARTIFACT_NAME>"
exit 1
fi
if [[ -z $artifact_name ]]; then
echo "[X] No artifact name provided."
echo "Usage: delete_artifact.sh <WORKFLOW_ID> <ARTIFACT_NAME>"
exit 1
fi
echo "[*] retrieving artifact ID"
artifact_id="$(get_artifact_id "$workflow_id" "$artifact_name")"
echo "[*] deleting artifact with ID $artifact_id"
delete_artifact_by_id "$artifact_id"