Last active
August 12, 2021 10:06
-
-
Save chinalwb/68ba726ff19a09397e71c2713a8aac49 to your computer and use it in GitHub Desktop.
The yml script for deleting a GitHub Package version in GitHub workflow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Use this segment in your workflow yml for deleting a GitHub package version | |
# Search and find these variables and replace with yours. | |
# ---------------- | |
# In step: Get packages list, set your own values for: | |
# YOUR ID OR YOUR ORG ID | |
# YOUR REPO NAME | |
# | |
# In step: Define version to delete, set your own version number to delete: | |
# 0.0.1 | |
# ---------------- | |
jobs: | |
DeleteExistingPackage: | |
# The type of runner that the job will run on | |
runs-on: ubuntu-latest | |
# Steps represent a sequence of tasks that will be executed as part of the job | |
steps: | |
- name: Get packages list | |
run: | | |
curl -X POST \ | |
-s \ | |
-H "Accept: application/vnd.github.package-deletes-preview+json" \ | |
-H "Authorization: bearer ${{ secrets.GITHUB_TOKEN }}" \ | |
-d '{"query":"query { repository(owner: \"YOUR ID OR YOUR ORG ID\", name:\"YOUR REPO NAME\") { packages(first: 20, names: \"YOUR PACKAGE\") {edges {node {id, name, versions(first:100) {edges {node {id, version}}}}}}}}"}' \ | |
-o /tmp/github_response.json \ | |
--url https://api.github.com/graphql | |
- name: Print cat response | |
run: cat /tmp/github_response.json | |
- name: Create idfile | |
run: echo -e "import json, sys\nidfile = open('/tmp/idfile', 'w')\nidfile.close()" | python | |
- name: Define version to delete | |
id: versionNameStep | |
run: echo "::set-output name=version::0.0.1" # replace 0.0.1 to the version number to delete | |
- name: Filter version id | |
run: echo -e "import json, sys\nf = open('/tmp/github_response.json',)\nobj = json.load(f)\npackages=obj['data']['repository']['packages']['edges'][0]['node']['versions']['edges']\nfor p in packages:\n if p['node']['version'] == '"${{steps.versionNameStep.outputs.version}}"':\n idfile = open('/tmp/idfile', 'w')\n idfile.write(p['node']['id'])\n idfile.close()\n break" | python | |
- name: Print cat idfile | |
run: cat /tmp/idfile | |
- name: Set package version id | |
id: versionIdStep | |
run: echo "::set-output name=id::"$(cat /tmp/idfile) | |
- name: Delete package version if exists | |
# run: echo "package version id = " ${{ steps.versionIdStep.outputs.id }} | |
uses: actions/delete-package-versions@v1 | |
if: steps.versionIdStep.outputs.id != '' | |
with: | |
package-version-ids: ${{ steps.versionIdStep.outputs.id }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment