Skip to main content

Check out Port for yourself ➜ 

Delete GitHub Repository

In the following guide, you are going to create a self-service action in Port that executes a GitHub workflow to delete a GitHub repository.

Available Github Integrations

This guide includes one or more steps that require integration with GitHub.
Port supports two GitHub integrations:

  • GitHub (Legacy) - uses a GitHub app, which is soon to be deprecated.
  • GitHub (Ocean) - uses the Ocean framework, recommended for new integrations.

Both integration options are present in this guide via tabs, choose the one that fits your needs.

Common use cases

  • Declutter Development: Clean up outdated, unused, or test repositories to streamline your GitHub environment.
  • Project Sunset: Offboard completed projects by gracefully deleting their repositories.
  • Enhanced Control: Manage repository lifecycles without needing in-depth GitHub permissions.

Prerequisites

  1. Port's GitHub Integration: Install it by clicking here. This is essential for Port to interact with your GitHub repositories.
  2. GitHub Data in Port: Ensure your repositories are synced with Port. If you haven't set this up yet, follow this quick guide.
  3. Workflow Repository: Decide on an existing repository where you'll store your GitHub workflow file, or create a dedicated repository for your Port actions.

Add GitHub secrets

  1. Create the following GitHub Action secrets:

Set up self-service action

  1. Go to the self-service page.

  2. Click on the + New Action button.

  3. Click on the {...} Edit JSON button.

  4. Copy and paste the following JSON configuration into the editor.

    Port Action: Delete GitHub Repository (click to expand)
    Replace the variables
    • <GITHUB-ORG> - your GitHub organization or user name.

    • <GITHUB-REPO-NAME> - your GitHub repository name.

    Note: Replace the blueprintIdentifier on line 30 with the id of your own blueprint.

    {
    "identifier": "service_delete_repo",
    "title": "Delete Repo",
    "icon": "Github",
    "description": "A github action that deletes a github repo",
    "trigger": {
    "type": "self-service",
    "operation": "DELETE",
    "userInputs": {
    "properties": {
    "org_name": {
    "icon": "Github",
    "title": "Organisation Name",
    "type": "string",
    "default": "default-org"
    },
    "delete_dependents": {
    "icon": "Github",
    "title": "Delete Dependent Items",
    "type": "boolean",
    "default": false
    }
    },
    "required": [],
    "order": [
    "org_name",
    "delete_dependents"
    ]
    },
    "blueprintIdentifier": "service"
    },
    "invocationMethod": {
    "type": "GITHUB",
    "org": "<GITHUB-ORG>",
    "repo": "<GITHUB-REPO-NAME>",
    "workflow": "delete-repo.yml",
    "workflowInputs": {
    "org_name": "{{inputs.org_name}}",
    "delete_dependents": "{{inputs.delete_dependents}}",
    "port_context": {
    "entity": "{{.entity.identifier}}",
    "blueprint": "{{.action.blueprint}}",
    "runId": "{{.run.id}}",
    "trigger": "{{ .trigger }}"
    }
    },
    "reportWorkflowStatus": true
    },
    "requiredApproval": false
    }
  5. Click Save to create the action.

Create GitHub workflow

Create a workflow file under .github/workflows/delete-repo.yml with the following content:

GitHub workflow script
delete-repo.yml
name: Delete Repository

on:
workflow_dispatch:
inputs:
org_name:
required: true
type: string
delete_dependents:
required: false
type: boolean
default: false
port_context:
required: true
type: string
jobs:
delete-repo:
runs-on: ubuntu-latest

steps:
- name: Inform starting of deletion
uses: port-labs/port-github-action@v1
with:
clientId: ${{ secrets.PORT_CLIENT_ID }}
clientSecret: ${{ secrets.PORT_CLIENT_SECRET }}
operation: PATCH_RUN
runId: ${{ fromJson(inputs.port_context).runId }}
logMessage: |
Deleting a github repository... ⛴️

- name: Delete Repository
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
REPO_NAME: ${{ fromJson(inputs.port_context).entity }}
run: |
echo $GH_TOKEN
HTTP_STATUS=$(curl -s -o /dev/null -w "%{http_code}" \
-X DELETE \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer $GH_TOKEN" \
"https://api.github.com/repos/${{ inputs.org_name }}/$REPO_NAME")

echo "HTTP Status: $HTTP_STATUS"

# Check if HTTP_STATUS is 204 (No Content)
if [ $HTTP_STATUS -eq 204 ]; then
echo "Repository deleted successfully."
echo "delete_successful=true" >> $GITHUB_ENV
else
echo "Failed to delete repository. HTTP Status: $HTTP_STATUS"
echo "delete_successful=false" >> $GITHUB_ENV
fi

- name: Delete record in Port
if: ${{ env.delete_successful == 'true' }}
uses: port-labs/port-github-action@v1
with:
clientId: ${{ secrets.PORT_CLIENT_ID }}
clientSecret: ${{ secrets.PORT_CLIENT_SECRET }}
operation: DELETE
delete_dependents: ${{ inputs.delete_dependents }}
identifier: ${{ fromJson(inputs.port_context).entity }}
blueprint: ${{ fromJson(inputs.port_context).blueprint }}

- name: Inform completion of deletion
uses: port-labs/port-github-action@v1
with:
clientId: ${{ secrets.PORT_CLIENT_ID }}
clientSecret: ${{ secrets.PORT_CLIENT_SECRET }}
operation: PATCH_RUN
runId: ${{ fromJson(inputs.port_context).runId }}
logMessage: |
GitHub repository deleted! ✅

Let's test it!

  1. Go to the self-service page of your Port application.
  2. Choose the GitHub repository you want to delete.
  3. Click on Execute.
  4. Done! wait for the repository to be deleted in GitHub.

More Relevant Guides