Skip to content

Instantly share code, notes, and snippets.

@xlionjuan
Created March 30, 2024 21:15
Show Gist options
  • Select an option

  • Save xlionjuan/3273178a112e0bdec6decdc52e860678 to your computer and use it in GitHub Desktop.

Select an option

Save xlionjuan/3273178a112e0bdec6decdc52e860678 to your computer and use it in GitHub Desktop.
Delete old Release with actions/github-script@v7
steps:
- name: Delete old Release
uses: actions/github-script@v7
with:
github-token: ${{secrets.GITHUB_TOKEN}}
script: |
const { owner, repo } = context.repo
// Fetch the latest release information
const latestRelease = await github.rest.repos.getLatestRelease({
owner,
repo,
});
// Check if the latest release information was successfully retrieved
if (latestRelease && latestRelease.data && latestRelease.data.id) {
// If there is a latest release, then delete it
await github.rest.repos.deleteRelease({
owner,
repo,
release_id: latestRelease.data.id,
});
console.log(`Release id ${latestRelease.data.id} has been deleted.`);
} else {
// If no latest release was found or retrieval failed, output a message
console.log("No latest release found or failed to retrieve it.");
}
@xlionjuan
Copy link
Author

Generated by ChatGPT

Sure, I'll translate the provided text and the comments within the YAML script into English for you.

Translated Text

Based on your request, if your goal is to use GitHub Actions with actions/github-script@v7 and the octokit client to fetch and delete the latest release from a repository, here is an adjusted and regenerated code snippet. This example will first fetch the latest release information for the repository and then delete that release.

steps:
  - uses: actions/github-script@v7
    with:
      github-token: ${{secrets.GITHUB_TOKEN}}
      script: |
        const { owner, repo } = context.repo
        // Fetch the latest release information
        const latestRelease = await github.rest.repos.getLatestRelease({
          owner,
          repo,
        });

        // Check if the latest release information was successfully retrieved
        if (latestRelease && latestRelease.data && latestRelease.data.id) {
          // If there is a latest release, then delete it
          await github.rest.repos.deleteRelease({
            owner,
            repo,
            release_id: latestRelease.data.id,
          });
          console.log(`Release id ${latestRelease.data.id} has been deleted.`);
        } else {
          // If no latest release was found or retrieval failed, output a message
          console.log("No latest release found or failed to retrieve it.");
        }

Explanation of the Code

  1. Fetching Repository Information: Retrieves the owner and repository name from the context of the current GitHub Actions run.
  2. Fetching the Latest Release: Uses the octokit.rest.repos.getLatestRelease method to get information about the latest release of the repository.
  3. Checking for Successful Retrieval: Determines if the latest release information was successfully retrieved by checking if latestRelease.data.id exists.
  4. Deleting the Release: If the latest release information was successfully retrieved, it uses the octokit.rest.repos.deleteRelease method to delete the specified release using its ID found in latestRelease.data.id.
  5. Handling No Latest Release Found: Outputs a message to the console if no latest release was found or if there was a failure in retrieving it.

Ensure your GitHub Actions workflow configuration file syntax is correct and that you have the necessary permissions to perform these actions. This code should be placed in the appropriate section of your GitHub Actions workflow .yml file.

@xlionjuan
Copy link
Author

First, I found this action, but it is deprecated and recommend to use github-script, it works, but it is using v4, so I upgrade to v7, than it broke because newer github-script is using octokit/rest.js now, so I provide both getLatestRelease and deleteRelease from the API docs to ChatGPT.
https://github.com/ame-yu/action-delete-latest-release

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment