From febe8f0801a34034c934fc6b5e5fe537492fe275 Mon Sep 17 00:00:00 2001 From: miampf Date: Mon, 25 Mar 2024 13:36:09 +0000 Subject: [PATCH] ci: add a delete artifact action (#2999) --- .github/actions/artifact_delete/action.yml | 17 ++++++++ .../artifact_delete/delete_artifact.sh | 43 +++++++++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 .github/actions/artifact_delete/action.yml create mode 100755 .github/actions/artifact_delete/delete_artifact.sh diff --git a/.github/actions/artifact_delete/action.yml b/.github/actions/artifact_delete/action.yml new file mode 100644 index 000000000..e2f001621 --- /dev/null +++ b/.github/actions/artifact_delete/action.yml @@ -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 }} diff --git a/.github/actions/artifact_delete/delete_artifact.sh b/.github/actions/artifact_delete/delete_artifact.sh new file mode 100755 index 000000000..9c94f8c69 --- /dev/null +++ b/.github/actions/artifact_delete/delete_artifact.sh @@ -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 " + exit 1 +fi + +if [[ -z $artifact_name ]]; then + echo "[X] No artifact name provided." + echo "Usage: delete_artifact.sh " + 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"